Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a requirement that I have to retrieve data base on month and year.I am using the below query

SQL
SELECT  SUM(areasft),DATEPART(year, sdate),DATEPART(month, sdate)  FROM storedata
GROUP BY DATEPART(year, sdate) ,DATEPART(month, sdate)
ORDER BY DATEPART(year, sDate) ,DATEPART(month, sDate)


The above query is retrieve data like below.
SUM YEAR MONTH
51013 2000 1
30970 2007 1
NULL 2007 3
900 2007 6
807 2007 8
NULL 2007 9
1756 2007 10
7535 2007 11
NULL 2008 1
1193 2008 2
4230 2008 3
350 2008 4
2200 2008 5
4660 2008 6
6685 2008 8

But I need to display including all the months of the year along with year irrespective of SUM . My query only displays only some months in a year.

Please solve this problem.

Regards,

N.SRIRAM
Posted

Maybe create an auxillary table to fill the gaps :
http://ask.metafilter.com/43840/Filling-Sparse-Data-In-An-SQL-Query[^]

Cheers
 
Share this answer
 
I'm not a DBA, nor really that skilled in it, but this is the way I'd approach it. For parts you need to research, google is definitely your friend:

0) Create a temporary table with all of the months in it - call it TEMP_YEARS.

Null 0 1
NULL 0 2
NULL 0 3
..
NULL 0 12

1) Create a stored procedure that updates that temp table with a given year (that you specify as a parameter) - call it sp_YearUpdate

2) Read all the data from your real table into a 2nd temp table (call it TEMP_DATA)

3) Determine the earliest year represented in the TEMP_DATA table.

4) Call your sp_YearUpdate with the earliest year

5) Merge TEMP_YEARS with your real table (using a union?)

6) Increment the year

7) Go back to step 4 (repeat as necessary)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900