[英]Could there be a situation like bringing only the data of the 1st column to first row and the 2nd row the data of the 2nd column?
我有 5 列(a 列、b 列、c 列、d 列、e 列)
會不會有這樣的情況,比如只把第一列的數據帶到第一行,第二行把第二列的數據帶到第二行?
這種情況我該怎么辦?
編輯 v2:
抱歉,我無法正確解釋。 有時 c 列可以重復。 有一列(列 e)我想對我的結果進行分組,該列的數據如 1-2-3-4 如下:
順便說一句,我有超過 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;
如果您想要任何特定順序的行,那么您可以使用您喜歡的 order by 子句而不是 order by(select 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.