dbTalk Databases Forums  

Select Distinct Help

comp.databases.mysql comp.databases.mysql


Discuss Select Distinct Help in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bruce
 
Posts: n/a

Default Select Distinct Help - 11-21-2010 , 01:35 PM






I want to return the primary key (keyID) and a text field(name) where
the text field is Distinct. This would look like: SELECT keyID,
DISTINCT name FROM mytable.. This doesn't work, so I tried
SELECT keyID, name FROM mytable WHERE name=(SELECT DISTINCT name FROM
mytable).

Again, no joy!!

I've run out of ideas. So, suggestions please??

Thanks...

Bruce

Reply With Quote
  #2  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 01:46 PM






On Nov 21, 7:35*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:
Quote:
I want to return the primary key (keyID) and a text field(name) where
the text field is Distinct. *This would look like: SELECT keyID,
DISTINCT name FROM mytable.. *This doesn't work, so I tried
SELECT keyID, name FROM mytable WHERE name=(SELECT DISTINCT name FROM
mytable).

Again, no joy!!

I've run out of ideas. *So, suggestions please??

Thanks...

Bruce
You need to tell us one more piece of information. Suppose your table
was like this:
keyID name
1 Fred
2 Harry
3 Fred
4 Jim
5 Fred
6 Harry

We know that you want 3 records (one each of Fred, Harry, Jim), but
how are you choosing which keyID you wish to be returned for each of
the non-unique names?

Reply With Quote
  #3  
Old   
bruce
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:00 PM



On Nov 21, 2:46*pm, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:
Quote:
On Nov 21, 7:35*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:

I want to return the primary key (keyID) and a text field(name) where
the text field is Distinct. *This would look like: SELECT keyID,
DISTINCT name FROM mytable.. *This doesn't work, so I tried
SELECT keyID, name FROM mytable WHERE name=(SELECT DISTINCT name FROM
mytable).

Again, no joy!!

I've run out of ideas. *So, suggestions please??

Thanks...

Bruce

You need to tell us one more piece of information. Suppose your table
was like this:
keyID name
1 * * * Fred
2 * * * Harry
3 * * * Fred
4 * * * Jim
5 * * * Fred
6 * * * Harry

We know that you want 3 records (one each of Fred, Harry, Jim), but
how are you choosing which keyID you wish to be returned for each of
the non-unique names?
Thanks for the response. The keyID I want is the one for the DISTINCT
Fred, Harry, Jim found,

Assuming the FIRST one found is the one that is kept as the DISTINCT
one, my result rows would be
{1,Fred}, {2, Harry} and {4,Jim}.

Okay?

Bruce

Reply With Quote
  #4  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:07 PM



On Nov 21, 8:00*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:
Quote:
On Nov 21, 2:46*pm, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:





On Nov 21, 7:35*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:

I want to return the primary key (keyID) and a text field(name) where
the text field is Distinct. *This would look like: SELECT keyID,
DISTINCT name FROM mytable.. *This doesn't work, so I tried
SELECT keyID, name FROM mytable WHERE name=(SELECT DISTINCT name FROM
mytable).

Again, no joy!!

I've run out of ideas. *So, suggestions please??

Thanks...

Bruce

You need to tell us one more piece of information. Suppose your table
was like this:
keyID name
1 * * * Fred
2 * * * Harry
3 * * * Fred
4 * * * Jim
5 * * * Fred
6 * * * Harry

We know that you want 3 records (one each of Fred, Harry, Jim), but
how are you choosing which keyID you wish to be returned for each of
the non-unique names?

Thanks for the response. *The keyID I want is the one for the DISTINCT
Fred, Harry, Jim found,

Assuming the FIRST one found is the one that is kept as the DISTINCT
one, my result rows would be
{1,Fred}, {2, Harry} and {4,Jim}.

Okay?

Bruce
My question was "Which one do you want to be found?". Do you want the
first one or do you want the last one or do you want some other one.

If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.

Reply With Quote
  #5  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:10 PM



Captain Paralytic:

Quote:
If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.
Is a strawberry query really necessary, here?
Why not something like:

select name, max(keyID) as MaxKey
from mytable
group by name
order by name



--
Erick

Reply With Quote
  #6  
Old   
bruce
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:18 PM



On Nov 21, 3:07*pm, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:
Quote:
On Nov 21, 8:00*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:



On Nov 21, 2:46*pm, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:

On Nov 21, 7:35*pm, bruce <bruc... (AT) bellsouth (DOT) net> wrote:

I want to return the primary key (keyID) and a text field(name) where
the text field is Distinct. *This would look like: SELECT keyID,
DISTINCT name FROM mytable.. *This doesn't work, so I tried
SELECT keyID, name FROM mytable WHERE name=(SELECT DISTINCT name FROM
mytable).

Again, no joy!!

I've run out of ideas. *So, suggestions please??

Thanks...

Bruce

You need to tell us one more piece of information. Suppose your table
was like this:
keyID name
1 * * * Fred
2 * * * Harry
3 * * * Fred
4 * * * Jim
5 * * * Fred
6 * * * Harry

We know that you want 3 records (one each of Fred, Harry, Jim), but
how are you choosing which keyID you wish to be returned for each of
the non-unique names?

Thanks for the response. *The keyID I want is the one for the DISTINCT
Fred, Harry, Jim found,

Assuming the FIRST one found is the one that is kept as the DISTINCT
one, my result rows would be
{1,Fred}, {2, Harry} and {4,Jim}.

Okay?

Bruce

My question was "Which one do you want to be found?". Do you want the
first one or do you want the last one or do you want some other one.

If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.
I don't really care which one is found. Either is fine..

Not sure what the "Strawberry Query" is but I'll search the discussion
group.

Thanks...

Reply With Quote
  #7  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:19 PM



On Nov 21, 8:10*pm, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m>
wrote:
Quote:
Captain Paralytic:

If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.

Is a strawberry query really necessary, here?
Why not something like:

select name, max(keyID) as MaxKey
from mytable
group by name
order by name

--
Erick
Well...
Apart from the fact that I would write this as

SELECT
name,
min( keyID ) keyID
FROM mytable
GROUP BY name
ORDER BY name

In order to (a) keep it tidy and (b) give his suggested results, I
would still go for the Strawberry Query in order to not hit troubles
when another column is added to the query (as it inevitably is).

The Strawberry Query version would be:

SELECT
a.keyID,
a.name
FROM mytable a
LEFT JOIN mytable b ON a.name = b.name AND a.keyID > b.keyID
WHERE b.name IS NULL

Reply With Quote
  #8  
Old   
bruce
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:26 PM



On Nov 21, 3:10*pm, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m>
wrote:
Quote:
Captain Paralytic:

If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.

Is a strawberry query really necessary, here?
Why not something like:

select name, max(keyID) as MaxKey
from mytable
group by name
order by name

--
Erick
This does exactly what I want now I need to study it and understand
what you are doing. I'm novice enough to have never done a select like
this..

Thanks...

Reply With Quote
  #9  
Old   
bruce
 
Posts: n/a

Default Re: Select Distinct Help - 11-21-2010 , 02:31 PM



On Nov 21, 3:19*pm, Captain Paralytic <paul_laut... (AT) yahoo (DOT) com> wrote:
Quote:
On Nov 21, 8:10*pm, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m
wrote:



Captain Paralytic:

If you want the first or the last one, then the answer is the
ubiquitous "Strawberry Query" which you will see many times in this
group.

Is a strawberry query really necessary, here?
Why not something like:

select name, max(keyID) as MaxKey
from mytable
group by name
order by name

--
Erick

Well...
Apart from the fact that I would write this as

SELECT
*name,
*min( keyID ) keyID
FROM mytable
GROUP BY name
ORDER BY name

In order to (a) keep it tidy and (b) give his suggested results, I
would still go for the Strawberry Query in order to not hit troubles
when another column is added to the query (as it inevitably is).

The Strawberry Query version would be:

SELECT
* a.keyID,
* a.name
FROM mytable a
LEFT JOIN mytable b ON a.name = b.name AND a.keyID > b.keyID
WHERE b.name IS NULL
Okay. I think I understand what is being done. The select will FIRST
group the output by name, then order each name within the group. Once
that is done, the SELECT will retrieve the record for each name having
the Maxid (From Erick) or the Minid(from yours) This gives me what I
want.

Thanks....

Reply With Quote
Reply




Thread Tools
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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.