我可以在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@H_403_5@谢谢,
阿米特
解决方法
示例(从
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;@H_403_5@