Как перевести эти формулы Excel в SQL? - PullRequest
0 голосов
/ 28 января 2019

У меня есть таблица Excel (не отформатированная как объект таблицы Excel), которая выглядит следующим образом: enter image description here И мне нужно перевести это в код SQL.

Пока мне удалось сделать это так:

Sum( CASE WHEN CONCAT( table.product_code, "_", table.account_id,
      table.account_name) = table2.account 
THEN Value ELSE 0 END)
AS 'Cost'

Но, как вы можете видеть, параметр (код продукта) не совпадает с указанным желтой строкой, поэтомуКак я могу принять этот параметр перемещения во внимание?

Спасибо!

1 Ответ

0 голосов
/ 28 января 2019

Это возможно со статическими столбцами.

create local temp table temp_table (
                                     product_code int         not null,
                                     account_id   int         not null,
                                     account_name varchar(25) not null,
                                     value        int         not null
                                   )
;


insert into temp_table
values (3001, 1005, 'Loading Fee', 50000),
       (3001, 1006, 'Seling Fee',  35000),
       (3002, 1007, 'Port-Charge', 12400),
       (3003, 1006, 'Seling Fee',  23000),
       (3005, 1007, 'Port-Charge', 18600),
       (3001, 1006, 'Seling Fee',  12000)
;


select account_id,
       account_name,
       sum(case when product_code = 3001
                then value
                else 0
            end)                            as "3001",
       sum(case when product_code = 3002
                then value
                else 0
            end)                            as "3002",
       sum(case when product_code = 3003
                then value
                else 0
            end)                            as "3003",
       sum(case when product_code = 3004
                then value
                else 0
            end)                            as "3004",
       sum(case when product_code = 3005
                then value
                else 0
            end)                            as "3005"
  from temp_table
 group by account_id,
          account_name
 order by account_id
;

Результат:

 account_id     account_name     3001     3002     3003     3004     3005    
 -------------  ---------------  -------  -------  -------  -------  ------- 
 1005           Loading Fee      50000    0        0        0        0       
 1006           Seling Fee       47000    0        23000    0        0       
 1007           Port-Charge      0        12400    0        0        18600   

Если вы хотите построить его с динамическими столбцами, вы можете попробовать использовать динамическую запись sql.

create temporary table temp_table (
                                     product_code int         not null,
                                     account_id   int         not null,
                                     account_name varchar(25) not null,
                                     value        int         not null
                                   )
;


insert into temp_table
values (3001, 1005, 'Loading Fee', 50000),
       (3001, 1006, 'Seling Fee',  35000),
       (3002, 1007, 'Port-Charge', 12400),
       (3003, 1006, 'Seling Fee',  23000),
       (3005, 1007, 'Port-Charge', 18600),
       (3001, 1006, 'Seling Fee',  12000),
       (3008, 1007, 'Port-Charge', 18600),
       (3009, 1006, 'Seling Fee',  12000),
       (3011, 1006, 'Seling Fee',  12000),
       (3018, 1007, 'Port-Charge', 18600),
       (3019, 1006, 'Seling Fee',  12000)
;

select group_concat(distinct concat('ifnull(sum(case when product_code = ''', product_code, ''' then value end), 0) as `', product_code, '`'))
  into @sql
  from temp_table
;
set @sql = concat('select account_id,
                          account_name, ', @sql, '
                     from temp_table 
                     group by account_id,
                              account_name');




prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...