Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 7th, 2004, 04:30 PM
dmors dmors is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Vancouver, BC
Posts: 2 dmors User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question DISTINCT syntax; selecting one record from multiple

Hopefully this question is not horribly out of place, and hopefully hasn't been answered countless times in the last day. I've searched this forum and several other places, but can't seem to find the right solution.

I have three tables "items", "dates", "membership", and "collection".

"items" joins "dates" on Id, a unique identifier. "collection" defines certain collections of "items", each has a unique collection_id. So "membership" has an Id field, and a collection_id field. Each "items" record can belong to multiple "collection"s.

The query:

select a.Id, a.Name, b.Date from items as a LEFT JOIN dates as b on (a.Id = b.Id) LEFT JOIN membership as c on (a.Id = c.Id) LEFT JOIN collection as d on (c.collection_id = d.collection_id)

returns the expected results, but I need to only get ONE of each "items" record, regardless of how many memberships it has. The membership record it returns is unimportant. I tried DISTINCT, but I'm not sure I have the syntax down right, and can't seem to find documentation or a sample that applies to this situation.

If anyone has any suggestions, I would love to hear them and appreciate it very much. Thank you.


D. Mors

Reply With Quote
  #2  
Old January 7th, 2004, 06:00 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
To use DISTINCT, apply it to the column in the SELECT list that you want only one of. In this case,
Code:
SELECT DISTINCT(a.Id), a.Name, b.Date...
Continue the SQL query as usual.

One small suggestion on table aliases - the same "rules" apply as programming - use variables (i.e. table aliases) that mean something. e.g. items as i

Reply With Quote
  #3  
Old January 8th, 2004, 10:10 AM
dmors dmors is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Vancouver, BC
Posts: 2 dmors User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Stumpy,
Thanks for the reply. Unfortunately, that did not seem to work. Mayhaps I'm doing something else wrong... I'll have to scrutinize my query and see what may be amiss.

I have read elsewhere that DISTINCT applies to all the columns in the SELECT statement, so I'm wondering if selecting certain columns in parenthesis is only valid in certain servers. It does not throw an error or exception, so I assume my server (MSSQL Server... 2000, I think) is accepting it.

I'm at a loss. Any other possible solutions? Thanks again for your help.

Reply With Quote
  #4  
Old January 8th, 2004, 06:51 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Another suggestion - if you can, use a server-side language to filter the recordset.
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #5  
Old January 9th, 2004, 07:42 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Look into grouping results as well. That may net some weird results, but I've had some luck using that to select distinct rows that included timestamps but in which some of the non-timestamp fields were identical and needed not to be duplicated.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > DISTINCT syntax; selecting one record from multiple


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT