I have data in my table as:
sku | grade | price |
---|---|---|
iphone | A | 700 |
iphone | B | 600 |
samsung | A | 200 |
I want to merge the rows based on the sku col and have cols as A_price and B_price based on the grade value.
The result should be:
sku | A_price | B_price |
---|---|---|
iphone | 700 | 600 |
samsung | 200 | null |
I have tried the below query:
SELECT
CASE WHEN grade = 'A' THEN price end as A_price,
CASE WHEN grade = 'B' THEN price end as B_price
FROM dataTable
GROUP BY sku
But its giving me error as ‘not a group by expression’.
2
Answers
You could sum over a
case
expression for each column:You can do it by this query :