How to get latest date for each distinct entry?

  • Follow


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'?
0
Reply Plantky 1/18/2008 2:55:03 PM

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/en/example-maximum-column-group-row.html
(you want the LEFT JOIN example)
0
Reply Captain 1/18/2008 2:59:58 PM


"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/en/example-maximum-column-group-row.html
> (you want the LEFT JOIN example)


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


0
Reply Luuk 1/18/2008 3:57:18 PM

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/en/example-maximum-column-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
0
Reply Rik 1/18/2008 4:18:19 PM

Thanks Luuk, it works!!
0
Reply Plantky 1/18/2008 4:19:32 PM

4 Replies
262 Views

(page loaded in 0.052 seconds)

Similiar Articles:













7/24/2012 10:49:44 AM


Reply: