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

Bagaimana cara PIVOT hingga 49 bidang?

Kueri pertama Anda adalah yang terdekat, kecuali bahwa Anda tidak menyertakan setiap case menjadi fungsi agregat. Juga tampaknya Anda tidak memerlukan beberapa kolom di group by klausa:

select
    [Encounter Number], 
    Proc1 = max(CASE When [Encounter Proc Sequence] = '1' Then [Procedure Code (Enctr)] END),
    SvcDate1 = max(Case When [Encounter Proc Sequence] = '1' Then [Date of Service] END),
    Surgeon1 = max(CASE When [Encounter Proc Sequence] = '1' Then [Surgeon] END),
    Proc2 = max(CASE When [Encounter Proc Sequence] = '2' Then [Procedure Code (Enctr)] END),
    SvcDate2 = max(Case When [Encounter Proc Sequence] = '2' Then [Date of Service] END),
    Surgeon2 = max(CASE When [Encounter Proc Sequence] = '2' Then [Surgeon] END),
    Proc3 = max(CASE When [Encounter Proc Sequence] = '3' Then [Procedure Code (Enctr)] END),
    SvcDate3 = max(Case When [Encounter Proc Sequence] = '3' Then [Date of Service] END),
    Surgeon3 = max(CASE When [Encounter Proc Sequence] = '3' Then [Surgeon] END)
    --- etc.
from EncounterProc
where [Date of Service] between '20090101' and '20091231'
    and [Procedure Code (ENCTR)] is not null
group by [Encounter Number], [Encounter Proc Sequence]



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pengaturan mana yang menyediakan format datetime default SQL Server?

  2. Menghubungkan Delphi di Linux ke SQL Server

  3. Cara Menonaktifkan Change Data Capture (CDC) Pada Database di SQL Server - Tutorial SQL Server

  4. Konversi Baris ke kolom menggunakan 'Pivot' di SQL Server

  5. Klausa WHERE pada tipe data Teks SQL Server