To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

HOME News MS SQL Oracle DB2 Access MySQL PHP Scripts Books Links DBA Talk


Go Back   Database Journal Forums > Related Sites > SQL Course

SQL Course SQL Course > Ask questions about the lessons on SQL Course 1 and 2. If you have problems > with the interface, please post in the Feedback forum

Reply Post New Thread
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-23-2002, 01:51 PM
aprdv aprdv is offline
Junior Member
 
Join Date: Oct 2002
Posts: 11
GROUP BY Clause

Hi All,

I have a SQL statement which gives me groupings of my employees in their age group. I have a DOB column in my table and this is what I want my output to be like .......

AgeGroup EmployeeCount
--------- -------------
0 - 10 yrs 0
11 - 20 yrs 2
21 - 30 yrs 29
31 - 40 yrs 21
41 - 50 yrs 15
51 - 60 yrs 9
61 - 70 yrs 3
> 70 yrs 0

But here is what I am getting .......

AgeGroup EmployeeCount
--------- -------------
11 - 20 yrs 2
21 - 30 yrs 29
31 - 40 yrs 21
41 - 50 yrs 15
51 - 60 yrs 9
61 - 70 yrs 3

I have two rows missing, 0-10 yrs and > 70 yrs age groups. This is happening since I don't have any employees falling into those categories. How do I force my query to display zeros for these categories. Any amount of help will be grately appreciated.

Thanks !
Reply With Quote
  #2  
Old 10-28-2002, 10:24 PM
Stephen Stephen is offline
Registered User
 
Join Date: Sep 2002
Posts: 169
Create a table with the age ranges you want to report on and left join to it

e.g.
SET NOCOUNT ON
CREATE TABLE MyData (Age int)
CREATE TABLE MyAgeRange (LowerAge int,
UpperAge int,
AgeRange VARCHAR (20))
go
INSERT INTO MyData VALUES (1)
INSERT INTO MyData VALUES (11)
INSERT INTO MyData VALUES (12)
INSERT INTO MyData VALUES (12)
INSERT INTO MyData VALUES (31)
INSERT INTO MyData VALUES (32)
INSERT INTO MyData VALUES (31)

INSERT INTO MyAgeRange
VALUES (0, 10, '0 - 10 years')
INSERT INTO MyAgeRange
VALUES (11, 20, '11 - 20 years')
INSERT INTO MyAgeRange
VALUES (21, 30, '21 - 30 years')

INSERT INTO MyAgeRange
VALUES (31, 40, '31 - 40 years')
INSERT INTO MyAgeRange
VALUES (41, 200, 'Over 40')

SELECT AgeRange, Count (MyData.Age)
FROM MyAgeRange
LEFT OUTER JOIN MyData ON MyData.Age BETWEEN MyAgeRange.LowerAge AND MyAgeRange.UpperAge
GROUP BY AgeRange
Reply With Quote
Reply Post New Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:57 PM.


DatabaseJournal Recent Articles


 » Searching and Sorting Strings in Oracle

 » Preparing To Upgrade Access Tables to SQL ...

 » Sun Expands MySQL With Closed Source

 » Microsoft Demos New SQL Server Features at...

 » Wipro and Oracle Launch First Joint Innova...

Search Database Journal:
 





Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.