会不会出现这样的情况:只将第一列的数据带到第一行,而第二行是第二列的数据



我有5列(a列、b列、c列、d列、e列(

会不会出现这样的情况:只将第一列的数据带到第一行,而第二行是第二列的数据?

这种情况我该怎么办?

编辑v2:

对不起,我不能正确解释。有时c列可以重复。有一列(e列(我想对我的结果进行分组,该列的数据像1-2-3-4一样:

  1. abc---1
  2. --abc---1
  3. ----abc--1
  4. ----abc--1
  5. ----abc 1
  6. abc----2
  7. --abc---2
  8. ----abc--2
  9. ----abc 2

Btw我有500多行,当组值发生变化时,abc从a列开始。

奇怪的请求,但APPLY做得很简单:

select v.*
from t cross apply
(values (t.a, null, null, null, null),
(null, t.b, null, null, null),
(null, null, t.c, null, null),
(null, null, null, t.d, null),
(null, null, null, null, t.e)
) v(a, b, c, d, e)
with cte as 
(
select t.*, row_number()over(order by(select 1))rn from mytable t
) 
select (case when rn=1 then column_a end) column_a,
(case when rn=2 then column_b end) column_b,
(case when rn=3 then column_c end) column_c,
(case when rn=4 then column_d end) column_d,
(case when rn=5 then column_e end) column_e
from cte;

如果你想按任何特定的顺序排列,那么你可以使用你喜欢的按条款排序,而不是按(选择1(排序

最新更新