Reputation: 13
This is my table :
Srlno Item Code Category Code
1 BT/000001 CLR
2 BT/000001 SIZ
3 BT/000001 WAT
1 BT/000002 Swt
2 BT/000002 Mol
3 BT/000002 SIZ
I use this Query:
SELECT Item_Code
FROM T_SubCatogoryDtls
WHERE SubCat_code in (N'SIZ' , N'Swt')
GROUP BY Item_Code
but the result is:
BT/000001
BT/000002
I need BT/000002
because category code Siz
and Swt
common in this item code
what is correct query
Upvotes: 1
Views: 37
Reputation: 72215
You have to add a HAVING
clause:
SELECT Item_Code
FROM T_SubCatogoryDtls
WHERE SubCat_code in (N'SIZ' , N'Swt')
GROUP BY Item_Code
HAVING COUNT(DISTINCT SubCat_code) = 2
HAVING
filters out any Item_Code
groups not having both codes, i.e. 'SIZ'
and 'Swt'
.
Upvotes: 2