Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como selecionar coluna na tabela criando linha em outra tabela no MySQL

select item_id, price,
       (min(case when tax_name = 'VAT' then tax end)) vat,
       (min(case when tax_name = 'LBT' then tax end)) lbt,
       coalesce(min(case when tax_name = 'VAT' then tax end),0) +
       coalesce(min(case when tax_name = 'LBT' then tax end),0) +
       price total
  from 
      (select a.item_id item_id,
              c.tax_name tax_name,
              (c.tax_value * b.price / 100) tax,
              b.price price
         from item_tax a inner join item_master b on a.item_id = b.item_id
                         inner join tax_master c on a.tax_id = c.tax_id) as calc
 group by item_id, price;

Demonstração aqui .