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 |
|
kamau
Starting Member
2 Posts |
Posted - 2008-03-11 : 10:53:12
|
Hi, my name's John and this is my first post here at SQL team. I've learnt a lot from the forums here, never needed to post though. I'm pretty good at sql, but not that good and I think for the first time in couple of years I now have a query that I'm completely unable to create.Let me explain the setup.I've got three tables e.g.Products (productid, Name)100 Computer 200 Printer 300 PDA ProductActions (ID, Name)6 changed casing8 Changed motherboard7 replaced cd-driveProductPerformedActions (productid, actionid)100,7100,9200,8This basically shows a simple setup showing that product (computer) has been done the action "repaired" and product (printer) has been done the action "changed motherboard". The query i'd like to have is where I can select all products that have not been performed on cerrtain actions. e.g. a list of products that have not been performed on all actions, such asUnPerformedActionsList(product,actionid)PDA,6PDA,7PDA,8Printer,7Printer,6Computer,8I know its foundation is something like this:select productid from products where productid not in (select productid from productperformedactions.... which elimiantes products that have been performed an action on, but it also removes it for ALL actions,which i'm trying to avoid. All i want is kind of a cross join, listing all actions, then inverting it, to showall products that have NOT had it performed on them.I hope this makes sense.ThanksJohn |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-11 : 10:57:42
|
[code]SELECT p.ProductID, pa.IDFROM Products AS pCROSS JOIN ProductActions AS pa[/code]will give you all combinations of Products and ProductActions.[code]SELECT p.ProductID, pa.IDFROM Products AS pCROSS JOIN ProductActions AS paLEFT JOIN ProductPerformedActions AS ppa ON ppa.ProductID = p.ProductID AND ppa.ActionID = pa.IDWHERE ppa.ProductID IS NULL[/code]will give you all combinations "minus" those already stored. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-11 : 11:00:04
|
| SELECT p.productid,pa.actionidFROM Products pCROSS JOIN ProductActions paLEFT OUTER JOIN ProductPerformedActions ppaON ppa.productid=p.productidAND ppa.actionid=pa.IDWHERE ppa.productid IS NULL |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-11 : 11:04:31
|
I think there is an internet echo somewhere...  E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-11 : 11:05:13
|
quote: Originally posted by Peso I think there is an internet echo somewhere...  E 12°55'05.25"N 56°04'39.16"
I have felt it myself a lot of times before |
 |
|
|
kamau
Starting Member
2 Posts |
Posted - 2008-03-11 : 11:08:36
|
| WOW! That has got to be the fastest set jof replies I've ever seen in my life.I just tested it out and it works perfect. Thank you Peso and Visakh16, your help ismuch appreciated. |
 |
|
|
|
|
|
|
|