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 |
|
ckuo@kahluadesigns.com
Yak Posting Veteran
58 Posts |
Posted - 2002-08-19 : 20:11:41
|
| Hi,I have two tables:PACKAGESPackageID-----ProductCode1-------------Test1011-------------Test102CARTSPackageID-----ProductCode1-------------Test101How can I select items in Packages that are not in Carts that have the same PackageID? So I would like to return Test102. Thanks |
|
|
simondeutsch
Aged Yak Warrior
547 Posts |
Posted - 2002-08-19 : 21:34:56
|
| Hi,WillSELECT * FROM Packages INNER JOIN Carts ON Carts.PackageID <> Packages.PackageIDdo?Or you can doSELECT * FROM Packages WHERE PackageID NOT IN(SELECT PackageID FROM Carts) if the Carts table is not very large.Sarah Berger MCSD |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2002-08-20 : 20:07:24
|
You TALK about PackageID, but the sample data you show says that you want to return ProductCodes that don't match... So, based on your sample data, try this:SET NOCOUNT ONCREATE TABLE #Packages ( PackageID int, ProductCode varchar(10))GOCREATE TABLE #Carts ( PackageID int, ProductCode varchar(10))GOINSERT INTO #Packages VALUES (1, 'Test101')INSERT INTO #Packages VALUES (1, 'Test102')INSERT INTO #Carts VALUES (1, 'Test101')GOSET NOCOUNT OFFSELECT * FROM #Packages PWHERE P.ProductCode <> ( SELECT C.ProductCode FROM #Carts C WHERE C.PackageID = P.PackageID )DROP TABLE #PackagesDROP TABLE #CartsThe part in red is the key, the rest is just sample tables to test my work. You'll need to test this with a larger set of data to determine if it is restrictive enough. It works with the minute dataset you provided though. Edited by - ajarnmark on 08/20/2002 20:08:01 |
 |
|
|
|
|
|