ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.8k

How to get partid that have two category and exist on 2000,2200 ?

Mar 15 2021 8:44 PM

How to get partid that have two category for same part and exist on 2000,2200 ?
I work on sql server 2012 i face issue i can't get partid that have two category for same part
and these two category must be on category 2000 and 2200

as partid 1246 it have two category for same part id
and also thesetwo category 2000 and 2200

so How to make select query do that please ?
  1. create table #category  
  2. (  
  3. PartId int,  
  4. Category int  
  5. )  
  6. insert into #category(PartId,Category)  
  7. values  
  8. (1246,2000),  
  9. (1246,2200),  
  10. (1250,2000),  
  11. (1250,2200),  
  12. (1290,2000),  
  13. (1350,2200),  
  14. (4000,3000),  
  15. (4000,5000)  
  16.   
  17.   
  18. what i try :  
  19.   
  20.  select partid,Category from #category  
  21.  where category in (2000,2200)  
  22.  group by partid  
  23.  having count(partid)=2  
  24.   
  25. expected result  
  26.   
  27. PartId Category  
  28. 1246 2000  
  29. 1246 2200  
  30. 1250 2000  
  31. 1250 2200

Answers (3)