6

I have a table power with a datetimetz field called sample_time and a column called amp_hours.

The amp_hours field gains a record about every two minute and is reset every night at midnight.

I would like to see sample_time and amp_hours for the last record of every day.

I'm very new to SQL so I may be overlooking an obvious answer.

I saw this post on how to select the last record of a group but I'm not familiar enough with SQL to get it to work for datetimes:

I thought to use lead() or lag() to compare the date of a record with the next record but I'm using postgresql 8.3 and I think windowing was introduced in 8.4.

1 Answer 1

14

Try this:

SELECT DISTINCT ON (sample_time::date) sample_time, amp_hours
FROM power
ORDER BY sample_time::date DESC, sample_time DESC;
8
  • 1
    Nice solution, the first DESC is optional. Aug 14, 2012 at 16:56
  • Thanks, that is clever! If I understand this correctly, the DISTINCT ON (sample_time::date) makes sure only one record per day is returned and the ORDER BY does the rest. If I omit the second DESC i get the first record of every day. The first DESC just makes the most recent records first.
    – RyanN
    Aug 14, 2012 at 17:07
  • @RyanN: DISTINCT automatically orders by the (to be) distinct columns. You can add an explicit ORDER BY - that cannot disagree with DISTINCT - with additional columns and/or DESC instead of the default ASC for the distinct columns. Aug 14, 2012 at 17:22
  • 2
    @ErwinBrandstetter: DISTINCT can generate the distinct rows through different plans, only some of which leave the data in a predictable order. If it chooses to use a hash table instead of a sort, the order will be hard to predict. If you want results to be in some order, it is always a good idea to specify ORDER BY rather than relying on implementation details. @RyanN: Without the ORDER BY it might have returned the most recent rows this time; don't count on that.
    – kgrittn
    Aug 15, 2012 at 12:29
  • @ErwinBrandstetter: In addition to kgrittn's comments, DISTINCT ON is different than DISTINCT in the point that the list of columns inside the ON() is required in the ORDER BY clause. Otherwise this error is thrown: "SELECT DISTINCT ON expressions must match initial ORDER BY expressions" Aug 16, 2012 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.