我有以下问题:
我有一张看起来像这样的表:
ArticleID|Group|Price
1|a|10
2|b|2
3|a|3
4|b|5
5|c|5
6|f|7
7|c|8
8|x|3
现在我试图得到这样的结果:
PriceA|PriceRest
13|30
意思是我想要汇总一列中a组的所有价格和另一列中其他所有的价格。
像这样的东西不起作用。
select
sum(Price) as PriceGroupA
sum(Price) as PriceRest
from
Table
where
Group='a'
Group<>'a'
有没有办法实现这个功能?
答案 0 :(得分:2)
SELECT
sum(case when [Group] = 'a' then Price else 0 end) as PriceA,
sum(case when [Group] <> 'a' then Price else 0 end) as PriceRest
from
Table
答案 1 :(得分:1)
请尝试:
select
sum(case when [Group]='A' then Price end) PriceA,
sum(case when [Group]<>'A' then Price end) PriceRest
from
Table
答案 2 :(得分:0)
您只需要两个子查询:
SELECT (SELECT SUM(PRICE)
FROM Table1
WHERE [Group] ='a') AS PriceGroupA,
(SELECT SUM(PRICE)
FROM Table1
WHERE [Group]<>'a') AS PriceRest