Select count(ITEM) from (
select D5.ORD_NUM, D5.ORD_LEV1 ITEM, D3.ALT_INVT_REP_CODE BRAND
from E_ORD_D5 D5
LEFT JOIN M_ITEM_D3 D3 on D3.COMP_CODE = D5.COMP_CODE and D3.CUST_CODE = D5.CUST_CODE and D3.ITEM_CODE = D5.ORD_LEV1
where D5.comp_code = 'W1' and D5.ord_num = 935373
)
WHERE BRAND = 'WILLIAMS'
SELECT
COUNT( ITEM ) total_items
, COUNT( BRAND ) wiliams_items
, MAX( BRAND ) brand
, MAX(CASE WHEN BRAND = 'WILLIAMS' THEN 'My String' END) some_string
FROM (
SELECT
D5.ORD_NUM
, D5.ORD_LEV1 ITEM
, D3.ALT_INVT_REP_CODE BRAND
FROM E_ORD_D5 D5
LEFT JOIN M_ITEM_D3 D3 ON D3.COMP_CODE = D5.COMP_CODE
AND D3.CUST_CODE = D5.CUST_CODE
AND D3.ITEM_CODE = D5.ORD_LEV1
AND D3.BRAND = 'WILLIAMS'
WHERE D5.comp_code = 'W1'
AND D5.ord_num = 935373
) d
;
If you are not interested in the count of all items, use an inner join, and here you can use the brand restriction in a where clause
SELECT
COUNT( BRAND ) wiliams_items
, MAX( BRAND ) brand
, MAX(CASE WHEN BRAND = 'WILLIAMS' THEN 'My String' END) some_string
FROM (
SELECT
D5.ORD_NUM
, D5.ORD_LEV1 ITEM
, D3.ALT_INVT_REP_CODE BRAND
FROM E_ORD_D5 D5
INNER JOIN M_ITEM_D3 D3 ON D3.COMP_CODE = D5.COMP_CODE
AND D3.CUST_CODE = D5.CUST_CODE
AND D3.ITEM_CODE = D5.ORD_LEV1
WHERE D5.comp_code = 'W1'
AND D5.ord_num = 935373
AND D3.BRAND = 'WILLIAMS'
) d
;
D5.ORD_NUM D5.ORD_LEV1 D3.ALT_INVT_REP_CODE
--------- ----------- --------------------
10001 AB001 MIKE
10001 AB023 MIKE
10001 AQ234 JOHN
10002 SW234 FRANK
10002 AQ232 WILLIAMS
10002 AB111 MIKE
select min(ord_num), 'MY STRING'
from tab1
where alt_invt_rep_code = 'WILLIAMS'
/
null
null
MY STRING
Null
MY STRING
Open in new window