-1

My table is setup like

site    date     level
 a    2014-4-09   80%
 a    2014-8-15   70%
 b    2014-5-23   95%
 c    2014-1-20   NULL

I want to select any thing that level IS NOT NULL and the most current date for each site. I have tried max(date) but that just gets me a single date from the table.

I would like to see as a result the most recent record for each site.

 a 2014-8-15 70%
 b 2014-5-23 95%
Scott
  • 3
  • 4

1 Answers1

0

Add GROUP BY site, like:

SELECT *
FROM tbl
WHERE level IS NOT NULL
GROUP BY site
ORDER BY date DESC

That should give the correct result.

You can do it in 1 query.

SELECT *
FROM tbl
WHERE level IS NOT NULL
ORDER BY date DESC
LIMIT 1

That will give you 1 row with the highest date where level is not null.

rdyhalt
  • 96
  • 2
  • 8
  • This returns only the most recent record. I need to most recent record for each site. Thanks for the help! – Scott Feb 13 '15 at 17:43