多个ALIAS GROUP BY问题,返回的行数超过了所需的数量

时间:2015-12-29 14:43:55

标签: sql sql-server

我无法获得只有一行的输出。如果我不包含TF.Table_Field_DescTFV.Value项,但它输出了以下内容,则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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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