在sql中的having子句中的case语句

前端之家收集整理的这篇文章主要介绍了在sql中的having子句中的case语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以在sql Server 2005的HAVING子句中使用case语句吗?

以下是我的HAVING声明.它给了我一个语法错误.

@CLIENTPK_NEW IS NULL OR 
    (
        CLIENT.OH_PK = @CLIENTPK_NEW and 
        CASE WHEN @RelatedOrgs <> '11' then CLIENT.OH_PK= @CLIENTPK_NEW
        ELSE CLIENT.OH_PK in (
            SELECT dbo.OrgHeader.OH_PK FROM dbo.OrgHeader WITH (NOLOCK) INNER JOIN
            dbo.OrgRelatedParty WITH (NOLOCK) ON dbo.OrgHeader.OH_PK = dbo.OrgRelatedParty.PR_OH_Parent INNER JOIN
            dbo.OrgHeader AS OrgHeader_1 WITH (NOLOCK) ON dbo.OrgRelatedParty.PR_OH_RelatedParty = OrgHeader_1.OH_PK
            where OrgHeader_1.OH_PK = @CLIENTPK_NEW
        ) 
        END 
    )
}
AND (@CGNEEPK IS NULL OR CGNEE.OH_PK = @CGNEEPK) AND    
part.OP_RH_NKCommodityCode = @type

谢谢,

阿米特

解决方法

示例(从 here开始):
USE AdventureWorks2008R2;
GO
SELECT JobTitle,MAX(ph1.Rate)AS MaximumRate
FROM HumanResources.Employee AS e
JOIN HumanResources.EmployeePayHistory AS ph1 
    ON e.BusinessEntityID = ph1.BusinessEntityID
GROUP BY JobTitle
HAVING (MAX(CASE WHEN Gender = 'M' 
        THEN ph1.Rate 
        ELSE NULL END) > 40.00
     OR MAX(CASE WHEN Gender  = 'F' 
        THEN ph1.Rate  
        ELSE NULL END) > 42.00)
ORDER BY MaximumRate DESC;
原文链接:https://www.f2er.com/mssql/76383.html

猜你在找的MsSQL相关文章