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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Count and group

Author  Topic 

moorzee
Starting Member

5 Posts

Posted - 2013-04-18 : 04:02:27
Hi all

I have a structure along the lines of Client, Area, Appointment.

I need to produce a report giving counts of appointments by type, age of client etc... for each area. A client is linked to an area by foreign key.

I can get the overall counts/totals but not by area.

I'm struggling to get the counts grouping by area name. can anyone point me to an example of this type of query?

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 04:52:25
just add a group by based Area field and apply your aggregation over required fields like in your case COUNT(ApointmentID) gives count of appointments etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

moorzee
Starting Member

5 Posts

Posted - 2013-04-18 : 05:55:52
But I need to return many different counts of differing criteria so I have sub selects as follows (not at work machine so from memory): SELECT (SELECT COUNT(1) FROM Appointment A INNER JOIN Client C ON C.ClientId = A.ClientId INNER JOIN Area A1 ON A1.AreaId = C.AreaId WHERE C.age BETWEEN 0 AND 15) AS BirthTo16, (SELECT COUNT(1) FROM Appointment A INNER JOIN Client C ON C.ClientId = A.ClientId INNER JOIN Area A1 ON A1.AreaId = C.AreaId WHERE C.age BETWEEN 16 AND 19) AS Age16To19

How can I return the area name and the counts here?
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-18 : 06:07:49
--May be this?
SELECT
COUNT(CASE WHEN c.age BETWEEN 0 AND 15 THEN AppointmentID END) AS BirthTo16,
COUNT(CASE WHEN c.age BETWEEN 16 AND 19 THEN AppointmentID END) AS Age16To19
FROM Appointment A
INNER JOIN Client C ON C.ClientId = A.ClientId
INNER JOIN Area A1 ON A1.AreaId = C.AreaId
--GROUP BY type


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 06:37:17
quote:
Originally posted by moorzee

But I need to return many different counts of differing criteria so I have sub selects as follows (not at work machine so from memory): SELECT (SELECT COUNT(1) FROM Appointment A INNER JOIN Client C ON C.ClientId = A.ClientId INNER JOIN Area A1 ON A1.AreaId = C.AreaId WHERE C.age BETWEEN 0 AND 15) AS BirthTo16, (SELECT COUNT(1) FROM Appointment A INNER JOIN Client C ON C.ClientId = A.ClientId INNER JOIN Area A1 ON A1.AreaId = C.AreaId WHERE C.age BETWEEN 16 AND 19) AS Age16To19

How can I return the area name and the counts here?


show your sample data
and explain counts you want
otherwise we cant make out what you're expecting

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

moorzee
Starting Member

5 Posts

Posted - 2013-04-18 : 14:56:16
quote:
Originally posted by bandi

--May be this?
SELECT
COUNT(CASE WHEN c.age BETWEEN 0 AND 15 THEN AppointmentID END) AS BirthTo16,
COUNT(CASE WHEN c.age BETWEEN 16 AND 19 THEN AppointmentID END) AS Age16To19
FROM Appointment A
INNER JOIN Client C ON C.ClientId = A.ClientId
INNER JOIN Area A1 ON A1.AreaId = C.AreaId
--GROUP BY type
Chandu


Just what I needed bandi. Thanks a lot.....
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-19 : 00:27:30
quote:
Originally posted by moorzee
Just what I needed bandi. Thanks a lot.....


Welcome

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-19 : 02:24:35
quote:
Originally posted by moorzee

quote:
Originally posted by bandi

--May be this?
SELECT
COUNT(CASE WHEN c.age BETWEEN 0 AND 15 THEN AppointmentID END) AS BirthTo16,
COUNT(CASE WHEN c.age BETWEEN 16 AND 19 THEN AppointmentID END) AS Age16To19
FROM Appointment A
INNER JOIN Client C ON C.ClientId = A.ClientId
INNER JOIN Area A1 ON A1.AreaId = C.AreaId
--GROUP BY type
Chandu


Just what I needed bandi. Thanks a lot.....


In that case what you could do is to define the age range values in another table so that you can redefine range values add or remove as you like without changing the above logic code which will add more flexibility to the solution

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -