简体   繁体   中英

How to use CASE statement inside HAVING clause

I tried searching the site for an answer on this topic but could not find anything that answered my question. I have a feeling this is very simple.

I am writing a simple SQL Query to help find Unvouchered Purchase Orders and depending on if the purchase order type is Purchase or Credit it requires different logic in the HAVING clause. In order to get the data I had to write two different queries. See Code Samples below

PURCHASE PO

select 
prchseordr_id as 'PO ID',
max(prchseordr_dte_rqstd) as DateRequested,
max(prchseordr_type) as POType,
max(vndr_nme) as Vendor, 
sum(imhstry_qntty_ordrd) as QuantityOrdered,
sum(imhstry_qntty_invcd_ap) as QuantityVouchered
from imhstry
join prchseordr on imhstry.imhstry_ordr_id = prchseordr.prchseordr_id
join brnch on prchseordr.brnch_rn = brnch.brnch_rn
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr_dte_rqstd between '2016-01-01' and '2016-04-01'
and prchseordr_type = 'PURCHASE'
group by prchseordr.prchseordr_id
HAVING sum(imhstry_qntty_invcd_ap) < sum(imhstry_qntty_ordrd)
order by prchseordr_id asc 

CREDIT PO

select 
prchseordr_id as 'PO ID',
max(prchseordr_dte_rqstd) as DateRequested,
max(prchseordr_type) as POType,
max(vndr_nme) as Vendor, 
sum(imhstry_qntty_ordrd) as QuantityOrdered,
sum(imhstry_qntty_invcd_ap) as QuantityVouchered
from imhstry
join prchseordr on imhstry.imhstry_ordr_id = prchseordr.prchseordr_id
join brnch on prchseordr.brnch_rn = brnch.brnch_rn
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr_dte_rqstd between '2016-01-01' and '2016-04-01'
and prchseordr_type = 'CREDIT'
group by prchseordr.prchseordr_id
HAVING sum(imhstry_qntty_invcd_ap) = '0' 
order by prchseordr_id asc 

What I want to is combine these two queries into one but I am running into errors when I try to combine them using a CASE statement in the HAVING clause. Latest attempt is throwing me a syntax error on the = and < sign in the HAVING clause. I am obviously missing something important.

select 
prchseordr_id as 'PO ID',
max(prchseordr_dte_rqstd) as DateRequested,
max(prchseordr_type) as POType,
max(vndr_nme) as Vendor, 
sum(imhstry_qntty_ordrd) as QuantityOrdered,
sum(imhstry_qntty_invcd_ap) as QuantityVouchered
from imhstry
join prchseordr on imhstry.imhstry_ordr_id = prchseordr.prchseordr_id
join brnch on prchseordr.brnch_rn = brnch.brnch_rn
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr_dte_rqstd between '2016-01-01' and '2016-04-01'
group by prchseordr.prchseordr_id
HAVING CASE WHEN prchseordr_type = 'Credit' THEN sum(imhstry_qntty_invcd_ap) = '0' 
OR CASE WHEN prchseordr_type = 'Purchase' THEN sum(imhstry_qntty_invcd_ap) < sum(imhstry_qntty_ordrd)
order by prchseordr_id asc 

Any ideas or help would be much appreciated.

Use aggregation on the entire case expression.

select 
prchseordr_id as "PO ID",
max(prchseordr_dte_rqstd) as DateRequested,
prchseordr_type as POType,
max(vndr_nme) as Vendor, 
sum(imhstry_qntty_ordrd) as QuantityOrdered,
sum(imhstry_qntty_invcd_ap) as QuantityVouchered
from imhstry
join prchseordr on imhstry.imhstry_ordr_id = prchseordr.prchseordr_id
join brnch on prchseordr.brnch_rn = brnch.brnch_rn
join vndr on prchseordr.vndr_rn = vndr.vndr_rn
where prchseordr_dte_rqstd between '2016-01-01' and '2016-04-01'
group by prchseordr.prchseordr_id,prchseordr_type
HAVING SUM(CASE WHEN prchseordr_type = 'Credit' THEN imhstry_qntty_invcd_ap END) = 0 
OR SUM(CASE WHEN prchseordr_type = 'Purchase' THEN imhstry_qntty_invcd_ap-imhstry_qntty_ordrd END) < 0
order by prchseordr_id asc 
HAVING CASE 
WHEN prchseordr_type = 'Credit' AND sum(imhstry_qntty_invcd_ap) = '0' THEN 1
WHEN prchseordr_type = 'Purchase' AND sum(imhstry_qntty_invcd_ap) < sum(imhstry_qntty_ordrd) THEN 1
ELSE 0 END = 1

Something like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM