Mysql - how to query different column with different values from one column

I tried to create different column with different values from one column... I still can't figure out how to handle this request..I have table like this

No  Description   Delivery  unit
1   Shipment to A   Car     2
2   Shipment to B   plane   4
3   Shipment to C   Ship    3
4   Shipment to A   Car     1
5   Shipment to C   Ship    2

I want to create something like this

no  description        Car   Plane  Ship
1   shipment to all     3      4     5

how to achieve that...

thank in advance.

Upvotes: 2

Views: 59

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use conditional aggregation:

select 1 as no, 'Shipment to all' as description,
       sum(case when delivery = 'Car' then unit else 0 end) as car,
       sum(case when delivery = 'plane' then unit else 0 end) as plane,
       sum(case when delivery = 'ship' then unit else 0 end) as ship
from t;

It is not clear how the first two columns are calculated, so I just included constants.

Upvotes: 4

Related Questions