需要添加一个条款

时间:2015-02-11 22:28:00

标签: sql

使用此查询...

SELECT 
    Product, LengthInch, Sum(PieceCount) AS TotPcs, 
    WeekYear, SpecialReq, ControlRoll, 
    CASE WHEN SpecialReq IS NULL THEN 0 ELSE CustNo END CustNum 
FROM 
    RMCutSumData 
GROUP BY 
    Product, LengthInch, WeekYear, SpecialReq, ProdGrade, 
    ControlRoll, 
    CASE WHEN SpecialReq IS NULL THEN 0 ELSE CustNo END
HAVING 
    Product = 'W14X145' 
    AND ProdGrade = '992' 
    AND WeekYear = 615 
ORDER BY 
    LengthInch 

上面的查询很有用:我需要添加

when SpecialReq IS NULL and CustNo = 1988 then 1988 else CustNo

我已经尝试了所有我能想到的东西,而且脸色很蓝。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为您希望将Group by case更改为包含上述条件

试试这个......

SELECT Product
    ,LengthInch
    ,Sum(PieceCount) AS TotPcs
    ,WeekYear
    ,SpecialReq
    ,ControlRoll
    ,CASE 
        WHEN SpecialReq IS NULL AND CUSTNO=1988
            THEN 1988
        WHEN SpecialReq IS NULL
        THEN 0
        ELSE CustNo
        END CustNum
FROM RMCutSumData
where Product = 'W14X145'
    AND ProdGrade = '992'
    AND WeekYear = 615
GROUP BY Product
    ,LengthInch
    ,WeekYear
    ,SpecialReq
    ,ProdGrade
    ,ControlRoll
    ,CASE 
        WHEN SpecialReq IS NULL AND CUSTNO=1988
            THEN 1988
        WHEN SpecialReq IS NULL
        THEN 0
        ELSE CustNo
        END

ORDER BY LengthInch

对于基于行的条件,我更倾向于使用Where而不是Having。如果您想在聚合上应用条件,我建议使用Having