gpt4 book ai didi

sql - 如何在sql中对同类行进行分组

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

我有一个像这样的表:

    Table1
---------------------
Id |Kind1|Kind2|Kind3|
---------------------
1 | 2 | 2 | 3 | *
---------------------
2 | 2 | 2 | 2 | +
---------------------
3 | 2 | 2 | 1 |
---------------------
4 | 2 | 2 | 3 | *
---------------------
5 | 2 | 1 | 1 |
---------------------
6 | 1 | 1 | 1 |
---------------------
7 | 2 | 2 | 2 | +
----------------------

我想得到如下所示的结果 ID 必须自动生成我必须计算同一行组的数量

Result
------------------------------
Id | Count |New1 |New2 |New3 |
------------------------------
1 | 2 | 2 | 2 | 2 |
------------------------------
2 | 2 | 2 | 2 | 3 |
------------------------------
3 | 1 | 2 | 2 | 1 |
------------------------------
4 | 1 | 2 | 1 | 1 |
------------------------------
5 | 1 | 1 | 1 | 1 |
------------------------------

我如何在 sql server 中执行此操作?

最佳答案

select ROW_NUMBER() over (order by (select 0)) as ID,count(*) as count,kind1 as new1,kind2 as new2,kind3 as new3
from yourtable
group by kind1,kind2,kind3

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

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