1
I have a problem in a query, because there is only one field that differs from one data from another. I have tried using group by
, order by
, having
, I searched in several forums, anyway, I tried everything and I can’t delete these duplicated data.
The query is the following:
SELECT
FTNF.nr_conhecimento AS conhecimento,
NFS.nr_nota_fiscal AS NUMERO_NF,
NFS.tp_registro AS TP_REG,
NFS.emissao,
NFS.vl_total,
NFS.volumes,
NFS.peso_liquido,
NFS.peso_bruto,
NFS.empresa,
EMP.razão,
NFS.frete_por_conta,
FE.vl_frete AS frete_cobrado,
NFS.cidade_entrega,
NFS.estado_entrega,
PV.[Valor do Frete] AS frete_pv,
(FE.vl_frete / NULLIF(NFS.vl_total,0))*100 AS pc_frete,
EST.Nome AS estado
FROM FreteEntrada AS FE
INNER JOIN FreteEntradaNotaSaida AS FTNF
ON (FTNF.nr_conhecimento = FE.nr_conhecimento)
AND (FTNF.tp_registro = FE.tp_registro)
INNER JOIN FVFNotaFiscalSaida AS NFS
ON (NFS.tp_registro = FTNF.tp_registro_nf)
AND (NFS.nr_nota_fiscal = FTNF.nr_nota)
AND (NFS.empresa = FTNF.empresa)
INNER JOIN FVFItemNotaFiscalSaida AS INFS
ON (NFS.nr_sequencial = INFS.nr_sequencial)
AND (NFS.tp_registro = INFS.tp_registro)
INNER JOIN EMPRESAS AS EMP
ON (NFS.empresa = EMP.APEL)
INNER JOIN TRANSPORTADORAS AS TP
ON (FE.cd_transportadora = TP.Código)
LEFT JOIN [Pedidos de Venda] AS PV
ON (INFS.nr_pedido = PV.Número)
AND (INFS.tp_pedido = PV.[Tipo de Registro])
LEFT JOIN Estados AS EST
ON (NFS.estado_entrega = EST.Sigla)
LEFT JOIN Duplicatas AS D
ON (NFS.nr_nota_fiscal = D.Nota)
AND (NFS.tp_registro = D.[Tipo])
AND (NFS.empresa = D.Empresa)
GROUP BY FTNF.nr_conhecimento,
NFS.nr_nota_fiscal,
NFS.tp_registro,
NFS.emissao,
NFS.vl_total,
NFS.volumes,
NFS.peso_liquido,
NFS.peso_bruto,
NFS.empresa,
EMP.razão,
NFS.frete_por_conta,
FE.vl_frete,
NFS.cidade_entrega,
NFS.estado_entrega,
PV.[Valor do Frete],
EST.Nome
The value that differs is the frete_cobrado
. The goal is not to delete this data, but to unite in only one adding up this freight. I tried to use the SUM
in that freight but it just doubles the value of each duplicated field.
Here’s an example of how you are:
| Conhecimento | Número NF | Empresa | Frete Cobrado |
|--------------|-----------|----------|----------------|
| 5 | 154 | Exemplo | 55,00 |
| 5 | 154 | Exemplo | 35,00 |
| 6 | 245 | Exemplo2 | 96,00 |
| 8 | 195 | Exemplo3 | 85,00 |
Note that the first two lines are equal except for the freight charged. This is how it should be:
| Conhecimento | Número NF | Empresa | Frete Cobrado |
|--------------|-----------|----------|----------------|
| 5 | 154 | Exemplo | 85,00 |
| 6 | 245 | Exemplo2 | 96,00 |
| 8 | 195 | Exemplo3 | 85,00 |
Freight is summed up resulting in a single line.
Someone knows what I can do?
Can edit your post and place an example table (can be 4 or 5 lines even)? Do this with a table with the data coming and how you’d like it to be.
– Marco
It is possible to mount a fiddle in the Sqlfiddle also.
– Marco
I edited and added the tables, thanks for the suggestion.
– Paulo Abdanur
I believe the column
pc_frete
also comes with differentiated value, no? Because bothpc_frete
andfrete_cobrado
use the fieldvl_frete
tableFreteEntrada
.– Marco
He does, but when he’s done
frete_cobrado
, thepc_frete
is corrected.– Paulo Abdanur
@Pauloabdanur: Problem solved? // What is the SQL Server version?
– José Diz