Select 不同的值将一列分成多列
Select distinct values one column into multiple columns
我有以下数据:第 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 列)的值不超过七个。 tb1 第 2 列中的所有值都不同并且大约有 + 50 条记录。
您想要旋转您的 table,但您的 table 当前不包含您想要旋转的字段("col1"、"col2"、"col3",等等...)。您需要一个行号,由 col1 分区。 Jet 数据库不提供 ROW_NUMBER
函数,因此您必须通过将 table 连接到自身来伪造它:
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
现在您可以在 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
我有以下数据:第 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 列)的值不超过七个。 tb1 第 2 列中的所有值都不同并且大约有 + 50 条记录。
您想要旋转您的 table,但您的 table 当前不包含您想要旋转的字段("col1"、"col2"、"col3",等等...)。您需要一个行号,由 col1 分区。 Jet 数据库不提供 ROW_NUMBER
函数,因此您必须通过将 table 连接到自身来伪造它:
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
现在您可以在 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