将单列值分为三个列值的报表

时间:2018-08-10 09:02:44

标签: sql reporting-services common-table-expression sql-query-store

例如

我在下面的场景中可以有人建议吗 当前方案

Col1 
-----
Test1
Test2
test3  
Test4
Test5
Test6
test7
Test8
Test9
Test10
test11
Test12

预期情况

Col1   Col2   Col3
-------------------
Test1  Test2  Test3         
Test4  Test5  Test6
test7  Test8  Test9
Test10 Test11 test12

... 继续

enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎想要这样的东西:

select max(case when seqnum % 3 = 0 then col end) as col1,
       max(case when seqnum % 3 = 1 then col end) as col2,
       max(case when seqnum % 3 = 2 then col end) as col3   
from (select t.*,
             row_number() over (order by col) - 1 as seqnum
      from t
     ) t
group by floor(seqnum / 3)
order by min(seqnum);

这将添加一个序列号,然后使用算术和group by减少行数。