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
 Conditional sum

Author  Topic 

akamole
Starting Member

17 Posts

Posted - 2014-02-24 : 10:09:20
Table with water consumtion per month and customer.
I want to sum up total consumption per customer with a select statement

maggie21620
Starting Member

27 Posts

Posted - 2014-02-24 : 11:06:32
i am looking for the same type of function let me know what you get please.

none
Go to Top of Page

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-02-24 : 11:49:48
SELECT Customer, SUM(Consumption) AS Total_Consumption
FROM Table_Name
GROUP BY Customer
ORDER BY Customer

=============================================================
If you are using SQL SERVER 2012, then you can also do:
SELECT Customer,
SUM(Consumption) OVER(Partition by Customer ORDER BY Customer) AS Total_Consumption
FROM Table_Name


I hope this helps.

!!_(M)_!!
Go to Top of Page

akamole
Starting Member

17 Posts

Posted - 2014-02-25 : 01:54:14
SELECT Customer, SUM(Consumption) AS Total_Consumption
FROM Table_Name
GROUP BY Customer
ORDER BY Customer

This worked great, thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-27 : 02:55:56
quote:
Originally posted by maunishq

SELECT Customer, SUM(Consumption) AS Total_Consumption
FROM Table_Name
GROUP BY Customer
ORDER BY Customer

=============================================================
If you are using SQL SERVER 2012, then you can also do:
SELECT Customer,
SUM(Consumption) OVER(Partition by Customer ORDER BY Customer) AS Total_Consumption
FROM Table_Name


I hope this helps.

!!_(M)_!!


You can use it for all versions from sql 2005 with a change


SELECT DISTINCT Customer,
SUM(Consumption) OVER(Partition by Customer) AS Total_Consumption
FROM Table_Name


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

- Advertisement -