查询以将单列数据值提取到 8 个定义的列数中
Query to fetch Single column data values into 8 defined number of columns
我有一个只有一列的 table,
看起来像:
NAMEs
-------------------
abc
bcd
cde
def
efg
fgh
ghi
hij
ijk
jkl
klm
我需要帮助以固定 8 columns
的矩阵格式获取这些项目
预计O/P
[1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] |
----|-------|-------|-------|-------|-------|-------|-------|
abc | bcd | cde | def | efg | fgh | ghi | hij |
ijk | jkl | klm | ... | ... | ... | ... | ... |
... | ... | ... | ... | ... | ... | ... | ... |
基本上,我想使用这种格式在SSRS报告中显示,所以如果有任何其他方法可以直接在SSRS中获得相同的输出,也将不胜感激。
使用除法和模运算符并旋转它,你可以这样做,
select [1],[2],[3],[4],[5],[6],[7],[8]
FROM
(select names,
1 + ((row_number() over (order by names) - 1) / 8) as rn1, -- to mark the iteration
1 + ((row_number() over (order by names) - 1) % 8) as rn2 -- to mark the column to be pivoted
from test) tbl
pivot
(
max(names)
for rn2 in ([1],[2],[3],[4],[5],[6],[7],[8])
) piv;
我有一个只有一列的 table,
看起来像:
NAMEs
-------------------
abc
bcd
cde
def
efg
fgh
ghi
hij
ijk
jkl
klm
我需要帮助以固定 8 columns
预计O/P
[1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] |
----|-------|-------|-------|-------|-------|-------|-------|
abc | bcd | cde | def | efg | fgh | ghi | hij |
ijk | jkl | klm | ... | ... | ... | ... | ... |
... | ... | ... | ... | ... | ... | ... | ... |
基本上,我想使用这种格式在SSRS报告中显示,所以如果有任何其他方法可以直接在SSRS中获得相同的输出,也将不胜感激。
使用除法和模运算符并旋转它,你可以这样做,
select [1],[2],[3],[4],[5],[6],[7],[8]
FROM
(select names,
1 + ((row_number() over (order by names) - 1) / 8) as rn1, -- to mark the iteration
1 + ((row_number() over (order by names) - 1) % 8) as rn2 -- to mark the column to be pivoted
from test) tbl
pivot
(
max(names)
for rn2 in ([1],[2],[3],[4],[5],[6],[7],[8])
) piv;