Skip to content
Advertisement

How to sort and group data in request?

I have got table.

It have some duplicates in purchaseNumber. I need select from this table only newest records for processing. In other words I need to:

  1. group elements by purchaseNumber
  2. sort them by docPublishDate and select newest.
  3. if any of elements in group is have status parsing_status true (suppose it was newest from step 2) any elements SHOULD NOT be returned.

After processing parsing_status flag is changing to true.

docPublishDate – TimeStamp

The items with old dates should not be selected if at last one newest was processed. I tried to do some grouping. But code simply return me all data:

Advertisement

Answer

I understand that you want the latest row per purchase, excepted those whose parsing status is true. One option uses distinct on in a subquery to get the latest element, and then filters in an outer query:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement