简体   繁体   中英

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?

I have a 5 columns (column a, column b, column c, column d, column e)

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?

How can i do this situation?

Edit v2:

Sorry i couldn't explain correctly. Sometimes c column can repeat. There is a column (column e) i want to group my result, that column's data like 1-2-3-4 like:

  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 I have over 500 rows and when group value changes, abc starts at column a.

Strange request, but APPLY does this pretty simply:

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;

If you want the rows in any specific order then instead of order by(select 1) you can use your prefered order by clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM