0
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
2
  • would there ever be more than two instances of weight per order_item_id? Commented Mar 24, 2015 at 11:17
  • Please edit your question and show the results that you want. Commented Mar 24, 2015 at 11:31

2 Answers 2

1
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.

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

Comments

0

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;

Comments

Your Answer

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