Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 2nd, 2004, 10:02 AM
spackbace spackbace is offline
graphics monkey
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Georgia Tech, Atlanta, GA
Posts: 71 spackbace User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 21 sec
Reputation Power: 7
Send a message via AIM to spackbace
selecting all fields while using distinct ?

I am trying to select non-duplicate records from a table, however I still need to have ALL fields in the record returned. Here is the SQL I have right now:

select distinct LogID from adjustments order by LogID

This works fine as far as only getting records with non-duplicated LogIDs but does not return all the fields in each record! How can I select all the records that have distinct LogID's but have all the fields returned? I tried something like this

select distinct LogID, * from adjustments order by LogID

but this does not work, it still returns ALL records instead of only the distinct ones. what should I do?
__________________
Brian

Reply With Quote
  #2  
Old January 2nd, 2004, 10:17 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,690 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 20 m
Reputation Power: 985
what should you do? heh

DISTINCT applies to every column in the SELECT list

obviously, what you want is those rows that have the same LogID, but perhaps something else different

the only time you'd want SELECT DISTINCT * is when you wanted completely unique rows, in a situation where there can be completely duplicate rows, which usually never happens, especially if you're using an auto_increment anywhere

anyhow, try this:
Code:
select * 
  from adjustments 
 where LogID
    in ( select LogID
           from adjustments
         group
             by LogID
         having count(*) > 1 )       
rudy
http://r937.com/

Reply With Quote
  #3  
Old January 7th, 2004, 09:49 PM
clutions's Avatar
clutions clutions is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Edwardsville, IL
Posts: 5 clutions User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nice ... but even shorter is:


select *
from adjustments
where LogID in ( select distinct LogID from adjustments )

Reply With Quote
  #4  
Old January 7th, 2004, 09:55 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,690 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 20 m
Reputation Power: 985
sorry, Francis, but that will return them all

i'm glad you posted because i took another look at my solution, and it's wrong, it gets all the rows of duplicated logins, whereas Brian wanted all the rows of non-duplicated logins

Brian, change the greater than sign in my solution to equals

Reply With Quote
  #5  
Old January 8th, 2004, 07:12 AM
clutions's Avatar
clutions clutions is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Edwardsville, IL
Posts: 5 clutions User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OOPS!

Thanks Rudy ... Can't imagine where I was!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > selecting all fields while using distinct ?


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 6 hosted by Hostway