根据索引范围将一列变成多列

Turn one column into multiple based on index ranges

我在 SQL 服务器中有以下 table:

| idx | value |
| --- | ----- |
| 1   | N     |
| 2   | C     |
| 3   | C     |
| 4   | P     |
| 5   | N     |
| 6   | N     |
| 7   | C     |
| 8   | N     |
| 9   | P     |

我想把它变成这样:

| idx 1-3 | idx 4-6 | idx 7-9 |
| ------- | ------- | ------- |
| N       | P       | C       |
| C       | N       | N       |
| C       | N       | P       |

我该怎么做?

您可以使用横向连接计算每行的类别,然后枚举每个类别中的行,最后使用条件聚合进行透视:

select 
    max(case when cat = 'idx_1_3' then value end) as idx_1_3,
    max(case when cat = 'idx_4_6' then value end) as idx_4_6,
    max(case when cat = 'idx_7_9' then value end) as idx_7_9
from (
    select t.*, row_number() over(partition by v.cat) as rn
    from mytable t
    cross apply (values (
        case 
            when idx between 1 and 3 then 'idx_1_3'
            when idx between 4 and 6 then 'idx_4_6'
            when idx between 7 and 9 then 'idx_7_9'
        end
    )) v(cat)
) t
group by rn

您可以按如下方式使用mod:

select max(case when idx between 1 and 3 then value end) as idx_1_3,
       max(case when idx between 4 and 6 then value end) as idx_4_6,
       max(case when idx between 7 and 9 then value end) as idx_7_9
  from t
group by (idx-1) % 3;

如果您的 idx 不是连续数字,那么请使用以下

而不是 from t
from (select value, row_number() over(order by idx) as idx
   from your_table t) t

联合所有运算符和row_number函数的另一种解决方案

select max(IDX_1_3) as IDX_1_3,  max(IDX_4_6) as IDX_4_6,  max(IDX_1_3) as IDX_1_3
from (
select 
case when idx in (1, 2, 3) then value end as idx_1_3
, null as idx_4_6
, null as idx_7_9
, row_number()over(order by idx) as rnb 
from Your_table where idx in (1, 2, 3) 
union all
select null  as idx_1_3
, case when idx in (4, 5, 6) then value end as idx_4_6
, null  as idx_7_9
, row_number()over(order by idx) as rnb
from Your_table where idx in (4, 5, 6) 
union all
select null as idx_1_3
, null as idx_4_6
, case when idx in (7, 8, 9) then value end as idx_7_9
, row_number()over(order by idx) as rnb 
from Your_table where idx in (7, 8, 9)
) t
group by rnb
;
drop table if exists #t;

create table #t (id int identity(1,1) primary key clustered, val varchar(20));

insert into #t(val)
select top (2002) concat(row_number() over(order by @@spid), ' - ', char(65 + abs(checksum(newid()))%26))
from sys.all_objects
order by row_number() over(order by @@spid);


select p.r, 1+(p.r-1)/3 grp3id, p.[1] as [idx 1-3], p.[2] as [idx 4-6], p.[3] as [idx 7-9]
from
(
    select 
        val, 
        1+((1+(id-1)/3)-1)%3 as c3,
        row_number() over(partition by 1+((1+(id-1)/3)-1)%3 order by id) as r
    from #t
) as src
pivot
(
max(val) for c3 in ([1], [2], [3])
) as p
order by p.r;

如果您想将数据分成三列,数据按 ID 排序——并假设 ID 从 1 开始并且没有间隔——那么对于您的特定数据,您可以使用:

select max(case when (idx - 1) / 3 = 0 then value end) as grp_1,
       max(case when (idx - 1) / 3 = 1 then value end) as grp_2,
       max(case when (idx - 1) / 3 = 2 then value end) as grp_3
from t
group by idx % 3
order by min(idx);

以上并未对范围进行硬编码,但“3”在不同的上下文中表示不同的含义——有时是列数,有时是结果集中的行数。

但是,下面的内容进行了概括,因此它会根据需要添加额外的行:

select max(case when (idx - 1) / num_rows = 0 then idx end) as grp_1,
       max(case when (idx - 1) / num_rows = 1 then idx end) as grp_2,
       max(case when (idx - 1) / num_rows = 2 then idx end) as grp_3
from (select t.*, convert(int, ceiling(count(*) over () / 3.0)) as num_rows
      from t
     ) t
group by idx % num_rows
order by min(idx);

Here 是一个 db<>fiddle.