首页 > 解决方案 > SQL Group By 分区 By

问题描述

这必须在 MS SQL Server 中完成。我相信OVER( PARTITION BY)必须使用,但我所有的尝试都失败了,我最终将记录计数到每个 ID 或其他东西......

我有这张桌子:

| ID   | COLOR  |
+------+--------+
| 1    | Red    |
| 1    | Green  |
| 1    | Blue   |
| 2    | Red    |
| 2    | Green  |
| 2    | Blue   |
| 3    | Red    |
| 3    | Brown  |
| 3    | Orange |

请注意,ID = 1 和 ID = 2 的 COLOR 值完全相同,但 ID = 3 仅共享值 COLOR = Red。

我想将表格分组如下:

| COLOR  | COUNT | GROUPING |
+--------+-------+----------+
| Red    | 2     | Type 1   |
| Green  | 2     | Type 1   |
| Blue   | 2     | Type 1   |
| Red    | 1     | Type 2   |
| Brown  | 1     | Type 2   |
| Orange | 1     | Type 2   |

这意味着 ID = 1 和 ID = 2 共享相同的 3 个颜色值,它们作为类型 1 聚合在一起。虽然 ID = 3 与 ID = 1 和 ID = 2 共享一个颜色值(即“红色” ) 其余值不共享,因此它被认为是类型 2(不同的分组)。

使用的表是简单的示例,足以复制到整个数据集,但是理论上每个 ID 可以有数百条记录,每行中的颜色值不同。但是它们是唯一的,一个 ID 在不同的行中不能具有相同的颜色。

我最好的尝试:

SELECT
    ID, 
    COLOR,
    CONCAT ('TYPE ', COUNT(8) OVER( PARTITION by ID)) AS COLOR_GROUP
FROM 
    {TABLE};

结果:

| ID   | COLOR  | GROUPING |
+------+--------+----------+
| 1    | Green  | Type 3   |
| 1    | Blue   | Type 3   |
| 1    | Red    | Type 3   |
| 2    | Green  | Type 3   |
| 2    | Blue   | Type 3   |
| 2    | Red    | Type 3   |
| 3    | Red    | Type 3   |
| 3    | Brown  | Type 3   |
| 3    | Orange | Type 3   |

虽然结果很糟糕,但我尝试了不同的方法,但没有一个更好。

希望我足够清楚。

感谢您的帮助!

标签: sql-servergroup-bypartition-by

解决方案


尝试以下操作:

declare @t table ( ID  int,COLOR varchar(100))
insert into @t select 1   ,'Red'    
insert into @t select 1   ,'Green'  
insert into @t select 1   ,'Blue'   
insert into @t select 2   ,'Red'    
insert into @t select 2   ,'Green'  
insert into @t select 2   ,'Blue'   
insert into @t select 3   ,'Red'    
insert into @t select 3   ,'Brown'  
insert into @t select 3   ,'Orange'


select *, STUFF((SELECT CHAR(10) + ' '+COLOR
                  FROM @t t_in where t_in.ID=t.ID
                  order by COLOR
                   FOR XML PATH ('')) , 1, 1, '') COLOR_Combined 
into #temp
from @t t

select COLOR, count(color) [COUNT], 'TYPE ' + convert(varchar(10), dense_rank() OVER (order by [grouping])) [GROUPING]
from
(   
    select id, COLOR, COLOR_Combined,  (row_number() over (order by id) - row_number() over (partition by Color_Combined order by id)) [grouping]
    from #temp
)t
group by COLOR, [grouping]
drop table if exists #temp

请在此处找到 db<>fiddle 。


推荐阅读