SELECT with repeated field

0

I'm having trouble executing a SQL query.

The query is:

SELECT DISTINCT CHAVE_FATO,COD_FILIAL,COD_DOCTO,NUM_DOCTO,DATA,VALOR_TOTAL,VALOR_LIQUIDO,COD_CLI_FOR,NOME_CLI_FOR,COD_VEND_COMP,NOME_VEND_COMP,COD_PRODUTO,DESC_PRODUTO_EST,NOME_SECAO,QTDE_UND,QTDE_PRI,QTDE_AUX,VALOR_UNITARIO,VALOR_TOTAL_ITEM 
FROM [SATK].[dbo].entrada 
WHERE DATA > '2018-01-01 00:00:00' 
ORDER BY NUM_DOCTO ASC

Does not work, return continues showing repeated values in column NUM_DOCTO.

I need to eliminate this, but I can not.

    
asked by anonymous 10.01.2018 / 13:16

2 answers

1

Complementing the response with group by , you need to indicate in a repeated case, which record will be chosen to be displayed. It can be with MIN (lower), MAX (higher), SUM (sum), AVG (average), COUNT (amount).

An example would be:


SELECT MAX(CHAVE_FATO),MIN(COD_FILIAL),MAX(COD_DOCTO),NUM_DOCTO,MIN(DATA),MAX(VALOR_TOTAL),MAX(VALOR_LIQUIDO),MAX(COD_CLI_FOR),MAX(NOME_CLI_FOR),MAX(COD_VEND_COMP),MAX(NOME_VEND_COMP),MAX(COD_PRODUTO),MAX(DESC_PRODUTO_EST),MAX(NOME_SECAO),MAX(QTDE_UND),MAX(QTDE_PRI),MAX(QTDE_AUX),MAX(VALOR_UNITARIO),MAX(VALOR_TOTAL_ITEM) 
FROM [SATK].[dbo].entrada 
WHERE DATA > '2018-01-01 00:00:00'
GROUP BY NUM_DOCTO
ORDER BY NUM_DOCTO ASC

But be aware, in this case only one line will be displayed. If you have more than one product linked to this document, the result will only be one respecting the aggregation function (min, max, sum ...).

    
10.01.2018 / 17:32
0

Use GROUP BY

SELECT DISTINCT CHAVE_FATO,COD_FILIAL,COD_DOCTO,NUM_DOCTO,DATA,VALOR_TOTAL,VALOR_LIQUIDO,COD_CLI_FOR,NOME_CLI_FOR,COD_VEND_COMP,NOME_VEND_COMP,COD_PRODUTO,DESC_PRODUTO_EST,NOME_SECAO,QTDE_UND,QTDE_PRI,QTDE_AUX,VALOR_UNITARIO,VALOR_TOTAL_ITEM 
FROM [SATK].[dbo].entrada 
WHERE DATA > '2018-01-01 00:00:00'
GROUP BY NUM_DOCTO
ORDER BY NUM_DOCTO ASC

More information at: link

    
10.01.2018 / 13:23