Sql-Server

SQL Server 行到列動態

  • November 18, 2019

以下是我的表格和查詢詳細資訊。

表定義

create table #ReviewData
   (
   ProjectId int,
   ReviewId int,
   ReviewDataPointKey varchar(max),
   ReviewDataPointValue varchar(max)
   )

表中數據

Insert into #ReviewData values 
 (1,73,'SaasEncounter_1_FacilityTypeId', 'Hospital_Inpatient')
, (1,73,'SaasEncounter_2_FacilityTypeId', 'Hospital_Inpatient_Other')
, (1,73,'SaasEncounter_1_DiagnosisCode_F11.14', 'F11.14_text')
, (1,73,'SaasEncounter_1_DiagnosisCode_F11.15', 'F11.15_text')
, (1,73,'SaasEncounter_2_DiagnosisCode_F12.16', 'F12.16_text')
, (1,73,'SaasEncounter_2_DiagnosisCode_F12.17', 'F12.17_text')
, (1,73,'SaasEncounter_1_EncounterStartDate', '11/04/2019')
, (1,73,'SaasEncounter_1_EncounterEndDate', '11/04/2019')
, (1,73,'SaasEncounter_2_EncounterStartDate', '11/08/2019')
, (1,73,'SaasEncounter_2_EncounterEndDate', '11/08/2019')
, (1,73,'SaasEncounter_1_DiagnosisPageNumber', '3')
, (1,73,'SaasEncounter_2_DiagnosisPageNumber', '5')
, (1,73,'SaasEncounter_1_ProviderCredential', 'Sample')
, (1,73,'SaasEncounter_2_ProviderCredential', 'Testing')
, (1,73,'SaasEncounter_3_DiagnosisCode_F12.16', 'F12.16_text')
, (1,73,'SaasEncounter_3_DiagnosisCode_F12.17', 'F12.17_text')
, (1,73,'SaasEncounter_3_FacilityTypeId', 'OutPatient')
, (1,73,'SaasEncounter_4_FacilityTypeId', 'OutPatient_Other')
, (1,73,'SaasEncounter_4_ProviderCredential', 'New')
, (1,901,'SaasEncounter_1_FacilityTypeId', 'Physician_InPatient')
, (1,901,'SaasEncounter_2_FacilityTypeId', 'Physician_InPatient_Other')
, (1,901,'SaasEncounter_1_DiagnosisCode_F10.14', 'F10.14_text')
, (1,901,'SaasEncounter_1_DiagnosisCode_F10.15', 'F10.15_text')
, (1,901,'SaasEncounter_2_DiagnosisCode_F20.16', 'F20.16_text')
, (1,901,'SaasEncounter_2_DiagnosisCode_F20.17', 'F20.17_text')
, (1,901,'SaasEncounter_1_DiagnosisCode_F25.14', 'F25.14_text')
, (1,901,'SaasEncounter_1_DiagnosisCode_F35.15', 'F35.15_text')
, (1,901,'SaasEncounter_2_DiagnosisCode_F45.16', 'F45.16_text')
, (1,901,'SaasEncounter_2_DiagnosisCode_F65.17', 'F65.17_text')
, (1,901,'SaasEncounter_1_EncounterStartDate', '11/04/2019')
, (1,901,'SaasEncounter_1_EncounterEndDate', '11/04/2019')
, (1,901,'SaasEncounter_2_EncounterStartDate', '11/08/2019')
, (1,901,'SaasEncounter_2_EncounterEndDate', '11/08/2019')
, (1,902,'SaasEncounter_1_EncounterStartDate', '11/08/2019')
, (1,902,'SaasEncounter_1_EncounterEndDate', '11/11/2019')
, (1,902,'SaasEncounter_2_EncounterStartDate', '11/05/2019')
, (1,902,'SaasEncounter_2_EncounterEndDate', '11/08/2019')

簡單的 SELECT 來驗證數據

Select * from #ReviewData where ProjectId = 1

我正在嘗試執行的 SELECT 語句

select
   rd.ProjectId, 
   rd.ReviewId, 
   rdpv.ReviewDataPointValue as FacilityType,
   rdpv3.ReviewDataPointValue as DiagnosisCode,
   rdpv1.ReviewDataPointValue as EncounterStartDate,
   rdpv2.ReviewDataPointValue as EncounterEndDate,
   rdpv4.ReviewDataPointValue as DiagnosisPageNumber,
   rdpv5.ReviewDataPointValue as ProviderCredential
from
   #ReviewData rd JOIN Project P ON P.ProjectId = 1 and rd.ProjectId = 1
   JOIN Client C ON C.ClietId = P.ClientId 
outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd2
    where  rd2.ProjectId = rd.ProjectId
           and rd2.ReviewId = rd.ReviewId
           and rd2.ReviewDataPointKey like '%FacilityTypeId%'
           and rd.ReviewDataPointKey like replace(rd2.ReviewDataPointKey, '_FacilityTypeId', '%')

           ) rdpv
           outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd2
    where  rd2.ProjectId = rd.ProjectId
           and rd2.ReviewId = rd.ReviewId
           and rd2.ReviewDataPointKey like '%EncounterStartDate%'
           and rd.ReviewDataPointKey like replace(rd2.ReviewDataPointKey, '_EncounterStartDate', '%')

           ) rdpv1
                       outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd2
    where  rd2.ProjectId = rd.ProjectId
           and rd2.ReviewId = rd.ReviewId
           and rd2.ReviewDataPointKey like '%EncounterEndDate%'
           and rd.ReviewDataPointKey like replace(rd2.ReviewDataPointKey, '_EncounterEndDate', '%')

           ) rdpv2
                                   outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd2
    where  rd2.ProjectId = rd.ProjectId
           and rd2.ReviewId = rd.ReviewId
           and rd2.ReviewDataPointKey like '%DiagnosisCode%'
           and rd.ReviewDataPointKey like replace(rd2.ReviewDataPointKey, '_DiagnosisCode', '%')

           ) rdpv3
                                               outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd3
    where  rd3.ProjectId = rd.ProjectId
           and rd3.ReviewId = rd.ReviewId
           and rd3.ReviewDataPointKey like '%DiagnosisPageNumber%'
           and rd.ReviewDataPointKey like replace(rd3.ReviewDataPointKey, '_DiagnosisPageNumber', '%')

           ) rdpv4
                                               outer apply
   (select ReviewDataPointValue
    from   #ReviewData rd4
    where  rd4.ProjectId = rd.ProjectId
           and rd4.ReviewId = rd.ReviewId
           and rd4.ReviewDataPointKey like '%ProviderCredential%'
           and rd.ReviewDataPointKey like replace(rd4.ReviewDataPointKey, '_ProviderCredential', '%')

           ) rdpv5

where
   rd.ReviewDataPointKey not like '%FacilityTypeId%'and
       rd.ReviewDataPointKey not like '%EncounterStartDate%' and
        rd.ReviewDataPointKey not like '%EncounterEndDate%' and 
                rd.ReviewDataPointKey not like '%ProviderCredential%' and 
                        rd.ReviewDataPointKey not like '%ProviderCredential%' and 

        rd.ProjectId = 1

我目前的輸出

在此處輸入圖像描述

我的預期輸出

在此處輸入圖像描述

嗨,麥克內茨,

你能幫我得到上述預期的輸出嗎?

另外你能解釋一下你使用的兩行嗎?

  1. rd.ReviewDataPointKey like replace(rd4.ReviewDataPointKey, '_FacilityTypeId', '%')
  2. where rd.ReviewDataPointKey not like '%FacilityTypeId%'

編輯

如果您想使用所有這些 APPLY 連接,只需將所有 CROSS APPLY 更改為 OUTER APPLY 即可。

db<>在這裡擺弄

首先,當您提出新問題時,讓我建議您,不要說“這是我想要的結果”,而是嘗試描述您想要實現的目標,在這種情況下,您需要通過 Facility 和 DiagnosisResult 來調整結果。

恕我直言,您正在處理一個糟糕的設計。也許轉換您的數據可以改善您的查詢。

create table #rd 
( 
    ProjectId int, 
    ReviewId int,
    Key1 varchar(100),
    Key2 varchar(100),
    RDPValue varchar(max),
);

create nonclustered index ix_rd on #rd
(
   ProjectId, ReviewId, Key1, Key2
)

insert into #rd
select
   ProjectId,
   ReviewId,
   substring(ReviewDataPointKey, 0, charindex('_', ReviewDataPointKey, 15)) [Key1],
   case
     when substring(ReviewDataPointKey, 
                    charindex('_', ReviewDataPointKey, 15) + 1,
                    len(ReviewDataPointKey) - charindex('_', ReviewDataPointKey, 15) + 1)
                    like 'DiagnosisCode%' then 'DiagnosisCode'
     else substring(ReviewDataPointKey, 
                    charindex('_', ReviewDataPointKey, 15) + 1,
                    len(ReviewDataPointKey) - charindex('_', ReviewDataPointKey, 15) + 1)
     end as [Key2],
   ReviewDataPointValue
from
   ReviewData
where
   ProjectId = 1;

我使用了一個臨時表將您的原始數據轉換為:

項目編號 | 評論 ID | 鍵1 | 鍵2 | RDP值 
--------: | -------: | :-------------- | :------------------ | :------------------------
 1 | 73 | SaasEncounter_1 | 設施類型 ID | 醫院_住院部 
 1 | 73 | SaasEncounter_2 | 設施類型 ID | 醫院_住院病人_其他
 1 | 73 | SaasEncounter_1 | 診斷程式碼 | F11.14_text 
 1 | 73 | SaasEncounter_1 | 診斷程式碼 | F11.15_text 
 1 | 73 | SaasEncounter_2 | 診斷程式碼 | F12.16_text 
 1 | 73 | SaasEncounter_2 | 診斷程式碼 | F12.17_text 
 1 | 73 | SaasEncounter_1 | 相遇開始日期 | 2019 年 11 月 4 日 
 1 | 73 | SaasEncounter_1 | 遇到結束日期 | 2019 年 11 月 4 日 
 1 | 73 | SaasEncounter_2 | 相遇開始日期 | 2019 年 12 月 8 日 
 1 | 73 | SaasEncounter_2 | 遇到結束日期 | 13/08/2019 
 1 | 73 | SaasEncounter_1 | 診斷頁碼 | 3 
 1 | 73 | SaasEncounter_2 | 診斷頁碼 | 5 
 1 | 73 | SaasEncounter_1 | 提供者憑證 | 樣本 
 1 | 73 | SaasEncounter_2 | 提供者憑證 | 測試 
 1 | 73 | SaasEncounter_3 | 診斷程式碼 | F12.16_text 
 1 | 73 | SaasEncounter_3 | 診斷程式碼 | F12.17_text 
 1 | 73 | SaasEncounter_3 | 設施類型 ID | 門診 
 1 | 901 | SaasEncounter_1 | 設施類型 ID | Physician_InPatient 
 1 | 901 | SaasEncounter_2 | 設施類型 ID | Physician_InPatient_Other
 1 | 901 | SaasEncounter_1 | 診斷程式碼 | F10.14_text 
 1 | 901 | SaasEncounter_1 | 診斷程式碼 | F10.15_text 
 1 | 901 | SaasEncounter_2 | 診斷程式碼 | F20.16_text 
 1 | 901 | SaasEncounter_2 | 診斷程式碼 | F20.17_text 
 1 | 901 | SaasEncounter_1 | 診斷程式碼 | F25.14_text 
 1 | 901 | SaasEncounter_1 | 診斷程式碼 | F35.15_text 
 1 | 901 | SaasEncounter_2 | 診斷程式碼 | F45.16_text 
 1 | 901 | SaasEncounter_2 | 診斷程式碼 | F65.17_text 
 1 | 901 | SaasEncounter_1 | 相遇開始日期 | 2019 年 11 月 4 日 
 1 | 901 | SaasEncounter_1 | 遇到結束日期 | 2019 年 11 月 4 日 
 1 | 901 | SaasEncounter_2 | 相遇開始日期 | 2019 年 12 月 8 日 
 1 | 901 | SaasEncounter_2 | 遇到結束日期 | 13/08/2019 

現在您可以通過這種方式旋轉您的數據:

select
   rd1.ProjectId,
   rd1.ReviewId,
   rd1.RDPValue as FacilityType,
   rd2.DiagnosisCode,
   (select RDPValue
    from   #rd 
    where  #rd.ProjectId = rd1.ProjectId
           and #rd.ReviewId = rd1.ReviewId
           and #rd.Key1 = rd1.Key1
           and #rd.Key2 = 'EncounterStartDate') as EncounterStartDate,
   (select RDPValue
    from   #rd 
    where  #rd.ProjectId = rd1.ProjectId
           and #rd.ReviewId = rd1.ReviewId
           and #rd.Key1 = rd1.Key1
           and #rd.Key2 = 'EncounterEndDate') as EncounterEndDate,
   (select RDPValue
    from   #rd 
    where  #rd.ProjectId = rd1.ProjectId
           and #rd.ReviewId = rd1.ReviewId
           and #rd.Key1 = rd1.Key1
           and #rd.Key2 = 'DiagnosisPageNumber') as DiagPageNum,
   (select RDPValue
    from   #rd 
    where  #rd.ProjectId = rd1.ProjectId
           and #rd.ReviewId = rd1.ReviewId
           and #rd.Key1 = rd1.Key1
           and #rd.Key2 = 'ProviderCredential') as ProviderCredential
from
   #rd rd1
outer apply
   (select RDPValue as DiagnosisCode
    from   #rd rd2
    where  rd2.ProjectId = rd1.ProjectId
           and rd2.ReviewId = rd1.ReviewId
           and rd2.Key1 = rd1.Key1
           and rd2.Key2 = 'DiagnosisCode') rd2
where
   rd1.Key2 = 'FacilityTypeId';

這是結果:

項目編號 | 評論 ID | 設施類型 | 診斷程式碼 | 相遇開始日期 | 遇到結束日期 | 診斷頁碼 | 提供者憑證
--------: | -------: | :------------------------ | :------------ | :----------------- | :--------------- | :---------- | :-----------------
 1 | 73 | 醫院_住院部 | F11.14_text | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | 3 | 樣本 
 1 | 73 | 醫院_住院部 | F11.15_文本 | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | 3 | 樣本 
 1 | 73 | 醫院_住院_其他 | F12.16_text | 2019 年 12 月 8 日 | 13/08/2019 | 5 | 測試 
 1 | 73 | 醫院_住院_其他 | F12.17_文本 | 2019 年 12 月 8 日 | 13/08/2019 | 5 | 測試 
 1 | 73 | 門診 | F12.16_text | *空* | *空* | *空* | *空* 
 1 | 73 | 門診 | F12.17_文本 | *空* | *空* | *空* | *空* 
 1 | 901 | 醫師_住院病人 | F10.14_文本 | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | *空* | *空* 
 1 | 901 | 醫師_住院病人 | F10.15_text | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | *空* | *空值* 
 1 | 901 | 醫師_住院病人 | F25.14_文本 | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | *空* | *空* 
 1 | 901 | 醫師_住院病人 | F35.15_text | 2019 年 11 月 4 日 | 2019 年 11 月 4 日 | *空* | *空* 
 1 | 901 | Physician_InPatient_Other | F20.16_text | 2019 年 12 月 8 日 | 13/08/2019 | *空* | *空* 
 1 | 901 | Physician_InPatient_Other | F20.17_文本 | 2019 年 12 月 8 日 | 13/08/2019 | *空* | *空值* 
 1 | 901 | Physician_InPatient_Other | F45.16_text | 2019 年 12 月 8 日 | 13/08/2019 | *空* | *空* 
 1 | 901 | Physician_InPatient_Other | F65.17_text | 2019 年 12 月 8 日 | 13/08/2019 | *空* | *空值*              

db<>在這裡擺弄

引用自:https://dba.stackexchange.com/questions/253477