Unix Technical Forum

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 forums, part of the Database Server Software category; --> Hi, I have a table called "obs" with the following: observation INT AUTO_INCREMENT, morph VARCHAR, date DATE, plot VARCHAR, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:32 AM
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'?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:32 AM
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:32 AM
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:32 AM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:32 AM
Plantky
 
Posts: n/a
Default Re: How to get latest date for each distinct entry?

Thanks Luuk, it works!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:21 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com