Mysql condition for unit of measure specific value

Asked

Viewed 29 times

0

I have a BD view where I need to bring a total amount of KG/LT of products expected to receive in the company, I need to include a condition that when the unit is TON or TN it multiplies by 1000, because when it is ton, it calculates as if it were normal unit, this information is taken from XML, it is possible to?

Follow the current query:

SELECT SUM(i_prod_quant) as QUANT, pha_table.pha_emp as EMP FROM pha_table 
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj 
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave 
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP
  • Ulysses, from what I understood the field "i_prod_quat" has the information regarding the quantity of the product, but would there be a field that informs what is the unit related to the quantity of the product? If yes, what would this field be? You can include in your question a diagram of your pha_table?

1 answer

1


In your case just make use of a condition with the IF in your select that will work, the IF should see the unit of measure to do the multiplication.

SELECT SUM(IF(UNIDADE='TON', i_prod_quant*1000, i_prod_quant)) as QUANT, pha_table.pha_emp as EMP FROM pha_table 
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj 
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave 
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP

You can also have the same effect by using CASE

SELECT SUM(CASE WHEN UNIDADE='TON' THEN i_prod_quant*1000 ELSE i_prod_quant END) as QUANT, pha_table.pha_emp as EMP FROM pha_table 
            INNER JOIN phs_table ON pha_table.pha_parceiro_cnpj = phs_table.phs_parceiro_cnpj 
            INNER JOIN nfe_table ON nfe_table.nfe_chave = phs_table.phs_nfe_chave 
            INNER JOIN i_nfe_table ON i_nfe_table.i_nfe_chave = nfe_table.nfe_chave GROUP BY EMP
  • And if I need more than one option, I tried to put but is giving error!

  • Got it! thanks for the reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.