Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Counting Age

Author  Topic 

Shelly1
Starting Member

25 Posts

Posted - 2009-05-06 : 06:23:19
I would like to count customer ages beetween 20 and 30, I can only work out how to search for 20+

ELECT CustomerAge, COUNT(*) TotalCount
FROM THIST
Where IncDate between '1 may 09' and '2 may 09'
GROUP BY PatientAge
HAVING COUNT(*) >20
ORDER BY COUNT(*) DESC

Can anyone help!
Thanks

Mich

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-06 : 06:31:49
Please post your table DDL with sample data and the expected result.

The query you posted is confusing. You have CustomerAge and PatientAge which is the correct column


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-05-06 : 06:33:12
I can't see your query working as posted.

You are grouping by patientAge but then referencing CustomerAge in the SELECT without an aggregate function.

Post some sample data and table structure.

You'll get a nice fast statement from someone in 5 mins if you do this.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

malaytech2008
Yak Posting Veteran

95 Posts

Posted - 2009-05-06 : 06:34:48

SELECT CustomerAge, COUNT(*) TotalCount
FROM THIST
GROUP BY PatientAge
HAVING IncDate between '1 may 09' and '2 may 09'
ORDER BY COUNT(*) DESC

Malay
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-05-06 : 06:34:52
having count(*)>20 filters out those customerages where count of records is greater than 20. Do you want to filter out basis count of customers, or customerage ?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-05-06 : 06:39:03
quote:
Originally posted by malaytech2008


SELECT CustomerAge, COUNT(*) TotalCount
FROM THIST
GROUP BY PatientAge
HAVING IncDate between '1 may 09' and '2 may 09'
ORDER BY COUNT(*) DESC

Malay



Why did you post this?

Event with the S in SELECT it's still wrong.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

malaytech2008
Yak Posting Veteran

95 Posts

Posted - 2009-05-06 : 06:39:57
SELECT CustomerAge, COUNT(*) TotalCount
FROM THIST
GROUP BY PatientAge
HAVING PatientAge between 20 and 30
ORDER BY COUNT(*) DESC


Malay
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-06 : 06:50:51
[code]
SELECT PatientAge, COUNT(*) TotalCount
FROM THIST
WHERE PatientAge BETWEEN 20 AND 30
GROUP BY PatientAge
ORDER BY COUNT(*) DESC
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Shelly1
Starting Member

25 Posts

Posted - 2009-05-06 : 09:09:19
Hi Sorry for the confusing syntax, the last post worked - thanks very much.

Mich
Go to Top of Page
   

- Advertisement -