gpt4 book ai didi

sql - 选择对行进行分组

转载 作者:行者123 更新时间:2023-12-04 07:07:49 25 4
gpt4 key购买 nike

我有一张这样的 table :

Alternative |Total  |Male  |Female
a |20 |10 |10
b |50 |20 |30
c |40 |10 |30
c |30 |15 |15

现在我想选择要分组的所有行和“c”替代项。

最佳答案

以下查询将选择所有不是“c”的行,然后将所有“c”行分组,对它们的值求和,并添加代表“c”总和的一行。我希望这就是你的意思。

SELECT *
FROM table1
WHERE Alternative != c

UNION

SELECT Alternative, Sum(Total) as Total, Sum(Male) as Male, Sum(Female) as Female
FROM table1
WHERE Alternative = c
GROUP BY Alternative

如果您只想对具有多个“替代”外观的任何内容进行分组,只需使用:
SELECT Alternative, Sum(Total) as Total, Sum(Male) as Male, Sum(Female) as Female
FROM table1
GROUP BY Alternative

关于sql - 选择对行进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/862678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com