我有以下数据:第1列包含许多类别,第2列包含每个类别的值。我需要转换或透视此信息,以显示多列中类别组的每个值。
col1 col2
----------------
1 a
2 b
2 c
2 d
3 e
3 f
4 g
4 h
需要这个结果:
col1 col2 col3 col4 col5 col6
-----------------------------------------------
1 a
2 b c d
3 e f
4 g h
每个tb1计数(第2列)组(第1列)的值不超过7个。来自tb1第2列的所有值都不同,约为+ 50条记录。
答案 0 :(得分:1)
You want to pivot your table, but your table doesn't currently contain the field that you want to pivot on ("col1", "col2", "col3", etc...). You need a row number, partitioned by col1. The Jet database does not provide a ROW_NUMBER
function, so you have to fake it by joining the table to itself:
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
Now you can pivot on row_num
:
transform Min(x.col2) select x.col1
from(
select t1.col1, t1.col2, count(*) as row_num
from [Sheet1$] t1
inner join [Sheet1$] t2 on t2.col1 = t1.col1 and t2.col2 <= t1.col2
group by t1.col1, t1.col2
) x
group by x.col1
pivot x.row_num