выберите результаты в горизонтальном формате, как показано ниже в MySQL - PullRequest
1 голос
/ 04 февраля 2020

У меня такой стол.

A Таблица продавца такая:

A Seller table like this

Код для той же таблицы:

create table sellers (seller_id int, country varchar(30), Month varchar(30), Sales int);'''

insert into sellers values 
(1, 'I', 'January', 10000),
(1, 'I', 'Februray', 60000),
(1, 'I', 'March', 80000),
(2, 'In', 'january', 20000), 
(2, 'In', 'February', 10000),
(2, 'In', 'March', 10000), 
(3, 'Ind', 'january', 50000),
(3, 'Ind', 'March', 20000),
(3, 'Ind', 'February', 10000)
;

I хотелось бы получить результат в следующем формате

Формат результата такой:

Result format like this

Я попробовал SQL код как это и не может получить результат:

select seller_id, 
(select sales where month = 'January') as Jan_sales, 
(select sales where month = 'February') as Feb_sales, 
(select sales where month = 'March') as Mar_sales
from sellers group by seller_id;

1 Ответ

0 голосов
/ 04 февраля 2020

вы можете использовать поддельную функцию агрегирования

select country
  , max(if(month = 'January', sales, 0) Jan_sales
  , max(if(month = 'February', sales, 0) Feb_sales
  , max(if(month = 'March', sales, 0) Mar_sales
from sellers
group by country
...