If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with DISTINCT query - should be easy (noob)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-14-04, 21:35
Atari Atari is offline
Registered User
 
Join Date: Nov 2004
Posts: 35
Help with DISTINCT query - should be easy (noob)

Hello,

I have a table with three columns of interest:

type int(1)
value int(3)
time_accessed timestamp(14)

Here is some sample data:

Quote:
type, value, time_accessed
1, 3, 20040611154031
0, 3, 20040611154000
0, 533, 20040601154031
1, 533, 20030611154031
Now, what I'd like to have, is a single query, that returns DISTINCT 'value', and only latest type per value, ex return:

type, value:
1, 3
0, 533

Anyone know how this is done? My instinct was:

SELECT DISTINCT value, type FROM table_a ORDER BY time_accessed DESC;

The problem however, is that this returns each 'type' entry..

Help appreciated!
A
Reply With Quote
  #2 (permalink)  
Old 11-14-04, 23:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select t1.type
     , t1.value
  from table_a as t1
inner
  join table_a as t2
    on t1.value
     = t2.value
group
    by t1.type
     , t1.value
having t1.time_accessed
     = max(t2.time_accessed)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-14-04, 23:40
Atari Atari is offline
Registered User
 
Join Date: Nov 2004
Posts: 35
Wow... I must learn GROUP BY and HAVING

The above gives me this error:
ERROR 1054: Unknown column 't1.time_accessed' in 'having clause'

My intuition would have me add t1.time_accessed to the SELECT clause, but then that makes the query take centuries. I've not been patient enough to wait for a return. The table has only 1600 rows.
Reply With Quote
  #4 (permalink)  
Old 11-14-04, 23:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
sorry

you ahve to add it to both the SELECT and the GROUP BY
Code:
select t1.type
     , t1.value
     , t1.time_accessed
  from table_a as t1
inner
  join table_a as t2
    on t1.value
     = t2.value
group
    by t1.type
     , t1.value
     , t1.time_accessed
having t1.time_accessed
     = max(t2.time_accessed)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-14-04, 23:55
Atari Atari is offline
Registered User
 
Join Date: Nov 2004
Posts: 35
Yes, that's what I last tried - that query sends the server for a loop
Reply With Quote
  #6 (permalink)  
Old 11-15-04, 00:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
do you have an index on time_accessed?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On