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 |
|
lane0618
Posting Yak Master
134 Posts |
Posted - 2007-05-16 : 16:19:19
|
| Below is a challenge I have tried to solve a couple of different ways, but it seems there must be an easier way.Here is my table structure/example data:productID, productAttribute1,1001,1051,1062,1062,1003,1093,1103,111I want to:select productID that have (productAttribute = 100 and productAttribute = 106) ...so I need to group by productID and find only the products that have both attributes.Thanks in Advance!dougCreate a Family Website! Share Photos, News, Polls, Calendar, Address Book and more! Visit www.familydetails.com for a free 30-day trial. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-16 : 16:44:16
|
| select t1.productidfrom (select productid from table1 where product attribute = 100) as t1inner join (select productid from table1 where product attribute = 106) as t2 on t2.productid = t1.prodctidPeter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-05-16 : 16:44:58
|
| select productIDfrom yourtablewhere productAttribute in (100,106)group by productIDhaving Count(*) = 2- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-16 : 16:48:38
|
| [code]select productidfrom ( select productid, max(case when productattribute = 100 then 1 else 0 end) as pa100, max(case when productattribute = 106 then 1 else 0 end) as pa100 from table1 group by productid ) as dwhere pa100 = 1 and pa106 = 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-17 : 02:30:40
|
| I agrees, until there is a duplicate of {1,106} in which your suggestion fails.OP didn't state if there is a possibility of this. If there is not, your suggestion is the best.If there is, my will work.Or a rewrite of yours will handle all cases.select productIDfrom yourtablewhere productAttribute in (100,106)group by productIDhaving Count(distinct productAttribute) = 2Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-05-17 : 08:13:01
|
| You are correct, it all depends on the table structure. we have no idea, because, unfortunately like usual, it was not indicated.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|
|
|
|
|