1

Assume I have the table below and I would like to return only those rows where Price != price in the previous row but only within a group where the grouping is by Time

ID :           {    1   ,  2      ,      3   ,    4    ,    5    ,    6 }

Time        : { 09:20:00, 09:20:00,  09:20:00, 09:20:01, 09:20:02, 09:20:03 }

Price :       {   100   , 100     ,  101     , 102     ,   102   ,   103 }

Because of the grouping by time, the output returned should be:

ID                   : {   1     ,    3    ,    4    ,   5      ,  6 }
Time                 : { 09:20:00, 09:20:00, 09:20:01, 09:20:02 , 09:20:03 }
Price :                {   100   ,  101    ,  102     , 102     , 103 }

Do notice that the output discards one of the 100 price at 09:20 but does not discard 102 price at 09:20:02 even though it is same as price at 09:20:01 since the grouping has to be done by time. Can someone help me with a postgres query for something like this.

2
  • How is the order within a group defined? Commented Nov 15, 2018 at 18:50
  • There is a unique id for every row, basically 3 columns ID, Time and Price. I have edited the table structure above. Commented Nov 15, 2018 at 18:54

1 Answer 1

2
SELECT time, price
FROM mytable
WHERE price <> coalesce(
                  lag(price) OVER (PARTITION BY time ORDER BY id),
                  price - 1
               )
ORDER BY time, id;

The coalesce takes care of the NULL value that appears for the first element in each partition.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.