N
N
Nposk2021-05-27 14:20:02
SQL
Nposk, 2021-05-27 14:20:02

How to get the quantity per state for a product?

Data set:
Name of goods, condition, quantity of goods.

you need to get
the Name of the product, (Number of states 1 (or 0)), (Number of states 2 (or 0)), (Number of states 3 (or 0)), Amount (number of all states)

Stupid question but I can't figure out how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-05-27
@Akina

Well, the usual conditional aggregation!

SELECT name,
       SUM((state=1)*amount) total_state_1,
       SUM((state=2)*amount) total_state_2,
       SUM((state=3)*amount) total_state_3
FROM tablename
GROUP BY name;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question