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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help with query (join / unique)

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 :)
mike123


SELECT distinct(OI.orderID) FROM tblOrders O
JOIN tblordereditems OI on OI.orderID = O.orderID
WHERE 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]

GO

CREATE 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 duplicates
What is wrong with distinct?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -