Line with NULL value is disregarded by SUM function?

8

I participated in the IFNMG competition, a test prepared by the CEFET Foundation. I solved the following question and I marked the letter C, but the template says that the correct one is the letter A.

Question 31 Note that the NotaFiscalItem table is stored in a relational Database Management System (DBMS), containing the following data:
Table: Financial Note

Next,lookatthefollowingSQLcommand.Figure:SQLcommand

TheresultproducedbytheexecutionoftheSQLcommand,whenconsideringthedatapresentedintheNotaFiscalItemtable,isa)28,007,0048,00
b)28,00NULLNULL
c)48,007,0048,00
d)48,000,0055,00
e)NULLNULLNULL

Thestatement"sum (qtd_item * vlr_unitario - vlr_desconto)" does not add the first line to "null" in the "vlr_desconto" column? But does not this depend on the DBMS being used?

    
asked by anonymous 17.12.2018 / 18:07

3 answers

6

At Documentation of Clustered Functions from MySQL :

  

... group functions ignore NULL values.

OR in free translation:

  

... grouped functions ignore NULL values.

No SQL Server o NULL of clustered columns invalidates the expression, so to answer this question accurately you would need to know what SGBD used, but according to this page (which I can not certify with 100% certainty of truthfulness), in default ANSI all aggregate functions except COUNT will ignore NULL values when computing their results.

Schema (MySQL v5.7)

CREATE TABLE NotaFiscalItem(
  nro_nota_fiscal int,
  nro_item smallint,
  qtd_item smallint,
  vlr_unitario numeric(7, 2),
  vlr_desconto numeric(7, 2)
);

INSERT INTO NotaFiscalItem VALUES
(1, 1, 2, 10.00, NULL),
(1, 2, 2, 7.50, 2.00),
(1, 3, 1, 20.00, 5.00);

Query # 1

SELECT SUM(qtd_item * vlr_unitario - vlr_desconto),
       SUM(vlr_desconto),
       SUM(qtd_item * vlr_unitario) - SUM(vlr_desconto)
  FROM NotaFiscalItem;

Result:

| SUM(qtd_item * vlr_unitario - vlr_desconto) | SUM(vlr_desconto) | SUM(qtd_item * vlr_unitario) - SUM(vlr_desconto) |
| ------------------------------------------- | ----------------- | ------------------------------------------------ |
| 28                                          | 7                 | 48                                               |

You can check out the DB Fiddle .

    
17.12.2018 / 18:28
4

To create the database:

CREATE TABLE NotaFiscalItem (
  nro_nota_fiscal integer,
  nro_item smallint,
  qtd_item smallint,
  vlr_unitario numeric(7, 2),
  vlr_desconto numeric(7, 2)
);

INSERT INTO NotaFiscalItem (nro_nota_fiscal, nro_item, qtd_item, vlr_unitario, vlr_desconto) VALUES (1, 1, 2, 10, NULL);
INSERT INTO NotaFiscalItem (nro_nota_fiscal, nro_item, qtd_item, vlr_unitario, vlr_desconto) VALUES (1, 2, 2, 7.50, 2.00);
INSERT INTO NotaFiscalItem (nro_nota_fiscal, nro_item, qtd_item, vlr_unitario, vlr_desconto) VALUES (1, 3, 1, 20.00, 5.00);

Transcribing SELECT:

Select sum(qtd_item*vlr_unitario - vlr_desconto),
       sum(vlr_desconto),
       sum(qtd_item*vlr_unitario) - sum(vlr_desconto)
  from NotaFiscalItem

When you run this (in MySQL 5.6), this answer came:

28    7    48
    
17.12.2018 / 18:28
0

Try to use COALESCE

ex:

SELECT coalesce(sum(qtd_item*vlr_unitario - vlr_desconto),0) from NotaFiscalItem
    
17.12.2018 / 18:34