birdy
birdy

Reputation: 9646

querying for multiple records by date

I've got few tables like this:

Color

id      Color_Name
---    -------
 1      RED
 2      GREEN

Color_Shades

id      ColorId    ShadeId    date_created
---     -------    -------   --------------
 1       1          55        03/15/2013
 2       1          43        02/01/2012
 3       2          13        05/15/2011
 4       2          15        06/11/2009

I'm trying to get a list of all distinct colors with their latest date.

I tried

SELECT a.Color_Name, b.date_created FROM Color a, Color_Shades b
WHERE a.id = b.ColorId

but this is giving me mixed results.

My desired results are:

 Color_Name   date_created
----------    ---------------
 RED              03/15/2013
 GREEN            05/15/2011

Upvotes: 1

Views: 56

Answers (1)

John Woo
John Woo

Reputation: 263893

You are near to what you need. You just need to aggregate those columns using MAX to get theor latest date.

SELECT  a.Color_name, MAX(b.date_created) date_created
FROM    Color a
        INNER JOIN Color_shades b
            ON a.id = b.colorID
GROUP   BY a.Color_Name

Upvotes: 3

Related Questions