sql-server - 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 |
虽然结果很糟糕,但我尝试了不同的方法,但没有一个更好。
希望我足够清楚。
感谢您的帮助!
解决方案
尝试以下操作:
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 。
推荐阅读
- python - 如何处理 utf-8 字符串,因为我想用我的文本中的空格替换 - 然后使用 split 函数将每个单词与其他单词分开
- javascript - 如何在 vue 中比较 array.include(arrayData)
- javascript - 动态地将对象添加到数组中
- django - 对 Django 查询集的 groupby 操作
- angular - 带有“道具”或函数参数类型错误的 createAction
- javascript - javascript代码在oninput属性中不起作用
- dart - 为什么 Dart 不能在编译时检查数组绑定?
- performance - 如何在 teamcity 中设置超时和活动的多运行构建器
- django - Django 过滤 URL 和 VIEWS - “在 /collection/... 找不到页面”
- java - 我不懂一行编程代码