2017-03-08 8 views
0

2つのテーブルを結合してCASE文を使用しようとしていますが、結果が得られません。ここでINNER JOIN CASE文を使用する

SELECT i.[ProviderID], l.FacilityName, 
     CASE WHEN [StepID] = 1 THEN 'Step0.5' END AS 'Step0.5', 
     CASE WHEN [StepID] = 2 THEN 'StepI' END AS 'StepI', 
     CASE WHEN [StepID] = 4 THEN 'StepI.D' END AS 'StepI.D', 
     CASE WHEN [StepID] = 5 THEN 'StepII.1' END AS 'StepII.1', 
     CASE WHEN [StepID] = 6 THEN 'StepII.5' END AS 'StepII.5', 
     CASE WHEN [StepID] = 7 THEN 'StepII.D' END AS 'StepII.D', 
     CASE WHEN [StepID] = 8 THEN 'StepIII.1' END AS 'StepIII.1' 
    FROM RD.dbo.vwProvider as l 
    INNER JOIN [RD].[dbo].[vwSteps] i 
    ON l.ProviderID = i.ProviderID 
    Group by i.[ProviderID], l.FacilityName, [StepID] 
    ORDER BY FacilityName 

私の現在の結果されています:ここで

は私の既存のコードである

ProviderID FacilityName Step0.5 StepI StepI.D StepII.1 StepII.5 StepII.D StepIII.1 

4   This Is Us Step0.5 NULL NULL  NULL  NULL  NULL  NULL 
4   This Is Us NULL  NULL StepI.D NULL  NULL  NULL  NULL 
6   Almost  NULL  NULL NULL  StepII.1 NULL  NULL  NULL 
6   Almost  NULL  NULL NULL  NULL  NULL  StepII.D NULL 
6   Almost  NULL  StepI NULL  NULL  NULL  NULL  NULL 

ここでは、私が探しています結果は以下のとおりです。

ProviderID FacilityName Step0.5 StepI StepI.D StepII.1 StepII.5 StepII.D StepIII.1 

4   This Is Us Step0.5 NULL StepI.D NULL  NULL  NULL  NULL 
6   Almost  NULL  StepI NULL  StepII.1 NULL  StepII.D NULL 
+1

があなたのDBMSにタグを付けてください。 – McNets

答えて

0
MAX()または MIN()を使用して

集計し、あなたがStepIDGROUP BYには必要ありませんようだ:

SELECT i.[ProviderID], l.FacilityName, 
     max(CASE WHEN [StepID] = 1 THEN 'Step0.5' END) AS 'Step0.5', 
     max(CASE WHEN [StepID] = 2 THEN 'StepI'  END) AS 'StepI', 
     max(CASE WHEN [StepID] = 4 THEN 'StepI.D' END) AS 'StepI.D', 
     max(CASE WHEN [StepID] = 5 THEN 'StepII.1' END) AS 'StepII.1', 
     max(CASE WHEN [StepID] = 6 THEN 'StepII.5' END) AS 'StepII.5', 
     max(CASE WHEN [StepID] = 7 THEN 'StepII.D' END) AS 'StepII.D', 
     max(CASE WHEN [StepID] = 8 THEN 'StepIII.1' END) AS 'StepIII.1' 
    FROM RD.dbo.vwProvider as l INNER JOIN [RD].[dbo].[vwSteps] i ON l.ProviderID = i.ProviderID 
     Group by i.[ProviderID], l.FacilityName 
     ORDER BY FacilityName 
+0

「Step0.5」に複数の一致があるとどうなりますか? –

+0

これは完全に機能しました!ありがとう。 – tnbumbray

+0

@Raj More Step0.5に複数の一致がある場合、それは1つの行に集約されます – leftjoin

1

あなたが削除する必要がありますStepIdGROUP BYから入手し、集約関数を導入してください:

SELECT i.[ProviderID], l.FacilityName, 
     MAX(CASE WHEN [StepID] = 1 THEN 'Step0.5') END AS 'Step0.5', 
     MAX(CASE WHEN [StepID] = 2 THEN 'StepI' END) AS 'StepI', 
     MAX(CASE WHEN [StepID] = 4 THEN 'StepI.D' END) AS 'StepI.D', 
     MAX(CASE WHEN [StepID] = 5 THEN 'StepII.1' END) AS 'StepII.1', 
     MAX(CASE WHEN [StepID] = 6 THEN 'StepII.5' END) AS 'StepII.5', 
     MAX(CASE WHEN [StepID] = 7 THEN 'StepII.D' END) AS 'StepII.D', 
     MAX(CASE WHEN [StepID] = 8 THEN 'StepIII.1' END) AS 'StepIII.1' 
    FROM RD.dbo.vwProvider l INNER JOIN 
     [RD].[dbo].[vwSteps] i 
     ON l.ProviderID = i.ProviderID 
    Group by i.[ProviderID], l.FacilityName 
    ORDER BY FacilityName