﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / problem inner joininig a derived column / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 09:13:07 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote][b]memymasta (12/4/2012)[/b][hr]@Cadavre - Thats that did the trick!@CELKO - Very useful insight, i like it. One thing i don't understand is: [quote]That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year.[/quote]  Do you store that as date? [code="sql"]SELECT CAST('2012-01-00' AS DATE)[/code]This don't seem to work.@Greg Snidow - Ah yes! Your small change made it work. Updated the code a bit and it looks really good now.[code="sql"]CREATE TABLE [#a](	[id] [int] NOT NULL,	[recorddate] [date] NULL,	[myvalue] [int] NULL,) ON [PRIMARY]GOINSERT INTO [#a] (id, [recorddate], [myvalue])SELECT 1,'2012-01-02',10 UNION ALLSELECT 2,'2012-01-13',20 UNION ALLSELECT 3,'2012-02-02',30 UNION ALLSELECT 4,'2012-02-24',40 UNION ALLSELECT 5,'2012-08-02',50 UNION ALLSELECT 6,'2012-12-01',60 UNION ALLSELECT 7,'2012-12-28',70GOWITH myCTE (c)AS(	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+1,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+2,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+3,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+4,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+5,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+6,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+7,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+8,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+9,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+10,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+11,0))SELECT DISTINCT c, ISNULL(SUM(myvalue),0) as myValueFROM myCTE LEFT JOIN #a  ON myCTE.c=CAST(YEAR(#a.recorddate) as CHAR(4))+'-'+CAST(MONTH(#a.recorddate) as CHAR(2))+'-'+'01' GROUP BY cGODROP TABLE #a[/code][/quote]A couple things i may change.  first is your calendar table, what happens when you want to start at a different date.  the version below to me has a smaller maintenance area when it comes to changing the date range you want in the report.  the second change is the ON clause, i replaced your string concatenation to some date math to get the first of the month so we have like data types. [code="sql"]WITH cteTally(N) AS (SELECT 0 UNION ALL                     SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))                       FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))x(N)                 CROSS JOIN (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))y(N)),myCTE (c) AS (SELECT TOP 12 DATEADD(MM,N,DATEADD(YY, DATEDIFF(YY,0,GETDATE()),0))                FROM cteTally)SELECT c, ISNULL(SUM(myvalue),0) as myValue  FROM myCTE   LEFT JOIN #a     ON myCTE.c = DATEADD(MM,DATEDIFF(MM,0,#a.recorddate),0) GROUP BY c[/code]EDIT:  had Tally (my persisted tally table) and not the cteTally.If you have no idea what a tally table is check out this great article by Jeff Moden  [url]http://www.sqlservercentral.com/articles/T-SQL/62867/[/url]</description><pubDate>Tue, 04 Dec 2012 10:50:58 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>@Cadavre - Thats that did the trick!@CELKO - Very useful insight, i like it. One thing i don't understand is: [quote]That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year.[/quote]  Do you store that as date? [code="sql"]SELECT CAST('2012-01-00' AS DATE)[/code]This don't seem to work.@Greg Snidow - Ah yes! Your small change made it work. Updated the code a bit and it looks really good now.[code="sql"]CREATE TABLE [#a](	[id] [int] NOT NULL,	[recorddate] [date] NULL,	[myvalue] [int] NULL,) ON [PRIMARY]GOINSERT INTO [#a] (id, [recorddate], [myvalue])SELECT 1,'2012-01-02',10 UNION ALLSELECT 2,'2012-01-13',20 UNION ALLSELECT 3,'2012-02-02',30 UNION ALLSELECT 4,'2012-02-24',40 UNION ALLSELECT 5,'2012-08-02',50 UNION ALLSELECT 6,'2012-12-01',60 UNION ALLSELECT 7,'2012-12-28',70GOWITH myCTE (c)AS(	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+1,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+2,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+3,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+4,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+5,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+6,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+7,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+8,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+9,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+10,0)	UNION ALL	SELECT DATEADD(mm, DATEDIFF(yy, 0, GETDATE())*12+11,0))SELECT DISTINCT c, ISNULL(SUM(myvalue),0) as myValueFROM myCTE LEFT JOIN #a  ON myCTE.c=CAST(YEAR(#a.recorddate) as CHAR(4))+'-'+CAST(MONTH(#a.recorddate) as CHAR(2))+'-'+'01' GROUP BY cGODROP TABLE #a[/code]</description><pubDate>Tue, 04 Dec 2012 02:10:09 GMT</pubDate><dc:creator>memymasta</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote][b]memymasta (11/30/2012)[/b][hr]Ive almost got it to work... but i fail on the joining step where i join my CTE with a derived column...[/quote]When you say you fail, what exactly does that mean?  In your ON criteria, you've got...[code]ON myCTE.c=#a.CAST(YEAR(#a.recorddate) [/code]change it to (take out the extra '#a.' alias, and leave the rest the same)...[code]ON myCTE.c=CAST(YEAR(#a.recorddate) [/code]And it worked for me.  The only differece being there are NULL's where you have zero's in your mock results.  It looks like there are some better suggestions any way, but I was just curious if this was the only 'failure' you were experiencing.</description><pubDate>Mon, 03 Dec 2012 15:29:03 GMT</pubDate><dc:creator>Greg Snidow</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote][b]Michael Valentine Jones (12/3/2012)[/b][hr]A calendar table is not really needed for this:[code="sql"]select	YearMonth = dateadd(mm,datediff(mm,0,[recorddate]),0),	MyValueSum = sum([myvalue])from	#agroup by	dateadd(mm,datediff(mm,0,[recorddate]),0)order by	dateadd(mm,datediff(mm,0,[recorddate]),0)[/code]Results:[code="plain"]YearMonth                MyValueSum----------------------- -----------2012-01-01 00:00:00.000          302012-02-01 00:00:00.000          702012-08-01 00:00:00.000          502012-12-01 00:00:00.000         130[/code][/quote]The op would also like "0" for the intervening months so we have to figure out some way to accomplish that.  a calendar table to join to seems to be the easiest from what i can see.</description><pubDate>Mon, 03 Dec 2012 11:34:09 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>A calendar table is not really needed for this:[code="sql"]select	YearMonth = dateadd(mm,datediff(mm,0,[recorddate]),0),	MyValueSum = sum([myvalue])from	#agroup by	dateadd(mm,datediff(mm,0,[recorddate]),0)order by	dateadd(mm,datediff(mm,0,[recorddate]),0)[/code]Results:[code="plain"]YearMonth                MyValueSum----------------------- -----------2012-01-01 00:00:00.000          302012-02-01 00:00:00.000          702012-08-01 00:00:00.000          502012-12-01 00:00:00.000         130[/code]</description><pubDate>Mon, 03 Dec 2012 10:51:25 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML. The report period table gives a name to a range of dates that is common to the entire enterprise. CREATE TABLE Report_Periods(report_name VARCHAR(30) NOT NULL PRIMARY KEY, report_start_date DATE NOT NULL, report_end_date DATE NOT NULL,CONSTRAINT date_ordering CHECK (report_start_date &amp;lt;= report_end_date),etc);These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantage is that it will sort with the ISO-8601 data format required by Standard SQL. SELECT R.report_name, SUM(F.my_value) AS month_value_tot  FROM Foobar, Report_Periods AS R WHERE F.recording_date       BETWEEN R.report_start_date AND R.report_end_date GROUP BY R.report_name;</description><pubDate>Mon, 03 Dec 2012 10:11:22 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote][b]memymasta (12/3/2012)[/b][hr]A followup question: if i only need the years span of 2000-2050, how do i select them instead of[code="sql"]SELECT TOP 97199 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM CTE5 [/code]Thanks[/quote]Well, there are 611 months between 2000-01-01 and 2050-12-01, so you'd want to change the code to something like this: -[code="sql"]CTE6(N) AS (SELECT 0 UNION ALL            SELECT TOP 611 ROW_NUMBER() OVER(ORDER BY (SELECT NULL))            FROM CTE5),TALLY(N) AS (SELECT DATEADD(month, N, CAST('2000' AS DATETIME))             FROM CTE6)[/code]Note the change to the "tally" section, which starts the calendar at 2000-01-01 instead of the previous version which started at 1900-01-01.[hr][b]--EDIT--[/b][quote][b]memymasta (12/3/2012)[/b][hr]Any thought on why the query stagnates?UPDATE: Checked some variables and getting 97k rows was a slight overkill. Changing it to 2050 rows made a big difference, and no more hickups. :)[/quote]Without seeing an actual execution plan, no. I'd imagine it'll be an indexing issue. You could also try changing the CTE to a physical table of all of the months, which when properly indexes may give some performance boost.</description><pubDate>Mon, 03 Dec 2012 09:33:39 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>A followup question: if i only need the years span of 2000-2050, how do i select them instead of[code="sql"]SELECT TOP 97199 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM CTE5 [/code]Thanks</description><pubDate>Mon, 03 Dec 2012 08:42:12 GMT</pubDate><dc:creator>memymasta</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote]In fact, my CTE tally table is a calendar table (note: TALLY(N) AS (SELECT DATEADD(month, N, 0) FROM CTE6) ) of months starting at 1900-01-01 and ending with 9999-12-01 (total is 97,200 rows, so I probably went overboard with the CTEs that build the sequential number lists [Tongue] )[/quote]Thanks once again for an excellent reply. The table worked well with the real data. However there's some weird hiccups with the query. If i execute the query multiple times, it goes into for ever executing mode... i.e cant complete the execution.Here's the slightly modified query:[code="sql"]WITH CTE(N) AS (SELECT 1 FROM (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) a(N)),CTE2(N) AS (SELECT 1 FROM CTE x CROSS JOIN CTE y),CTE3(N) AS (SELECT 1 FROM CTE2 x CROSS JOIN CTE2 y),CTE4(N) AS (SELECT 1 FROM CTE3 x CROSS JOIN CTE3 y),CTE5(N) AS (SELECT 1 FROM CTE4 x CROSS JOIN CTE4 y),CTE6(N) AS (SELECT 0 UNION ALL            SELECT TOP 97199 ROW_NUMBER() OVER(ORDER BY (SELECT NULL))            FROM CTE5),TALLY(N) AS (SELECT DATEADD(month, N, 0)             FROM CTE6)SELECT a.N AS recorddate, ISNULL(b.myValue,0) AS myValueFROM TALLY aOUTER APPLY (SELECT DATEADD(month, DATEDIFF(month, 0, f.recorddate), 0), SUM(e.numvalue)             FROM admin_tasks as e INNER JOIN admin_tasks as f ON e.id=f.numvalue             WHERE DATEADD(month, DATEDIFF(month, 0, f.recorddate), 0) = a.N AND f.actionid=5 AND f.numtype=1             GROUP BY DATEADD(month, DATEDIFF(month, 0, f.recorddate), 0)			) b(recorddate,myValue)CROSS APPLY (SELECT MIN(DATEADD(month, DATEDIFF(month, 0, h.recorddate), 0)),             MAX(DATEADD(month, DATEDIFF(month, 0, h.recorddate), 0))             FROM admin_tasks as g INNER JOIN admin_tasks as h ON g.id=h.numvalue WHERE h.actionid=5 AND h.numtype=1) d(minDate,maxDate)WHERE a.N &amp;gt;= d.minDate AND a.N &amp;lt;= d.maxDateORDER BY recorddate ASC[/code]And here's the statistics IO &amp; time[code="plain"](11 row(s) affected)Table 'admin_tasks'. Scan count 17, logical reads 8099, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table 'Worktable'. Scan count 11, logical reads 28546, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times:   CPU time = 187 ms,  elapsed time = 257 ms.SQL Server parse and compile time:    CPU time = 0 ms, elapsed time = 0 ms.[/code]Any thought on why the query stagnates?UPDATE: Checked some variables and getting 97k rows was a slight overkill. Changing it to 2050 rows made a big difference, and no more hickups. :)</description><pubDate>Mon, 03 Dec 2012 06:48:47 GMT</pubDate><dc:creator>memymasta</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote]In fact, my CTE tally table is a calendar table (note: TALLY(N) AS (SELECT DATEADD(month, N, 0) FROM CTE6) ) of months starting at 1900-01-01 and ending with 9999-12-01 (total is 97,200 rows, so I probably went overboard with the CTEs that build the sequential number lists :-P )[/quote]You're right.  :-)  Given the approach used in the initial query, it didn't seem that the OP was familiar with calendar or tally tables.  Just wanted to provide further background using the articles cited.  You've given the OP a great example of why tally and calendar tables are powerful tools.  :-)</description><pubDate>Fri, 30 Nov 2012 09:16:31 GMT</pubDate><dc:creator>kl25</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>[quote][b]kl25 (11/30/2012)[/b][hr]To follow-up my earlier suggestion, note that Cadavre's approach uses a tally table.  A calendar table is a specialized version of a tally table.[/quote]In fact, my CTE tally table is a calendar table (note: TALLY(N) AS (SELECT DATEADD(month, N, 0) FROM CTE6) ) of months starting at 1900-01-01 and ending with 9999-12-01 (total is 97,200 rows, so I probably went overboard with the CTEs that build the sequential number lists :-P )</description><pubDate>Fri, 30 Nov 2012 09:10:31 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>To follow-up my earlier suggestion, note that Cadavre's approach uses a tally table.  A calendar table is a specialized version of a tally table.It's a powerful tool.  In addition to the article on calendar tables, you may find the following articles about tally tables give you a helpful addition to your querying tools.[url=http://www.sqlservercentral.com/articles/T-SQL/62867/][b]Jeff Moden on Tally Tables[/b][/url][url=http://www.sqlservercentral.com/articles/Tally+Table/70735/][b]Stefan Krzywicki on the use of Tally Tables - I[/b][/url][url=http://www.sqlservercentral.com/articles/Tally+Table/70738/][b]Stefan Krzywicki on the use of Tally Tables - II[/b][/url]</description><pubDate>Fri, 30 Nov 2012 09:05:16 GMT</pubDate><dc:creator>kl25</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>Not currently at my desk so the syntax might need correcting, but could you do something like this?[code="sql"]WITH CTE(N) AS (SELECT 1 FROM (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))a(N)),CTE2(N) AS (SELECT 1 FROM CTE x CROSS JOIN CTE y),CTE3(N) AS (SELECT 1 FROM CTE2 x CROSS JOIN CTE2 y),CTE4(N) AS (SELECT 1 FROM CTE3 x CROSS JOIN CTE3 y),CTE5(N) AS (SELECT 1 FROM CTE4 x CROSS JOIN CTE4 y),CTE6(N) AS (SELECT 0 UNION ALL            SELECT TOP 97199 ROW_NUMBER() OVER(ORDER BY (SELECT NULL))            FROM CTE5),TALLY(N) AS (SELECT DATEADD(month, N, 0)             FROM CTE6)SELECT a.N AS recorddate, ISNULL(b.myValue,0) AS myValueFROM TALLY aOUTER APPLY (SELECT DATEADD(month, DATEDIFF(month, 0, [recorddate]), 0), SUM([myvalue])             FROM [#a] c             WHERE DATEADD(month, DATEDIFF(month, 0, [recorddate]), 0) = a.N             GROUP BY DATEADD(month, DATEDIFF(month, 0, [recorddate]), 0)			) b(recorddate,myValue)CROSS APPLY (SELECT MIN(DATEADD(month, DATEDIFF(month, 0, [recorddate]), 0)),             MAX(DATEADD(month, DATEDIFF(month, 0, [recorddate]), 0))             FROM [#a]) d(minDate,maxDate)WHERE a.N &amp;gt;= d.minDate AND a.N &amp;lt;= d.maxDate;[/code]</description><pubDate>Fri, 30 Nov 2012 08:56:34 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>Try building a calendar table, joining on your dates.  Then you can aggregate by month using the month values in the calendar table.  See the  [url=http://www.sqlservercentral.com/articles/T-SQL/70482/][b]following article[/b][/url] for details about how to use calendar tables.</description><pubDate>Fri, 30 Nov 2012 08:51:00 GMT</pubDate><dc:creator>kl25</dc:creator></item><item><title>problem inner joininig a derived column</title><link>http://www.sqlservercentral.com/Forums/Topic1391360-392-1.aspx</link><description>Hi, I'm trying to get a nice chart working, that gets data from SQL Server, but having trouble getting the right data...I have a table, that have a value and a date... I want to sum all the values and group them by 12 months of the current year.Se lets say i got the table [#a]... With id, recorddate and myvalue columns as shown below.[code="sql"]CREATE TABLE [#a](	[id] [int] NOT NULL,	[recorddate] [date] NULL,	[myvalue] [int] NULL,) ON [PRIMARY]GOINSERT INTO [#a] (id, [recorddate], [myvalue])SELECT 1,'2012-01-02',10 UNION ALLSELECT 2,'2012-01-13',20 UNION ALLSELECT 3,'2012-02-02',30 UNION ALLSELECT 4,'2012-02-24',40 UNION ALLSELECT 5,'2012-08-02',50 UNION ALLSELECT 6,'2012-12-01',60 UNION ALLSELECT 7,'2012-12-28',70[/code]And i would like to get this result...'2012-01-01' || '30''2012-02-01' || '70''2012-03-01' || '0''2012-04-01' || '0''2012-05-01' || '0''2012-06-01' || '0''2012-07-01' || '0''2012-08-01' || '50''2012-09-01' || '0''2012-10-01' || '0''2012-11-01' || '0''2012-12-01' || '130'Ive almost got it to work... but i fail on the joining step where i join my CTE with a derived column... as seen below:[code="sql"]CREATE TABLE [#a](	[id] [int] NOT NULL,	[recorddate] [date] NULL,	[myvalue] [int] NULL,) ON [PRIMARY]GOINSERT INTO [#a] (id, [recorddate], [myvalue])SELECT 1,'2012-01-02',10 UNION ALLSELECT 2,'2012-01-13',20 UNION ALLSELECT 3,'2012-02-02',30 UNION ALLSELECT 4,'2012-02-24',40 UNION ALLSELECT 5,'2012-08-02',50 UNION ALLSELECT 6,'2012-12-01',60 UNION ALLSELECT 7,'2012-12-28',70WITH myCTE (c)AS(	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-1-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-2-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-3-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-4-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-5-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-6-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-7-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-8-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-9-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-10-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-11-1' AS DATE) AS c	UNION ALL	SELECT CAST(CAST(YEAR(GETDATE()) AS CHAR(4))+'-12-1' AS DATE) AS c)SELECT DISTINCT c, SUM(myvalue) FROM myCTE LEFT JOIN #a  ON myCTE.c=#a.CAST(YEAR(#a.recorddate) as CHAR(4))+'-'+CAST(MONTH(#a.recorddate) as CHAR(2))+'-'+'01' GROUP BY cDROP TABLE #a[/code]Is it possible to achieve it this way?</description><pubDate>Fri, 30 Nov 2012 08:21:40 GMT</pubDate><dc:creator>memymasta</dc:creator></item></channel></rss>