Continuing from https://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=27116
I found another wrong SQL in the following case.
I'm using v5.6.3RTM
LLBLGen runtime framework
Adapter
I have this small table UitgaveMagazijnMutatie with the following fields
UitgaveId int32
MagazijnId int32
Datum DateTime
Aantal int32
Vvp Decimal
I need an aggregate of this to sum up a count (stock on date) and amount, so pseudo:
UitgaveId int32
MagazijnId int32
Voorraad int32 : sum of Aantal
Waarde Decimal : sum of (Aantal * Vvp)
public async Task<List<UitgaveVoorraadOpPeildatum>> GetUitgaveMagazijnVoorradenOpPeildatumAsync()
{
using var adapter = _dataAdapterFactory.CreateDataAccessAdapter();
var metaData = new LinqMetaData(adapter);
var q = metaData.UitgaveMagazijnMutatie
.GroupBy(x => new {x.UitgaveId, x.MagazijnId})
.Select(x => new UitgaveVoorraadOpPeildatum
{
UitgaveId = x.Key.UitgaveId,
Voorraad = x.Sum(umm => umm.Aantal),
DoubleWaarde = x.Sum(umm => umm.Aantal * Convert.ToDouble(umm.Vvp) ),
MagazijnId = x.Key.MagazijnId,
});
return await q.ToListAsync();
}
It generates the following SQL:
SELECT [LPA_L1].[UitgaveId],
[LPA_L1].[LPAV_] AS [Voorraad],
[LPA_L1].[LPAV_1] AS [DoubleWaarde],
[LPA_L1].[MagazijnId]
FROM (SELECT [LPA_L3].[UitgaveId],
[LPA_L3].[MagazijnId],
SUM([LPA_L3].[LPAV_]) AS [LPAV_],
SUM([LPA_L3].[LPAV_]) AS [LPAV_1]
FROM (SELECT [LPLA_1].[UitgaveID] AS [UitgaveId],
[LPLA_1].[MagazijnID] AS [MagazijnId],
(CDbl([LPLA_1].[Aantal]) * CDbl([LPLA_1].[VVP])) AS [LPAV_]
FROM [UitgaveMagazijnMutatie] [LPLA_1]) [LPA_L3]
GROUP BY [LPA_L3].[UitgaveId],
[LPA_L3].[MagazijnId]) [LPA_L1]
you can see it generates an alias for the expression (Aantal * VVP) named LPAV_
In the outer SELECT it gets the same LPAV_ in both the sum to LPAV_ and LPAV_1. Then Voorraad becomes LPAV_ and DoubleWaarde becomes LPAV_1, but then both have the same value.
Expected was that Voorraad would have been Sum of Aantal instead of Sum of (Aantal*VVP)