Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
gagani
Posting Yak Master
112 Posts |
Posted - 2012-03-13 : 11:10:46
|
| orderid productid quantity100 1 2100 2 1100 3 1101 1 4102 3 2 output should be: orderid 100I want the orderids which has gotproductids 1 and 2 both for that particular order. Like for example,from the abovetable only orderid 100 has productids1 and 2. I want the output as 100. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-13 : 12:13:27
|
select distinct orderidfrom table t1where productid=1and exists(select * from table t2 where t2.orderid=t1.orderid and t2.productid=2) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-13 : 15:11:29
|
| [code]select orderidfrom table where productid in (1,2)group by orderidhaving min(productid) <> max(productid)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-03-13 : 15:28:00
|
| SELECT OrderIDFROM TableGROUP BY orderidhaving SUM( case when ProductId = 1 THEN 1 ELSE 0 END) > 0 and SUM( case when ProductId = 2 THEN 1 ELSE 0 END) > 0JimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-13 : 15:53:29
|
this variant also worksselect orderidfrom table where productid in (1,2)group by orderidhaving count( distinct productid) =2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|