2015-12-29 89 views
1

我無法獲取只有一行的輸出。本集團給出了一個錯誤,如果我不包括TF.Table_Field_DescTFV.Value項目,但它的輸出:多個ALIAS GROUP BY問題,返回比需要更多的行

Current output

我想要得到的結果,其中,代替六行與前三欄是重複和大量的空值,我得到一行與我所有的列填充。

我想我需要改變我做CASE陳述的方式,但我不知道如何。謝謝!

SELECT V.Var_Desc, RTS.Route_Desc, T.Result, 
CASE WHEN TF.Table_Field_Desc = 'TestTime' THEN TFV.Value END AS Duration, 
CASE WHEN TF.Table_Field_Desc = 'LongTaskName' THEN TFV.Value END AS TaskName, 
CASE WHEN TF.Table_Field_Desc = 'Criteria' THEN TFV.Value END AS Criteria, 
CASE WHEN TF.Table_Field_Desc = 'Method' THEN TFV.Value END AS Method, 
CASE WHEN TF.Table_Field_Desc = 'Hazards' THEN TFV.Value END AS Hazards, 
CASE WHEN TF.Table_Field_Desc = 'TaskType' THEN TFV.Value END AS TaskType 
FROM dbo.Tests AS T 
    RIGHT JOIN dbo.Table_Fields_Values AS TFV ON T.Var_Id = TFV.KeyId 
    RIGHT JOIN dbo.Table_Fields AS TF ON TFV.Table_Field_Id = TF.Table_Field_Id 
    RIGHT JOIN dbo.Variables AS V ON T.Var_Id = V.Var_Id 
    RIGHT JOIN dbo.RouteTasks AS RTKS ON T.Var_Id = RTKS.Var_Id 
    RIGHT JOIN dbo.Routes AS RTS ON RTKS.Route_Id = RTS.Route_Id 
    RIGHT JOIN dbo.TeamRoutes AS TMRTS ON RTKS.Route_Id = TMRTS.Route_Id 
    RIGHT JOIN dbo.Teams AS TMS ON TMRTS.Team_Id = TMS.Team_Id AND TMS.Team_Desc LIKE '%C Team%' 
WHERE TFV.Table_Field_Id IN (31,32,35,40,41,42) AND (T.Result = 'Pending' OR T.Result = 'Late') 
GROUP BY TFV.Value, TF.Table_Field_Desc, T.Result, V.Var_Desc, RTS.Route_Desc 
ORDER BY Var_Desc 

回答

0
SELECT 
    V.Var_Desc, 
    RTS.Route_Desc, 
    T.Result, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'TestTime' THEN TFV.Value END) AS Duration, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'LongTaskName' THEN TFV.Value END) AS TaskName, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'Criteria' THEN TFV.Value END) AS Criteria, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'Method' THEN TFV.Value END) AS Method, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'Hazards' THEN TFV.Value END) AS Hazards, 
    MAX(CASE WHEN TF.Table_Field_Desc = 'TaskType' THEN TFV.Value END) AS TaskType 
FROM dbo.Tests AS T 
RIGHT JOIN ... 
WHERE TFV.Table_Field_Id IN (31,32,35,40,41,42) 
    AND T.Result IN ('Pending', 'Late') 
GROUP BY TFV.Value, T.Result, V.Var_Desc, RTS.Route_Desc 
ORDER BY Var_Desc 
1

我猜測你想要更多這樣的查詢:

SELECT V.Var_Desc, RTS.Route_Desc, T.Result, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'TestTime' THEN TFV.Value END) AS Duration, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'LongTaskName' THEN TFV.Value END AS TaskName, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'Criteria' THEN TFV.Value END) AS Criteria, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'Method' THEN TFV.Value END) AS Method, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'Hazards' THEN TFV.Value END) AS Hazards, 
     MAX(CASE WHEN TF.Table_Field_Desc = 'TaskType' THEN TFV.Value END) AS TaskType 
FROM dbo.Tests AS T 
    RIGHT JOIN dbo.Table_Fields_Values AS TFV ON T.Var_Id = TFV.KeyId 
    RIGHT JOIN dbo.Table_Fields AS TF ON TFV.Table_Field_Id = TF.Table_Field_Id 
    RIGHT JOIN dbo.Variables AS V ON T.Var_Id = V.Var_Id 
    RIGHT JOIN dbo.RouteTasks AS RTKS ON T.Var_Id = RTKS.Var_Id 
    RIGHT JOIN dbo.Routes AS RTS ON RTKS.Route_Id = RTS.Route_Id 
    RIGHT JOIN dbo.TeamRoutes AS TMRTS ON RTKS.Route_Id = TMRTS.Route_Id 
    RIGHT JOIN dbo.Teams AS TMS ON TMRTS.Team_Id = TMS.Team_Id AND TMS.Team_Desc LIKE '%C Team%' 
WHERE TFV.Table_Field_Id IN (31, 32, 35, 40, 41, 42) AND 
     T.Result IN ('Pending', 'Late') 
GROUP BY V.Var_Desc, RTS.Route_Desc, T.Result, 
ORDER BY Var_Desc; 
+0

冗餘'(''附近... T.Result' +1 :) – Devart

+0

HRM,也許我不正確地使用它,但是當我這樣做時,它會返回正確的行數,但是我所有的ALIASed列都只是充滿NULL值。所有ALIASed列將會有字符串值,如果有所作爲。 –

+0

@RussRosenlof。 。 。你可以設置一個SQL小提琴嗎? –