Question

I need help about merge 2 query.

QUERY 1:

SELECT  anag.Negozio,anag.NomeNegozio, 
ISNULL(SUM(ven.Quantita), 0) as PezziVenduti, 
ISNULL(SUM(ven.Valore*ven.Quantita), 0) as TotaleVendite 
FROM Dylog_NOL_Anagrafica AS anag 
LEFT JOIN Dylog_NOL_Movimento AS ven ON anag.IDAnagrafica=ven.IDAnagrafica AND ven.TipoMovimento='VE' 
WHERE anag.PortalID=709 AND anag.Negozio IN ('02', '01') 
GROUP BY anag.Negozio, anag.NomeNegozio 
HAVING ISNULL(SUM(ven.Quantita), 0)<>0 

RESULT:

NEGOZIO|NOMENEGOZIO    |PezziVenduti|TOTALE VENDITE
     01|SEDE PRINCIPALE|           2|         51,78

QUERY 2

SELECT  anag.Negozio,anag.NomeNegozio, 
ISNULL(SUM(res.Quantita), 0) as PezziResi,
ISNULL(SUM(res.Valore*res.Quantita), 0) as TotaleResi 
FROM Dylog_NOL_Anagrafica AS anag 
LEFT JOIN Dylog_NOL_Movimento AS res ON anag.IDAnagrafica=res.IDAnagrafica AND res.TipoMovimento='RC' 
WHERE anag.PortalID=709 AND anag.Negozio IN ('02', '01') 
GROUP BY anag.Negozio, anag.NomeNegozio 
HAVING ISNULL(SUM(res.Quantita), 0)<>0

Result:

NEGOZIO|NOMENEGOZIO    |PEZZIRESI|TOTALERESI
     01|SEDE PRINCIPALE|        1|     25,89

MERGE:

SELECT DISTINCT anag.Negozio,anag.NomeNegozio, 
ISNULL(SUM(ven.Quantita), 0) as PezziVenduti, 
ISNULL(SUM(ven.Valore*ven.Quantita), 0) as TotaleVendite, 
ISNULL(SUM(res.Quantita), 0) as PezziResi,
ISNULL(SUM(res.Valore*res.Quantita), 0) as TotaleResi 
FROM Dylog_NOL_Anagrafica AS anag 
LEFT JOIN Dylog_NOL_Movimento AS ven ON (anag.IDAnagrafica=ven.IDAnagrafica AND ven.TipoMovimento='VE') 
LEFT JOIN Dylog_NOL_Movimento AS res ON (anag.IDAnagrafica=res.IDAnagrafica AND res.TipoMovimento='RC')
WHERE anag.PortalID=709 AND anag.Negozio IN ('02', '01') 
GROUP BY anag.Negozio, anag.NomeNegozio 
HAVING ISNULL(SUM(ven.Quantita), 0)<>0 
OR ISNULL(SUM(res.Quantita), 0)<>0

Result:

NEGOZIO|NOMENEGOZIO    |PEZZI VENDUTI|TOTALE VENDITE|**PEZZI RESI|TOTALE RESI**
     01|SEDE PRINCIPALE|            2|         51,78|         **2|      51,78**

WHY ERROR ON FIELD "PEZZI RESI"? THE CORRECT VALUE IS 1.

Was it helpful?

Solution

Instead of "ISNULL(SUM(ven.Quantita), 0) as PezziVenduti" use "ven.Quantita". Same for the other IsNull entries. This obviously is not what you want but you will allow you to see an extra row that you did not expect. The additional Left join is very likely causing an extra row to summed. You might be able to use one left join and use a case statement in your select to filter the sum on TipoMovimento. Hope this helps

;with Returned_And_Sold as 
(
Select ID,
     Type,
     case when type = 'VE' then isnull(sum(M.Quantity),0) end as QuantitySold,
     case when type = 'VE' then isnull(sum(M.Quantity*M.Value),0) end as TotalSold,
     case when type = 'RC' then isnull(sum(M.Quantity),0) end as QuantityReturned,
     case when type = 'RC' then isnull(sum(M.Quantity*M.Value),0) end as TotalReturned
 from dbo.Dylog_NOL_Movimento M
 where m.type in ('VE','RC')
 group by ID,Type
 having isnull(sum(M.Quantity), 0) <> 0
)

 select 
    A.Shop,
    A.StoreName,
    S.QuantitySold,
    S.TotalSold,
    S.QuantityReturned,
    S.TotalReturned 
 from Dylog_NOL_Anagrafica A 
 left JOIN Returned_And_Sold S on 
 S.ID = A.ID 

OTHER TIPS

I think the probelm is OR in

ISNULL(SUM(PezziVenduti), 0) <> 0 
OR ISNULL(SUM(PezziResi), 0) <> 0 

Based on your logic (IsNull(Sum(..)) you can make it with AND

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top