title: case statement in having clause in sql
body: <p>Can I have a case statement in a <code>HAVING</code> clause in SQL server 2005?</p>
  
  <p>Below is my <code>HAVING</code> statement. It is giving me a syntax error.</p>
  
  <pre><code>@CLIENTPK_NEW IS NULL OR 
      (
          CLIENT.OH_PK = @CLIENTPK_NEW and 
          CASE WHEN @RelatedOrgs &lt;&gt; '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 
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Amit</p>