I am fairly new to SQL and I am stuck on a query. I have the following table called Appointment
NAME SERVICE DATE
-----------------------------
John Abc 2012-11-10
Michael Xyz 2012-11-10
Denis Xyz 2012-11-10
Denis Pqr 2012-11-06
John Abc 2012-11-06
Michael Abc 2012-11-05
Sofy Abc 2012-11-04
John Pqr 2012-11-01
Michael Pqr 2012-11-01
Michael Abc 2012-10-21
John Abc 2012-10-23
John HFD 2012-10-23
John BHU 2012-11-09
Michael KSD 2012-11-08
What I want to do is count the services for individuals between the dates 2012-11-01 and 2012-11-10.
Following is the required output
NAME Abc Pqr Xyz
--------------------------------
John 2 1 0
Michael 1 1 1
Denis 0 1 1
Sofy 1 0 0
This is what I’ve tried:
SELECT Name, COUNT(Distinct Service)
FROM [Appointment]
WHERE [DATE] between '2012-11-01 00:00:00.000' and '2012-11-10 23:59:59.999'
AND (Service = 'Abc' or Service = 'Pqr' or Service = 'Xyz')
GROUP BY Name
Thank you in advance.
or