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 > Max/Min dates from records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-31-06, 04:10
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Post Max/Min dates from records

Code:
+----+------------+
| id | date       |
+----+------------+
|  0 | 2006-01-01 |
|  1 | 2006-01-10 |
|  2 | 2006-01-15 |
|  3 | 2006-01-05 |
|  4 | 2006-01-03 |
|  5 | 2006-01-02 |
+----+------------+
I'd like to get the id for the last date.
Maybe by doing something like that:
Code:
SELECT
 t.id
FROM
 table AS t
WHERE
 t.date = MAX(t.date)
This query is not valid.
How can I do it ?
Reply With Quote
  #2 (permalink)  
Old 01-31-06, 04:16
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Do I have to do
Code:
SELECT
 t.id
FROM
 table AS t
ORDER BY
 t.date DESC
LIMIT 1
?
Reply With Quote
  #3 (permalink)  
Old 01-31-06, 05:11
MoonCrawler MoonCrawler is offline
Registered User
 
Join Date: Dec 2005
Location: Arnhem, Gld, NL
Posts: 21
i thought this could work,..
Code:
select id from (select max(date) from table)
Reply With Quote
  #4 (permalink)  
Old 01-31-06, 05:28
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,258
your could try the "top" clause in the select
with an order by <mycolumn> DESC;

select top 1 <mydatecolumn> from <mytable> order by <mydatecolumn> DESC;
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 01-31-06, 09:03
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Are you sure that TOP is implemented in MySQL ?
Ok now let's imagine that content
Code:
+----+------------+------+
| id | date       | user |
+----+------------+------+
|  0 | 2006-01-01 |    5 |
|  1 | 2006-01-10 |    6 |
|  2 | 2006-01-15 |    2 |
|  3 | 2006-01-05 |    2 |
|  4 | 2006-01-03 |    6 |
|  5 | 2006-01-02 |    0 |
+----+------------+------+
How can we get the id for the last date for each user.
Which should give us:
Code:
+----+------+
| id | user |
+----+------+
|  0 |    5 |
|  1 |    6 |
|  2 |    2 |
|  5 |    0 |
+----+------+
Now I'm not sure that this query works:
Code:
SELECT DISTINCT
 t.id AS "id",
 t.user AS "user"
FROM
 table AS t
ORDER BY
 t.date
Reply With Quote
  #6 (permalink)  
Old 03-29-07, 09:02
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Isn't the question clear ?
Reply With Quote
  #7 (permalink)  
Old 03-29-07, 11:51
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
steer clear of distinct whereever possible:

SELECT id,user
FROM <table> t
WHERE t.date=(SELECT MAX(date) FROM <table> WHERE id=t.id)


WHAT IS THE NAME OF YOUR TABLE? IT'S NOT "TABLE" IS IT?
in the sql above replace <table> with the actual name of your table.
Reply With Quote
  #8 (permalink)  
Old 03-30-07, 05:34
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Ok so we are obliged to do a sub-select to get this.
Reply With Quote
  #9 (permalink)  
Old 03-30-07, 05:58
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
You're not obliged to do anything of course. I'm just writing out your query another way ... Using a correlated subquery.

I think another way to do this would be to use a GROUP BY clause:

SELECT id,user,MAX(date)
FROM <table> t
GROUP BY date,user
Reply With Quote
  #10 (permalink)  
Old 03-30-07, 06:02
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Actually having tested that query above I can say it doesn't work... Actually neither of the queries I supplied above work correctly. It's always handy to try these things out
Reply With Quote
  #11 (permalink)  
Old 03-30-07, 06:07
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
SELECT id,user,MAX(date)
FROM <table> t
GROUP BY user
ORDER BY t.date

That works ok and doesn't have a distinct in it...
Reply With Quote
  #12 (permalink)  
Old 03-30-07, 06:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by gtk
Ok so we are obliged to do a sub-select to get this.
what's the problem, are you not on 4.1 yet? if not, why not?
Code:
select t1.id 
     , t1.date
     , t1.user
  from daTable as t1
inner
  join daTable as t2
    on t2.user = t1.user
group
    by t1.id 
     , t1.date
     , t1.user
having t1.id = max(t2.id)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 03-30-07, 06:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by aschk
That works ok and doesn't have a distinct in it...
no, it only appears to work okay

it's actually invalid, and the value of the id is unpredictable

see GROUP BY and HAVING with Hidden Fields

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 03-30-07, 06:12
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Here is the CORRECT correlated query:

SELECT id,user
FROM <table> t
WHERE t.date=(SELECT MAX(date) FROM <table> where `user`=t.`user`);

That's what I should have written the first time. I was comparing on id and it should have been user... silly me.

Edit: Using a BTREE index on user and date (ALTER TABLE ADD INDEX(`user`,`date`)) improves correlated subquery causing ONLY 1 row lookup.

Last edited by aschk; 03-30-07 at 06:22.
Reply With Quote
  #15 (permalink)  
Old 03-30-07, 06:27
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Quote:
select t1.id
, t1.date
, t1.user
from daTable as t1
inner
join daTable as t2
on t2.user = t1.user
group
by t1.id
, t1.date
, t1.user
having t1.id = max(t2.id)
This query gives the wrong result. I believe he is looking for the ID and User where the date is greatest. Not where the ID is greatest.
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