日期:2014-05-17 浏览次数:20338 次
declare @a table(id INT, A_id INT, type VARCHAR(20), amount INT, price INT)
insert @a select 11 , 1 ,'x', 10 , 10
union all select 12 , 1 ,'x', 10 , 10
union all select 13 , 1 ,'y', 10 , 10
union all select 14 , 2 ,'y', 10 , 10
SELECT A_id, SUM(CASE WHEN TYPE='x' THEN amount ELSE 0 END),SUM(amount*price)
FROM @a
GROUP BY A_id
--result
/*A_id
----------- ----------- -----------
1 20 300
2 0 100
(所影响的行数为 2 行)
*/