Command in SQL already with correct calculation

Asked

Viewed 61 times

-1

We have a differential in the payment of ICMS and I would like to export a spreadsheet which I have already created the command, also create what amount we will pay from ICMS.

In case it would be the following.:

Where we highlight 10% ICMS, we pay only 4% effectively.
Where we highlight 4% of the ICMS, we pay effectively 1.4%

The Current Command:

SELECT DISTINCT nf.codloja, 
                nf.dtemissao, 
                nf.numnota, 
                ni.icms, 
                Sum(ni.subtoticms) AS toticms 
FROM   notafiscal nf 
       JOIN nfitens ni 
         ON ni.codnf = nf.codnf 
WHERE  dtemissao BETWEEN '06/01/2019' AND '06/30/2019' 
       AND COALESCE(nf.anulada, 0) = 0 
       AND COALESCE(nf.cancelada, 0) = 0 
       AND codloja = 2 
       AND COALESCE(nf.protocolonfe, '') <> '' 
GROUP  BY 1, 
          2, 
          3, 
          4 
  • What exactly values you want to display?

  • The remarks: "Where we highlight 10% of ICMS, we pay only 4% effectively." and "Where we highlight 4% of the ICMS, we effectively pay 1.4%" refer to which fields of your table(s) ()?

  • Ronaldo, I would like to already make the calculation of the real value that I will pay in each of the situations. Based on the value of ICMS that I already have and knowing what was the percentage paid in Note.

  • In the column icms table nfitens would have the percentages of ICMS (10% and 4%)? And in the column subtoticms would the amount paid? Put some sample records of each table, to help you better understand.

  • It returns with the following example. COD LOJA - DT EMISSAO - NUM NOTA - ICMS - SUM OF ICMS 1 - 25/06/2019- 25656 - 4% - 237,00 1 - 25/06/2019 - 25656 - 10% - 421,00

1 answer

-1

Assuming that in your tables you do not have the note value for recalculation of the ICMS, and therefore you need to recalculate from the ICMS value you have, and that the icms column contains a string with the percentage, try:

SELECT DISTINCT nf.codloja, 
                nf.dtemissao, 
                nf.numnota, 
                ni.icms, 
                Sum(CASE WHEN ni.icms = '4%' THEN (ni.subtoticms * 0.014) / 0.04
                         WHEN ni.icms = '10%' THEN (ni.subtoticms * 0.04) / 0.1
                         ELSE ni.subtoticms
                    END) AS toticms 
FROM   notafiscal nf 
       JOIN nfitens ni 
         ON ni.codnf = nf.codnf 
WHERE  dtemissao BETWEEN '06/01/2019' AND '06/30/2019' 
       AND nf.anulada IS NOT NULL 
       AND nf.cancelada IS NOT NULL 
       AND codloja = 2 
       AND nf.protocolonfe <> '' 
GROUP  BY 1, 
          2, 
          3, 
          4 

If not, explain how your data is organized.

  • In the command I put, the data returns with the value of the ICMS already taking the Note and also the rate. I thought something like what I have in another command that I recalculated for PIS and COFINS: <code> cast( cast(nf.total as Numeric(10,2)) * 0.0165 as Numeric(10,2)))the new valuables, cast( cast(nf.total as Numeric(10,2)) * 0.076 as Numeric(10,2)) as valorcofinsnovo, nf.valoricms from notafiscal nf

  • According to what you said it is not enough then just identify and change the percentage from 4% to 1.4% and from 10% to 4%? I did not understand the meaning of these multipliers 0.0165 (16.5%) and 0.076 (7.6%).

Browser other questions tagged

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