2016-12-28 38 views
-1

我從表中選擇PatientID,Location,LN,FN,ServiceCode1,ServiceDate1,ServiceCode2,ServiceDate2。我試圖列出每個服務和相應的日期作爲自己的行。附圖顯示了我想如何以綠色突出顯示。我試過使用PIVOT功能,但沒有運氣。 ColumnToRow將特定列轉換爲SQL中的行

+0

你真正想要'UNPIVOT'。 http://stackoverflow.com/questions/24828346/sql-server-unpivot-multiple-columns –

+0

第二個UNPIVOT。你能提供樣本數據嗎?謝謝 –

回答

1

與交叉的幫助下加入

Select A.PatientID 
     ,A.Location 
     ,A.Last_Name 
     ,A.First_Name 
     ,B.* 
From YourTable A 
Cross Join (
       Values ('Service_Code1',A.Service_Date1) 
        ,('Service_Code2',A.Service_Date2) 
      ) B (Service_Code,Service_Date)