Can you please explain me why this query is not doing the SUM(LI.Unidades) as expected? I want all rows with the same LINECOUNT to be added together.
First I also had LI.Unidades in the select and the group by (also need the LI.Unidades field without the SUM()) but I removed it just to test, and still not adding correctly.
SELECT
CASE AL.CodigoEmpresa WHEN 1 THEN '802' WHEN 100 THEN '801' ELSE 'N/A' END AS 'COMPANYID',
D365.Account as 'CUSTACCOUNT',
SUM(LI.Unidades) as 'GROSSWEIGHT',
AL.NumeroLineas as 'LINECOUNT',
'KG' as 'SALESUNIT',
LI.ImporteBruto as 'GROSSAMOUNT'
FROM CabeceraAlbaranCliente AL inner join LineasAlbaranCliente LI on (AL.NumeroAlbaran = LI.NumeroAlbaran and AL.FechaAlbaran = Li.FechaAlbaran and AL.CodigoEmpresa = LI.CodigoEmpresa)
inner join AALPLA_D365Customers D365 on (AL.CifEuropeo = D365.Tax_exempt_number and D365.Plant = AL.CodigoEmpresa)
WHERE AL.EjercicioAlbaran = '2020' and AL.NumeroAlbaran = '14424'
GROUP BY D365.Account, AL.CodigoEmpresa, AL.NumeroAlbaran, AL.NumeroLineas, LI.ImporteBruto
This is the result I am getting:
COMPANYID CUSTACCOUNT GROSSWEIGHT LINECOUNT SALESUNIT GROSSAMOUNT
802 40801 0.0000000000 4 KG 555
802 40801 1.0000000000 4 KG 3000
802 40801 4.0000000000 4 KG 1280
802 40801 16.000000000 4 KG 112
802 40801 100.00000000 13 KG 3123
802 40801 225.00000000 13 KG 11
802 40801 500.00000000 13 KG 100
802 40801 550.00000000 13 KG 30
802 40801 750.00000000 13 KG 12
802 40801 1000.0000000 13 KG 1247
802 40801 1080.0000000 13 KG 1
802 40801 1125.0000000 13 KG 8
802 40801 1200.0000000 13 KG 1765
802 40801 1375.0000000 13 KG 987
802 40801 1475.0000000 13 KG 40
802 40801 2425.0000000 13 KG 30
802 40801 7075.0000000 13 KG 984
And what I want is:
COMPANYID CUSTACCOUNT GROSSWEIGHT LINECOUNT SALESUNIT GROSSAMOUNT
802 40801 21.000000000 4 KG 555
802 40801 21.000000000 4 KG 3000
802 40801 21.000000000 4 KG 1280
802 40801 21.000000000 4 KG 112
802 40801 18880.000000 13 KG 3123
802 40801 18880.000000 13 KG 11
802 40801 18880.000000 13 KG 100
802 40801 18880.000000 13 KG 30
802 40801 18880.000000 13 KG 12
802 40801 18880.000000 13 KG 1247
802 40801 18880.000000 13 KG 1
802 40801 18880.000000 13 KG 8
802 40801 18880.000000 13 KG 1765
802 40801 18880.000000 13 KG 987
802 40801 18880.000000 13 KG 40
802 40801 18880.000000 13 KG 30
802 40801 18880.000000 13 KG 984
I think you want a cumulative sum, not aggregation:
SELECT (CASE AL.CodigoEmpresa WHEN 1 THEN '802' WHEN 100 THEN '801' ELSE 'N/A' END) AS COMPANYID,
D365.Account as 'CUSTACCOUNT',
SUM(LI.Unidades) OVER (ORDER BY LI.Unidades) as GROSSWEIGHT,
AL.NumeroLineas as LINECOUNT,
'KG' as SALESUNIT,
LI.ImporteBruto as GROSSAMOUNT
FROM CabeceraAlbaranCliente AL inner join
LineasAlbaranCliente LI
ON AL.NumeroAlbaran = LI.NumeroAlbaran and
AL.FechaAlbaran = Li.FechaAlbaran and
AL.CodigoEmpresa = LI.CodigoEmpresa inner join
AALPLA_D365Customers D365
on AL.CifEuropeo = D365.Tax_exempt_number and
D365.Plant = AL.CodigoEmpresa
WHERE AL.EjercicioAlbaran = '2020' and AL.NumeroAlbaran = '14424'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.