SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Display all months with no data


Display all months with no data

Author
Message
boehnc
boehnc
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 102
Newbie...I've seen some suggestions on how to display query results for all months even if there is not data.

Ex. Jan 12, 4
Feb 12, 7
Sept 12, 9
Dec 12, 4

what I'd like

Jan 12, 4
Feb 12, 7
Mar 12, 0
April 12, 0
ETC

I created a dimDate table and left outer joined with my data table, but is still not populating all months. Any help would be appreciated. Below is a scaled down view of my query. Thank you.


SELECT

month(da.calendar_dt) as month, da.year,
isnull(sum(p.psioutcome),0) as count


FROM [AnalystsEDSSandbox].[dbo].[dimDate] da
left outer join quality.dbo.fact_patientsafetyindicators p on CAST(p.dischargedatetime AS DATE)= da.calendar_dt


where da.calendar_dt>'2012-01-01'


group by month(da.calendar_dt),da.year

order by year,month(da.calendar_dt)
Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48749 Visits: 10844
I don't have your table structures, but I'd like to present an alternate way. Instead of relying on the data that's there, create a table of months you want to report and then update it from your data table. This will use a tally table to populate a temp table with one row per month going back 13 months from a base date. If you aren't familiar with tally tables yet, check out the article at http://www.sqlservercentral.com/articles/T-SQL/62867/ and take the time to get familiar with them. They will change the way you look at data.

DECLARE @dtmBase datetime = '11/01/2013';

IF OBJECT_ID('tempdb.dbo.#tblMonths', 'u') IS NOT NULL DROP TABLE #tblMonths;
CREATE TABLE #tblMonths (
ID Integer not null identity (1, 1),
Start Datetime not null,
TheCount Integer);

INSERT INTO #tblMonths(Start, TheCount)
SELECT DATEADD(month, -t.N + 1, @dtmBase), 0
FROM Tally1K t
WHERE t.N <= 13
ORDER BY 1 DESC;



You can then update #tblMonths.TheCount from your data table using the dates in your #tblMonths table. If you have the date field indexed, I expect that it would perform acceptably.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
stevenb 14609
stevenb 14609
SSChasing Mays
SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)

Group: General Forum Members
Points: 611 Visits: 627
Another option is to use a tally table.
See http://www.sqlservercentral.com/articles/T-SQL/62867/

and go down to the section dozens of other uses. What you're looking for is in that section.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search