如何将此Excel公式转换为SQL?

时间:2019-01-28 08:22:28

标签: mysql sql excel

我有一个如下所示的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 :(得分:1)

可能是静态列。

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;