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)
 SUMIF

Author  Topic 

patrickjao
Starting Member

24 Posts

Posted - 2012-08-05 : 05:14:14
I have table as follow:

Type Qty Price
ABCD 100 20000
BCDE 200 10000
DEFG 500 30000
ABCD 200 40000
DEFG 600 50000

HOW TO CREATE THE SUMIF COLUMN AS FOLLOW :
Type Qty Price TQty Tprice
ABCD 100 20000 300 60000
ABCD 200 40000 300 60000
BCDE 200 10000 200 10000
DEFG 500 30000 1100 80000
DEFG 600 50000 1100 80000

Thanks


lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-08-05 : 07:17:38
CREATE TABLE #SumIf (TType VARCHAR(10) ,Qty INT, Price INT)

INSERT INTO #SumIf
SELECT 'ABCD', 100, 20000 UNION ALL
SELECT 'BCDE', 200, 10000 UNION ALL
SELECT 'DEFG', 500, 30000 UNION ALL
SELECT 'ABCD', 200, 40000 UNION ALL
SELECT 'DEFG', 600, 50000


SELECT TType ,Qty, Price,
SUM(Qty) OVER (Partition By TType) QtySum ,
SUM(Price) OVER (Partition By TType) PriceSum
FROM #SumIf

DROP TABLE #SumIf

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page
   

- Advertisement -