How to get latest date for each distinct entry?

This is a discussion on How to get latest date for each distinct entry? within the MySQL Database forums, part of the Database Forums category; Hi, I have a table called "obs" with the following: observation INT AUTO_INCREMENT, morph VARCHAR, date DATE, plot ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-18-2008
Plantky
 
Posts: n/a
Default How to get latest date for each distinct entry?

Hi,

I have a table called "obs" with the following:

observation INT AUTO_INCREMENT,
morph VARCHAR,
date DATE,
plot VARCHAR,
number INT,
comment TEXT,
week VARCHAR

observation is the primary key, and morph is a foreign key.

How do I make a query such that I get all the latest observation
'dates' of every distinct 'morph'?
Reply With Quote
  #2 (permalink)  
Old 01-18-2008
Captain Paralytic
 
Posts: n/a
Default Re: How to get latest date for each distinct entry?

On 18 Jan, 14:55, Plantky <ngokang...@gmail.com> wrote:
> Hi,
>
> I have a table called "obs" with the following:
>
> observation INT AUTO_INCREMENT,
> morph VARCHAR,
> date DATE,
> plot VARCHAR,
> number INT,
> comment TEXT,
> week VARCHAR
>
> observation is the primary key, and morph is a foreign key.
>
> How do I make a query such that I get all the latest observation
> 'dates' of every distinct 'morph'?


Search this group for "strawberry query"
Also read
http://dev.mysql.com/doc/refman/5.0/...group-row.html
(you want the LEFT JOIN example)
Reply With Quote
  #3 (permalink)  
Old 01-18-2008
Luuk
 
Posts: n/a
Default Re: How to get latest date for each distinct entry?


"Captain Paralytic" <paul_lautman@yahoo.com> schreef in bericht
news:b39176b1-e71d-4439-b09e-c08d0bebfaf5@d21g2000prf.googlegroups.com...
> On 18 Jan, 14:55, Plantky <ngokang...@gmail.com> wrote:
>> Hi,
>>
>> I have a table called "obs" with the following:
>>
>> observation INT AUTO_INCREMENT,
>> morph VARCHAR,
>> date DATE,
>> plot VARCHAR,
>> number INT,
>> comment TEXT,
>> week VARCHAR
>>
>> observation is the primary key, and morph is a foreign key.
>>
>> How do I make a query such that I get all the latest observation
>> 'dates' of every distinct 'morph'?

>
> Search this group for "strawberry query"
> Also read
> http://dev.mysql.com/doc/refman/5.0/...group-row.html
> (you want the LEFT JOIN example)



why not do:
SELECT morph, max(date) date FROM obs GROUP BY morph


Reply With Quote
  #4 (permalink)  
Old 01-18-2008
Rik Wasmus
 
Posts: n/a
Default Re: How to get latest date for each distinct entry?

On Fri, 18 Jan 2008 16:57:18 +0100, Luuk <luuk@invalid.lan> wrote:
> "Captain Paralytic" <paul_lautman@yahoo.com> schreef in bericht
> news:b39176b1-e71d-4439-b09e-c08d0bebfaf5@d21g2000prf.googlegroups.com...
>> On 18 Jan, 14:55, Plantky <ngokang...@gmail.com> wrote:
>>> Hi,
>>>
>>> I have a table called "obs" with the following:
>>>
>>> observation INT AUTO_INCREMENT,
>>> morph VARCHAR,
>>> date DATE,
>>> plot VARCHAR,
>>> number INT,
>>> comment TEXT,
>>> week VARCHAR
>>>
>>> observation is the primary key, and morph is a foreign key.
>>>
>>> How do I make a query such that I get all the latest observation
>>> 'dates' of every distinct 'morph'?

>>
>> Search this group for "strawberry query"
>> Also read
>> http://dev.mysql.com/doc/refman/5.0/...group-row.html
>> (you want the LEFT JOIN example)

>
>
> why not do:
> SELECT morph, max(date) date FROM obs GROUP BY morph


Could be done, but not if you want the field 'observation' in there too.
So either solution could be valid, depending on what the OP really needs.
--
Rik Wasmus
Reply With Quote
  #5 (permalink)  
Old 01-18-2008
Plantky
 
Posts: n/a
Default Re: How to get latest date for each distinct entry?

Thanks Luuk, it works!!
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0