SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Count and group
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

moorzee
Starting Member

United Kingdom
5 Posts

Posted - 04/18/2013 :  04:02:27  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 04/18/2013 :  04:52:25  Show Profile  Reply with Quote
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

United Kingdom
5 Posts

Posted - 04/18/2013 :  05:55:52  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/18/2013 :  06:07:49  Show Profile  Reply with Quote
--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

India
52325 Posts

Posted - 04/18/2013 :  06:37:17  Show Profile  Reply with Quote
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

United Kingdom
5 Posts

Posted - 04/18/2013 :  14:56:16  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/19/2013 :  00:27:30  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 04/19/2013 :  02:24:35  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000