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
 SQL Query using SUM and GROUP by

Author  Topic 

potn1
Starting Member

33 Posts

Posted - 2011-04-26 : 00:54:00
I have a table in my database and I am extracting a query to display the person_id, appointment_date, amount_of_records. A person can have multiple appointment_dates per month and at each of these appointments the number_of_records varies. Basically I am looking for a sql statement where I can find the total amount_of_records per person_id for any given month.

For example one person has 3 appointment_dates in the month of June. At these appointments they have 5, 10 and 15 records. Basically what I am looking for is a way to display in a table the total records per person which in this case would be 30.

I believe I have to use some sort of aggregate function with a SUM and GROUP BY. Anyone have any suggestions? I’m not sure if this total should also just be displayed in another column?

This statement below totals of the amount of records per person. Is there a way I can include a set of dates to get the total per records for a specific month per person?

SELECT person_id, SUM(amount_of_records)
FROM person
GROUP BY amount_of_records

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-26 : 01:39:25
SELECT person_id, YEAR(DateTimeColumn), MONTH(DateTimeColumn), SUM(amount_of_records)
FROM person
GROUP BY person_id, YEAR(DateTimeColumn), MONTH(DateTimeColumn)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

potn1
Starting Member

33 Posts

Posted - 2011-04-26 : 11:12:32
That is exactly what I was looking for, thanks a bunch!

This can be marked as answered if needed.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-26 : 12:39:33
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -