1

I have a table order_item like this:

  | sku | quantity | subtotal |
  |-----|----------|----------|
  |  A  |        1 |   25.00  |
  |  B  |        2 |   40.00  |
  |  C  |        3 |   45.00  |

Is there any way to select "unit order_item" rows from order_item with result like:

  | sku | unit_price |
  |-----|------------|
  |  A  |   25.00    |
  |  B  |   20.00    |
  |  B  |   20.00    |
  |  C  |   15.00    |
  |  C  |   15.00    |
  |  C  |   15.00    |

where the result row counts of a sku is the quantity in order_item and the unit_price is (subtotal/quantity) ?

1 Answer 1

2

You can use generate_series() for that:

select oi.sku, oi.subtotal / oi.quantity as unit_price
from order_item oi
  cross join lateral generate_series(1,oi.quantity)
order by oi.sku;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.