January 25, 2010 at 9:48 am
I have a table called DGUsers. This table has a primary key of UserID. I have another table called Orders that has a field called UserID. I need to get a list of the DG Users that have placed orders. It seemed pretty easy. I therefore wrote the following query which seems to work. Something was bothering me about the query so I started to poke around the internet. I read a little about multi column query problems using distinct. Now I am not sure if the query is correct. Can anyone tell me if my query is correct or not?
Select Distinct(DG.UserID)
, DG.UserName
, DG.LastName
, DG.FirstName
From DGUsers DG
Join Orders O
On O.UserID = DG.UserID
Order by DG.UserID
Thanks
January 25, 2010 at 9:58 am
Thread continues - http://www.sqlservercentral.com/Forums/Topic853116-1291-1.aspx
No replies here please.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 25, 2010 at 9:58 am
Let me know if the below script works better...
Select Distinct(DG.UserID)
, DG.UserName
, DG.LastName
, DG.FirstName
From DGUsers DG
where DG.UserID in (select distinct UserId from Orders)
Order by DG.UserID
The_SQL_DBA
MCTS
"Quality is never an accident; it is always the result of high intention, sincere effort, intelligent direction and skillful execution; it represents the wise choice of many alternatives."
January 25, 2010 at 9:59 am
Sorry Gail..I guess i posted my reply about the same time as you asked us to redirect our replies..
The_SQL_DBA
MCTS
"Quality is never an accident; it is always the result of high intention, sincere effort, intelligent direction and skillful execution; it represents the wise choice of many alternatives."
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply