Solved

Sql Sytnax to fill in missing month with default of zero

Posted on 2012-04-09
11
371 Views
Last Modified: 2012-08-13
I have a sql query that I do for a year and count the number of occurrences of that program for each month. I have January with a count of "1" but nothing for February, but then I have something for March, say '5', and and so on for the full year. I do this for each program.

How do I write the sql syntax to get the 12 months of data with "0" filler for each program.
****************
select year(date_created) as selyear, month(date_created) as selmonth, count(a.program) as cntpgm,
b.program_id
FROM mrb_details a, mrb_program b
where date_created between '01/01/2011' and '12/31/2011'
and a.program = b.program_id
group by year(date_created), month(date_created), b.program_id
order by month(date_created) asc, b.program_id
*******************

Thanks!
sample.pdf
0
Comment
Question by:mahpog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37823564
Which tables are the date fields in?

It would be even more helpful if you could take a screen shot of the top half of the query design grid, with both tables expanded so we can see all of the appropriate fields.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37823744
Please try the following:
select 
    r.selyear, 
    r.selmonth, 
    (select count(1) from mrb_details a
      where a.program = b.program_id
        and a.date_created between '01/01/2011' and '12/31/2011'
        and year(date_created) = r.selyear
        and month(date_created) = r.selmonth) as cntpgm,
    b.program_id
from 
  (select 2011 as selyear, number as selmonth
     from spt_values
    where type = 'P' and number between 1 and 12) r,
  mrb_program b
group by r.selyear, r.selmonth, b.program_id
order by r.selmonth, b.program_id

Open in new window

0
 

Author Comment

by:mahpog
ID: 37824649
okay here is the original query unedited(  I am attaching screenshots of two tables referenced)
thx!
**********************************************************
select year(date_created) as selyear, month(date_created) as selmonth, isnull(count(a.program),0) as cntpgm, b.program
FROM mrb_details a, mrb_program b
where date_created between '01/01/2011' and '12/31/2011'
and a.program = b.program_id
group by year(date_created), month(date_created), b.program
order by month(date_created), b.program
***********************************************************
sample.pdf
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 37824700
Did the query returned the wrong results or did it generate an error?  Please indicate what's missing or the specific error message.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37824706
Try this:

select year(a.date_created) as selyear, month(a.date_created) as selmonth, count(a.program) as cntpgm, b.program
FROM (
SELECT [Date_created], Program
FROM mrb_details
where date_created between '01/01/2011' and '12/31/2011') as a
LEFT JOIN mrb_program b
ON a.program = b.program_id
group by year(date_created), month(date_created), b.program
order by month(date_created), b.program

This first identifies all of the records from table: mrb_details where the [Date_Created] falls within your date range.

It then uses a left join (just in case there are some Program codes in mrb_details that are not in mrb_program
to join to mrb_program, and then counts the number of records by year, month, program.
0
 

Author Comment

by:mahpog
ID: 37824872
wdosanjos - I got an error on spt_values,when I ran.

*********************
fyed - I ran your query and got same result from original query.

***************
I want to output a value for each month in the year for 2011. So, if the pgm has a value for January, I output that value. and again for feb - thru december for 2011. If there is no value for the pgm, I want to output "0".  

For example

pgm     jan  f eb  mar apr may jun jul aug sep oct nov dec

b2         1     0       0      0     3     0     0    3   1    0      0        1

(my current query only returns the pgm with a value for the month)
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37824883
Please try again:
select 
    r.selyear, 
    r.selmonth, 
    (select count(1) from mrb_details a
      where a.program = b.program_id
        and a.date_created between '01/01/2011' and '12/31/2011'
        and year(date_created) = r.selyear
        and month(date_created) = r.selmonth) as cntpgm,
    b.program_id
from 
  (select 2011 as selyear, number as selmonth
     from [master].[dbo].[spt_values]
    where type = 'P' and number between 1 and 12) r,
  mrb_program b
group by r.selyear, r.selmonth, b.program_id
order by r.selmonth, b.program_id

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37824905
Provide us with some sample data and what you want the output to look like

Your explanation and the little information you gave us in the PDF is not enough info.
0
 

Author Comment

by:mahpog
ID: 37825026
wdosanjos - it gave me a full output of all the pgms and counts.  That is what I wanted.

how do I code to get the pgm name to come out as well.  in last sample, the output had the pgm name.

- After this, I need to read up on the steps you took.

thx!
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37825058
This brings the pgm name as well:
select 
    r.selyear, 
    r.selmonth, 
    (select count(1) from mrb_details a
      where a.program = b.program_id
        and a.date_created between '01/01/2011' and '12/31/2011'
        and year(date_created) = r.selyear
        and month(date_created) = r.selmonth) as cntpgm,
    b.program_id,
    b.program
from 
  (select 2011 as selyear, number as selmonth
     from [master].[dbo].[spt_values]
    where type = 'P' and number between 1 and 12) r,
  mrb_program b
group by r.selyear, r.selmonth, b.program_id
order by r.selmonth, b.program_id

Open in new window

0
 

Author Closing Comment

by:mahpog
ID: 37825185
Perfect, and I need to learn to do this!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question