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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help with query

Author  Topic 

joe8079
Posting Yak Master

127 Posts

Posted - 2012-01-25 : 16:23:03





Hello,

I hope this formats correctly, but I'm having some issues creating a new column that calculates the Amount difference between
Jan 1, Product A, Description A
and
Jan 2, Product A, Description A
For this example, it would display 34345 - 8798 = 25547.
I know this involves a sub query in the select statement, but I cannot figure out to do this. I hope this example makes sense.

Code:
Select date, product, product_descr, sum(amount) as amount
From table1
Group by Date, Product, Product_descr

Results Sample

Date Product Product_Descr Amount
Jan 1 A Description A 34345
Jan 1 A Description B 4658
Jan 1 A Description C 859
Jan 1 A Description D 6546
Jan 2 A Description A 8798
Jan 2 A Description B 3245
Jan 2 A Description C 2222
Jan 2 A Description D 5688
   

- Advertisement -