Jump to content

Show records from the most recent date only.


suttercain

Recommended Posts

Hi everyone,

 

Please bear with me as I try and write this in a concise manner. I have three tables:

 

TABLE A

| group | name  |

----------------------

| 1        | Yellow |

| 2        | Yellow |

| 3        | Blue    |

----------------------

 

TABLE B

| group | code | date_created |

---------------------------------------

| 1        | j7uw | 2009-10-10

| 1        | op0i  | 2009-10-10

| 1        | hw7i | 2009-10-09

| 1        | usp1 | 2009-10-10

| 2        | eiu2  | 2010-07-21

| 2        | y9oi  | 2009-12-25

| 2        | j7uw | 2009-12-25

---------------------------------------

 

TABLE C

| code | size |

------------------

| j7uw | Large

| op0i  | Large

| hw7i | Medium

| usp1 | Small

| eiu2  | Medium

| y9oi  | XLarge

| j7uw | Large

------------------

 

What I'm doing is an INNER JOIN on these tables:

SELECT *
FROM table_a
INNER JOIN table_b (table_a.group = table_b.group)
INNER JOIN table_c (table_b.code = table_c.code)
WHERE table_b.group = 1

 

With the above query I'll get this expected output:

 

| 1        | j7uw | 2009-10-10 | Yellow | Large

| 1        | op0i  | 2009-10-10 | Yellow | Large

| 1        | hw7i | 2009-10-09 | Yellow | Medium

| 1        | usp1 | 2009-10-10 | Yellow | Small

 

My desired results however are only the MOST RECENT date_created from the date column based on the selected group. Meaning, I'd like to get these results instead:

 

| 1        | j7uw | 2009-10-10 | Yellow | Large

| 1        | op0i  | 2009-10-10 | Yellow | Large

| 1        | usp1 | 2009-10-10 | Yellow | Small

 

Because the other date was October, 9 2009 and "newer" date(s) have occured within that group, that record(s) will be omitted.

 

So if tomorrow a new record is added with GROUP 1 and a date of 2009-10-11, only that newest record will appear. There may also be multiple records with the same date and group and I need them all to display.

 

Any ideas?

 

Thanks ahead of time.

 

-SC

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.