Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 *SOLVED* Variance of 2 summed values
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

davidais
Starting Member

17 Posts

Posted - 10/04/2012 :  10:54:58  Show Profile  Reply with Quote
Hi all,

How would I display in a new column the difference of [Value2012] - [Value2011] and exclude any instances where [Value2012] - [Value2011] = 0

SELECT
	tbCustomers.AccountNumber, tbCustomers.CustomerName,
	SUM(CASE WHEN InvoiceDate BETWEEN '2011-01-01' AND '2011-07-31' THEN InvoiceValue ELSE 0 END) AS Value2011,
	SUM(CASE WHEN InvoiceDate BETWEEN '2012-01-01' AND '2012-07-31' THEN InvoiceValue ELSE 0 END) AS Value2012

FROM 
	tbSalesHistory
	JOIN tbCustomers ON tbSalesHistory.AccountNumber = tbCustomers.AccountNumber

GROUP BY
	tbCustomers.AccountNumber, tbCustomers.CustomerName

Many thanks, D

Edited by - davidais on 10/04/2012 11:14:40

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 10/04/2012 :  11:05:26  Show Profile  Reply with Quote

SELECT
	tbCustomers.AccountNumber, tbCustomers.CustomerName,
	SUM(CASE WHEN InvoiceDate BETWEEN '2011-01-01' AND '2011-07-31' THEN -1 * InvoiceValue 
	WHEN InvoiceDate BETWEEN '2012-01-01' AND '2012-07-31' THEN InvoiceValue ELSE 0 END) AS [Value2012-Value2011]

FROM 
	tbSalesHistory
	JOIN tbCustomers ON tbSalesHistory.AccountNumber = tbCustomers.AccountNumber

GROUP BY
	tbCustomers.AccountNumber, tbCustomers.CustomerName
HAVING SUM(CASE WHEN InvoiceDate BETWEEN '2011-01-01' AND '2011-07-31' THEN -1 * InvoiceValue 
	WHEN InvoiceDate BETWEEN '2012-01-01' AND '2012-07-31' THEN InvoiceValue ELSE 0 END)>0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

davidais
Starting Member

17 Posts

Posted - 10/04/2012 :  11:14:07  Show Profile  Reply with Quote
Awesome
Go to Top of Page

davidais
Starting Member

17 Posts

Posted - 10/04/2012 :  11:35:13  Show Profile  Reply with Quote
out of curiosity what does the [ -1 * ] part of the expression do?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 10/04/2012 :  11:53:00  Show Profile  Reply with Quote
quote:
Originally posted by davidais

out of curiosity what does the [ -1 * ] part of the expression do?


it negates the values for last year inside SUM to give variance from current year ie 2012

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next 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.07 seconds. Powered By: Snitz Forums 2000