user3946530
user3946530

Reputation:

how to use a column value in postgresql

order_item_id   order_status   weight
158871745        "delivered"       0.3
158032756        "delivered"       0.3
158871745        "return"          0.5

i want to find the difference between weight of same order_item_id have different

order_status

i want output like

 order_item_id   order_status  weight   error 
    158871745     "return"      0.5      0.2

Upvotes: 0

Views: 41

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

One method uses window functions:

select t.*,
       (case when min(order_status) over (partition by order_item_id) <>
                  max(order_status) over (partition by order_item_id)
             then max(weight) over (partition by order_item_id) - 
                  min(weight) over (partition by order_item_id)
        end) as weightdiff
from table t;

Upvotes: 0

Dani
Dani

Reputation: 181

select t1.weight-t2.weight from table as 't1' join table as 't2' on t1.order_item_id=t2.order_item_id where t1.order_status!=t2.order_status;

where table is the actual name of the table.

Upvotes: 1

Related Questions