Reputation: 59
I've been stuck with a problem in MS Access. I am trying to add a column but I got a error.
Your query does not include the specified expression 'BorgA' as part of an aggregate function.
I am using this query:
SELECT
SUM(A.TotaalPrijs) As TotaalPrijs,
A.AutoNR,
A.AutoKlasse,
MAX(A.Factuur.Dagen) as Dagen,
A.Prijzen.dag125KM as PrijsPerDag,
A.Prijzen.ExtraKM As PrijsPerExtraKM,
A.Factuur.FactuurNR,
A.Factuur.KlantNR,
A.Factuur.Begindatum,
A.Factuur.Einddatum,
A.Factuur.Borg,
Gegevens.voorletters,
Gegevens.tussenvoegsel,
Gegevens.achternaam,
Gegevens.straatnaam,
Gegevens.huisNR,
Gegevens.Postcode,
Gegevens.rekeningNR,
Gegevens.Plaats,
A.KMteVEEL,
B.BorgA
FROM
(SELECT
Factuur.Dagen, Factuur.AutoNR AS carNR, autos.AutoNR,
autos.Klasse AS AutoKlasse, Prijzen.Klasse,
Prijzen.dag125KM, Prijzen.ExtraKM,
(prijzen.dag125KM * Factuur.Dagen) AS MinPrijs,
Factuur.FactuurNR, Factuur.KlantNR,
Factuur.Begindatum, Factuur.Einddatum,
Factuur.Borg,
(KMteVEEL * [Prijzen]![ExtraKM]) + ([Prijzen]![dag125KM] * [Factuur]![Dagen]) AS TotaalPrijs,
Gegevens.voorletters, Gegevens.tussenvoegsel,
Gegevens.achternaam, Gegevens.straatnaam,
Gegevens.huisNR, Gegevens.Postcode, Gegevens.rekeningNR,
Gegevens.Plaats,
IIF([Factuur]![EindKMStand] - [Factuur]![BeginKMStand] - ([Factuur]![Dagen] * 125) < 0, 0, [Factuur]![EindKMStand]-[Factuur]![BeginKMStand]-([Factuur]![Dagen]*125)) AS KMteVEEL
FROM
autos, Factuur, Prijzen, Gegevens
WHERE
(((Factuur.AutoNR) = Autos.AutoNR)
AND ((autos.Klasse) = Prijzen.Klasse)
AND ((Factuur.KlantNR) = Gegevens.KlantNR))
) AS A,
(SELECT Prijzen.Borg as BorgA
FROM ((Prijzen
INNER JOIN Autos ON Autos.Klasse = Prijzen.Klasse)
INNER JOIN Factuur ON Factuur.AutoNR = Autos.AutoNR)) AS B
GROUP BY
A.AutoNR, A.AutoKlasse, A.Prijzen.dag125KM, A.Prijzen.ExtraKM, A.Factuur.FactuurNR, A.Factuur.KlantNR, A.Factuur.Begindatum, A.Factuur.Einddatum, A.Factuur.Borg, Gegevens.voorletters, Gegevens.tussenvoegsel, Gegevens.achternaam, Gegevens.straatnaam, Gegevens.huisNR, Gegevens.Postcode, Gegevens.rekeningNR, Gegevens.Plaats, A.KMteVEEL;
The code worked perfect before I added the B.BorgA
and the
(SELECT Prijzen.Borg as BorgA
FROM ((Prijzen
INNER JOIN Autos ON Autos.Klasse = Prijzen.Klasse)
INNER JOIN Factuur ON Factuur.AutoNR = Autos.AutoNR)) AS B
Part.
Is there any way how to get it working?
I've tried adding B.BorgA in the GROUP BY but that didn't return the correct result.
Any other way how to get it working?
Thanks,
Upvotes: 0
Views: 130
Reputation: 34784
Since you had no criteria to limit the join between your A
subquery and B
subquery you were getting a Cartesian product. Your subquery already joins to Prijzen
so just add that into the SELECT
and GROUP BY
lists. Also other cleanup:
SELECT
SUM(TotaalPrijs) As TotaalPrijs,
AutoNR,
AutoKlasse,
MAX(Dagen) as Dagen,
dag125KM as PrijsPerDag,
ExtraKM As PrijsPerExtraKM,
FactuurNR,
KlantNR,
Begindatum,
Einddatum,
Borg,
voorletters,
tussenvoegsel,
achternaam,
straatnaam,
huisNR,
Postcode,
rekeningNR,
Plaats,
KMteVEEL,
BorgA
FROM (SELECT p.Borg as BorgA,f.Dagen, f.AutoNR AS carNR, a.AutoNR, a.Klasse AS AutoKlasse, p.Klasse, p.dag125KM, p.ExtraKM, (p.dag125KM*f.Dagen) AS MinPrijs, f.FactuurNR, f.KlantNR, f.Begindatum, f.Einddatum, f.Borg, (KMteVEEL*[Prijzen]![ExtraKM])+([Prijzen]![dag125KM]*[Factuur]![Dagen]) AS TotaalPrijs, g.voorletters, g.tussenvoegsel, g.achternaam, g.straatnaam, g.huisNR, g.Postcode, g.rekeningNR, g.Plaats, IIf([Factuur]![EindKMStand]-[Factuur]![BeginKMStand]-([Factuur]![Dagen]*125) < 0, 0, [Factuur]![EindKMStand]-[Factuur]![BeginKMStand]-([Factuur]![Dagen]*125)) AS KMteVEEL
FROM autos a
INNER JOIN Factuur f ON a.AutoNR = f.AutoNR
INNER JOIN Prijzen p ON a.Klasse = p.Klasse
INNER JOIN Gegevens g ON f.KlantNR = g.KlantNR
) AS sub
GROUP BY AutoNR,
AutoKlasse,
dag125KM,
ExtraKM,
FactuurNR,
KlantNR,
Begindatum,
Einddatum,
Borg,
voorletters,
tussenvoegsel,
achternaam,
straatnaam,
huisNR,
Postcode,
rekeningNR,
Plaats,
KMteVEEL,
BorgA
I also eliminated your outdated JOIN
syntax and added aliases. The subquery isn't even necessary, but you'd have to change aliases appropriately if you eliminated it.
Upvotes: 0
Reputation: 93754
You have selected a non aggregate field B.BorgA
in select list but you missed to add it in B.BorgA
in group by
Select ...
....
GROUP BY A.AutoNR,
A.AutoKlasse,
A.Prijzen.dag125KM,
A.Prijzen.ExtraKM,
A.Factuur.FactuurNR,
A.Factuur.KlantNR,
A.Factuur.Begindatum,
A.Factuur.Einddatum,
A.Factuur.Borg,
Gegevens.voorletters,
Gegevens.tussenvoegsel,
Gegevens.achternaam,
Gegevens.straatnaam,
Gegevens.huisNR,
Gegevens.Postcode,
Gegevens.rekeningNR,
Gegevens.Plaats,
A.KMteVEEL,
B.BorgA; -- Missed
Upvotes: 0