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 |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2009-09-04 : 03:59:32
|
| Hi,I have the following query, I'm wondering if I am writing it properly. I just want a unique list of the ORDERID's , but I am having to use a distinct clause. Is there any way around this ?Any help greatly appreciated!Thanks once again :)mike123SELECT distinct(OI.orderID) FROM tblOrders OJOIN tblordereditems OI on OI.orderID = O.orderIDWHERE itemID NOT IN (SELECT itemID FROM tblItemAssignments)tables:CREATE TABLE [dbo].[tblOrders]( [orderID] [int] IDENTITY(1,1) NOT NULL, [clientID] [int] NOT NULL, [orderCode] [char](6) NOT NULL, [orderDate] [datetime] NOT NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[tblOrderedItems]( [itemID] [int] IDENTITY(1,1) NOT NULL, [orderID] [int] NOT NULL, [item_OrderDate] [datetime] NOT NULL, [item_OrderStatusID] [tinyint] NOT NULL) ON [PRIMARY]GO |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-04 : 04:13:35
|
| As, there can duplicate orderIDs in child table, you may get duplicatesWhat is wrong with distinct?MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-04 : 04:17:25
|
I think there is no way around DISTINCT.But I also think in given example there is no need to join tblOrders.SELECT distinct(OI.orderID) FROM tblordereditems OI WHERE itemID NOT IN (SELECT itemID FROM tblItemAssignments) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|