Author |
Topic |
allanurban
Starting Member
21 Posts |
Posted - 2013-06-07 : 06:21:55
|
Hello allI have a table which contains 'productId', 'way' and 'quanitity'way indicates if I have bought ('B') or sold ('S') the product and quantity is only positive integers. How can I get the net change from this table?I can get the seperate results and could just do it manually, but would really like this in a single query.SELECT productId, way, SUM(quantity)FROM tableWHERE way = 'B'GROUP BY productId, wayAnyone who can push me in the right direction? |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2013-06-07 : 06:36:37
|
something like thisselect productid, sum(case when way = 'B' then 0 - quantity else quantity end) as Qtyfrom tablegroup by productid Duane. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-07 : 06:44:27
|
[code]SELECT productId,[B] AS Bought,[S] AS Sold,[B]-[S] AS NetFROM (SELECT productId, way, quantity AS QtyFROM table)tPIVOT(SUM(Qty) FOR way in ([B],[S]))p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-07 : 06:44:28
|
quote: Originally posted by ditch something like thisselect productid, sum(case when way = 'S' then 0 - quantity else quantity end) as Qtyfrom tablegroup by productid Duane.
CheersMIK |
 |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2013-06-07 : 06:48:44
|
Hi MikThat depends if you are talking about Stock Holding Or sales..... I don't believe it was specified.Duane. |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-07 : 06:58:33
|
Absolutely agree, it was not specified. Perhaps I should have given a narrative too. Actually I was going to suggest same query as yours but you posted before me :) and that instead of reposting it I just took yours and repalce 'B' with 'S' Since that is how I perceived it.CheersMIK |
 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-06-07 : 07:03:11
|
quote: Originally posted by visakh16
SELECT productId,[B] AS Bought,[S] AS Sold,[B]-[S] AS NetFROM (SELECT productId, way, SUM(quantity) AS QtyFROM table)tPIVOT(SUM(Qty) FOR way in ([B],[S]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Visakh, seems you missed to add group by :) SELECT id,[B] AS Bought,[S] AS Sold FROM (SELECT id, way, sum(quantity) AS Qty FROM @tab group by id,way )t PIVOT(SUM(Qty) FOR way in ([B],[S]))pCheersMIK |
 |
|
allanurban
Starting Member
21 Posts |
Posted - 2013-06-07 : 07:10:30
|
Thank you for the quick help everyone.Much appriciated! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-07 : 07:31:06
|
quote: Originally posted by MIK_2008
quote: Originally posted by visakh16
SELECT productId,[B] AS Bought,[S] AS Sold,[B]-[S] AS NetFROM (SELECT productId, way, SUM(quantity) AS QtyFROM table)tPIVOT(SUM(Qty) FOR way in ([B],[S]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Visakh, seems you missed to add group by :) SELECT id,[B] AS Bought,[S] AS Sold FROM (SELECT id, way, sum(quantity) AS Qty FROM @tab group by id,way )t PIVOT(SUM(Qty) FOR way in ([B],[S]))pCheersMIK
I think you didnt refresh I edited it before you posted this------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2013-06-07 : 08:04:16
|
Nice solution Vikash.Duane. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-07 : 08:12:54
|
quote: Originally posted by ditch Nice solution Vikash.Duane.
thanks But for the given scenario I think your approach is better. There's no need for OP to crosstab if the intention is just to get the net value.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|