473,289 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Select data from table an return all all months

I have the following query:

SELECT Month, Sum(Hits) AS Hits FROM tblHits GROUP BY Month ORDER BY
Month

Unfortunately it only returns rows for months that have data assigned
to them.

How can I tweak this so that months 1-12 are returned, and Hits = 0 for
months with no data in the base table?

Thanks.

Jul 23 '05 #1
2 7905
Try this:

SELECT Month, COALESCE(SUM(Hits),0) AS Hits
FROM (
SELECT 1 AS Month
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
UNION ALL SELECT 10
UNION ALL SELECT 11
UNION ALL SELECT 12
) T1
LEFT JOIN Hits
ON Hits.Month=T1.Month
GROUP BY T1.Month
ORDER BY T1.Month

The query is a bit easier if you use a numbers table instead of the
current subquery.

Gert-Jan
Chris Becker wrote:

I have the following query:

SELECT Month, Sum(Hits) AS Hits FROM tblHits GROUP BY Month ORDER BY
Month

Unfortunately it only returns rows for months that have data assigned
to them.

How can I tweak this so that months 1-12 are returned, and Hits = 0 for
months with no data in the base table?

Thanks.

Jul 23 '05 #2
Chris Becker (sl*****@gmail.com) writes:
I have the following query:

SELECT Month, Sum(Hits) AS Hits FROM tblHits GROUP BY Month ORDER BY
Month

Unfortunately it only returns rows for months that have data assigned
to them.

How can I tweak this so that months 1-12 are returned, and Hits = 0 for
months with no data in the base table?


SELECT a.Month, SUM(h.Hits)
FROM (SELECT 1 UNION SELECT 2 SELECT 3 UNION
SELECT 4 UNION SELECT 5 SELECT 6 UNION
SELECT 7 UNION SELECT 8 SELECT 9 UNION
SELECT 10 UNION SELECT 11 SELECT 12) a
LEFT JOIN tblHits h ON a.Month = h.Month
GROUP BY a.Month
ORDER BY a.Month

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JohnnyOnTheSpot | last post by:
If I select from a table and, for example, group by the month or day to determine how much activity there is day to day or month to month, it will only return months and days with a record and...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
2
by: Jeff Blee | last post by:
I am hoping someone can help me. I am making a Access 97 app for a person and have run up against a problem to do with MS Graph. There is a table that has a number of data elements and a date field...
6
by: A_M_IS | last post by:
Hello group, hope to anybodys help on my temporary blackout. (Using Access 2003 on XP Win.) I know how to create and edit temporary query recordset, then I can set this data source as my form...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
28
by: Rickster66 | last post by:
This thread is similar to the thread (Select only 10 columns going back) that I posted regarding the dynamic selection of data going back one month at a time for 12 months. Here is the qiote fron...
22
by: Rickster66 | last post by:
As Instructed this is a new thread regarding my original post: "Select Only 10 Columns Going Back" I'm sorry for the late response. I've been gathering up information and carefully with as much...
11
by: jglabas | last post by:
I have a MS Access query that calculates a member’s attendance at team events over the past 12-months (i.e., Now()-365). This is one of several queries used; others calculate the total number of...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.