我想要从我的表中组合一组重复项,但不是全部。
示例:
acct date bal
--------------------
123 1/1/2013 40.00
123 1/1/2013 2.00
456 1/2/2013 50.00
456 1/1/2013 5.00
789 1/1/2013 10.00
789 1/1/2013 17.00
我想将acct 123合并到一行,总结这些行的余额,但剩下的就是。
期望的输出:
acct date bal
--------------------
123 1/1/2013 42.00
456 1/2/2013 50.00
456 1/1/2013 5.00
789 1/1/2013 10.00
789 1/1/2013 17.00
在SQL Server 2005中工作。
答案 0 :(得分:3)
SELECT acct, date, SUM(bal)
FROM T
WHERE acct = 123
UNION
SELECT acct, date, bal
FROM T
WHERE acct <> 123
答案 1 :(得分:3)
在GROUP BY子句中使用CASE
SELECT acct, date, SUM(bal) AS bal
FROM dbo.tets73
GROUP BY acct, date, CASE WHEN acct != 123 THEN bal END
SQLFiddle上的演示
答案 2 :(得分:2)
select acct, date, sum(bal) from table where acct = 123
union
select acct, date bal from table where acct <> 123