Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

SQL Server :Pivot dengan nama kolom khusus

Ada beberapa cara yang bisa Anda lakukan.

Jika Anda memiliki jumlah pertanyaan/jawaban yang diketahui maka Anda dapat menggunakan row_number() bersama dengan fungsi agregat dan ekspresi CASE:

select id,
  max(case when rn = 1 then question end) question1,
  max(case when rn = 1 then answer end) answer1,
  max(case when rn = 2 then question end) question2,
  max(case when rn = 2 then answer end) answer2,
  max(case when rn = 3 then question end) question3,
  max(case when rn = 3 then answer end) answer3
from
(
  select id, question, answer,
    row_number() over(partition by id order by id, question) rn
  from yt
) src
group by id;

Lihat SQL Fiddle dengan Demo

Saran lain adalah menggunakan fungsi UNPIVOT dan PIVOT untuk mendapatkan hasilnya. UNPIVOT akan menjawab question your dan answer kolom dan mengubahnya menjadi beberapa baris.

Sintaks dasar untuk UNPIVOT adalah:

select id,
  col+cast(rn as varchar(10)) col,
  value
from
(
  -- when you perform an unpivot the datatypes have to be the same. 
  -- you might have to cast the datatypes in this query
  select id, question, cast(answer as varchar(500)) answer,
    row_number() over(partition by id order by id, question) rn
  from yt
) src
unpivot
(
  value
  for col in (question, answer)
) unpiv;

Lihat Demo . Ini memberikan hasil:

|      ID |       COL |                                VALUE |
--------------------------------------------------------------
| 4482515 | question1 | I would like to be informed by mail. |
| 4482515 |   answer1 |                                   No |
| 4482515 | question2 |                    Plan to Purchase? |
| 4482515 |   answer2 |                       Over 12 months |
| 4482515 | question3 |                   Test Question Text |
| 4482515 |   answer3 |                          some Answer |

Seperti yang Anda lihat, saya menambahkan row_number() nilai ke subquery awal sehingga Anda dapat mengaitkan setiap jawaban dengan pertanyaan. Setelah ini tidak dipivot, Anda dapat memutar hasilnya pada nama kolom baru dengan question /answer dengan nilai nomor baris yang digabungkan. Kode dengan sintaks PIVOT akan menjadi:

select id, question1, answer1, question2, answer2,
  question3, answer3
from
(
  select id,
    col+cast(rn as varchar(10)) col,
    value
  from
  (
  -- when you perform an unpivot the datatypes have to be the same. 
  -- you might have to cast the datatypes in this query
    select id, question, cast(answer as varchar(500)) answer,
      row_number() over(partition by id order by id, question) rn
    from yt
  ) src
  unpivot
  (
    value
    for col in (question, answer)
  ) unpiv
) d
pivot
(
  max(value)
  for col in (question1, answer1, question2, answer2,
              question3, answer3)
) piv;

Lihat SQL Fiddle dengan Demo . Sekarang dalam situasi Anda, Anda menyatakan bahwa Anda akan memiliki sejumlah pertanyaan/jawaban yang dinamis. Jika demikian, maka Anda perlu menggunakan SQL dinamis untuk mendapatkan hasilnya:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(c.col+cast(rn as varchar(10))) 
                    from 
                    (
                      select row_number() over(partition by id 
                                               order by id, question) rn
                      from yt
                    ) d
                    cross apply
                    (
                      select 'question' col, 1 sort union all select 'answer', 2
                    ) c
                    group by col, rn, sort
                    order by rn, sort
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT id, ' + @cols + '
              from
              (
                select id,
                  col+cast(rn as varchar(10)) col,
                  value
                from
                (
                 -- when you perform an unpivot the datatypes have to be the same. 
                 -- you might have to cast the datatypes in this query
                  select id, question, cast(answer as varchar(500)) answer,
                    row_number() over(partition by id order by id, question) rn
                  from yt
                ) src
                unpivot
                (
                  value
                  for col in (question, answer)
                ) unpiv
              ) d
              pivot 
              (
                  max(value)
                  for col in (' + @cols + ')
              ) p '

execute(@query);

Lihat SQL Fiddle dengan Demo . Ini memberikan hasil:

|      ID |                            QUESTION1 | ANSWER1 |         QUESTION2 |        ANSWER2 |          QUESTION3 |     ANSWER3 |
------------------------------------------------------------------------------------------------------------------------------------
| 4482515 | I would like to be informed by mail. |      No | Plan to Purchase? | Over 12 months | Test Question Text | some Answer |



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. dapatkan string yang dipisahkan koma dari baris

  2. Memantau Penghitung Kinerja melalui PERFMON | Pemecahan Masalah Kinerja SQL Server -3

  3. Cara Menggunakan FILEPROPERTY() di SQL Server

  4. Gunakan FILEGROUP_ID() untuk Mengembalikan ID Filegroup di SQL Server

  5. Kontrol Sumber dan prosedur tersimpan