Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Select Distinct

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-07-03, 06:45
ACOLLINS ACOLLINS is offline
Registered User
 
Join Date: May 2003
Posts: 2
Select Distinct

I am using SQL QueryAnalyser, and need to select from a list of people in a table with fields
name
address1
address2
county
postcode
..
..
.
but i have different people at the same address and want to select on distinct postcode.
I have used SELECT DISTINCT postcode from table but this only gives me a list of the postcodes how do i get the rest of the info, I do not care which of the duplicates i get.

Thanks
Reply With Quote
  #2 (permalink)  
Old 05-07-03, 07:02
alligatorsql.com alligatorsql.com is offline
Registered User
 
Join Date: Jul 2001
Location: Germany
Posts: 189
Distinct

Hello,

which of the datas do you want to select ?
DISTINCT will show you only the unique entries of the field - in your example the postcode.
Oracle have the same problem than you have. How will the database decide, which postcode is the correct postcode for the people ?

Or do I get something wrong ?

Hope that helps ?

Manfred Peter
(Alligator Company Software GmbH)
http://www.alligatorsql.com
Reply With Quote
  #3 (permalink)  
Old 05-07-03, 07:05
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Select Distinct

The problem is, 2 or more people at the same postcode are NOT duplicates, so how can the DBMS decide which person to return? If you just want one arbitrary person per postcode, then you can do this:

SELECT postcode, MIN(name)
FROM people
GROUP BY postcode;

That gets you one person per postcode (the one with the "minimum" name!) But you are still missing the address details. You can't use MIN on those fields as well, otherwise you will end up with a dog's breakfast of values from different people. So you do this:

SELECT name, address1, address2, county, postcode
FROM people
WHERE (postcode, name) IN
( SELECT postcode, MIN(name)
FROM people
GROUP BY postcode
);
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #4 (permalink)  
Old 05-07-03, 08:20
ACOLLINS ACOLLINS is offline
Registered User
 
Join Date: May 2003
Posts: 2
Thanks for the reply, the first part solved the problem i had. It would be nice to get the second bit working but i get an error
Line 3: Incorrect syntax near ','.


SELECT *
FROM tablename
WHERE (ucn, pc) IN
( SELECT min(ucn), pc
FROM tablename
GROUP BY pc)

regards
Reply With Quote
  #5 (permalink)  
Old 05-07-03, 08:34
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Seems your DBMS can't handle multiple columns in the IN clause - I know Oracle can.

How about an in-line view?

SELECT *
FROM tablename t,
( SELECT min(ucn) AS minucn, pc
FROM tablename
GROUP BY pc) v
WHERE v.minucn = t.ucn
AND v.pc = t.pc;
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On