Sql 多个选择的具体位置
我有以下问题: 我有一张像这样的桌子:Sql 多个选择的具体位置,sql,select,Sql,Select,我有以下问题: 我有一张像这样的桌子: 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 现在我试图得到这样的结果: 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组的所有价格加在一列,把其他所有价
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
现在我试图得到这样的结果:
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'
有没有办法实现此功能?请尝试:
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
select
sum(case when [Group]='A' then Price end) PriceA,
sum(case when [Group]<>'A' then Price end) PriceRest
from
Table
您只需要两个子查询:
SELECT (SELECT SUM(PRICE)
FROM Table1
WHERE [Group] ='a') AS PriceGroupA,
(SELECT SUM(PRICE)
FROM Table1
WHERE [Group]<>'a') AS PriceRest
不显示仅包含两列的单个组。不显示仅包含两列的单个组。这正是我所需要的。谢谢