sql >> Database teknologi >  >> RDS >> Sqlserver

Hvordan PIVOT over op til 49 felter?

Din første forespørgsel er den nærmeste, bortset fra at du ikke vedlagde alle case ind i samlet funktion. Det ser også ud til, at du ikke har brug for nogle kolonner i group by klausul:

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. Anonym TABLE- eller VARRAY-type i Oracle

  2. Sådan indsætter du json-output i mysql med node js

  3. Fundamentals of Table Expressions, Del 13 – Inline Table-Valued Functions, Fortsat

  4. MYSQL Vælg fra tabeller baseret på flere rækker