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
 General SQL Server Forums
 New to SQL Server Programming
 Conditional sum
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

akamole
Starting Member

17 Posts

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

maggie21620
Starting Member

USA
27 Posts

Posted - 02/24/2014 :  11:06:32  Show Profile  Reply with Quote
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

Canada
71 Posts

Posted - 02/24/2014 :  11:49:48  Show Profile  Reply with Quote
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 - 02/25/2014 :  01:54:14  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 02/27/2014 :  02:55:56  Show Profile  Reply with Quote
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
  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.08 seconds. Powered By: Snitz Forums 2000