Solved

Sql Sytnax to fill in missing month with default of zero

Posted on 2012-04-09
11
372 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 48

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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 48

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 48

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

717 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