Search code examples
sqldatabasems-accessrdbms

Adding other columns Access


I have to make a DBMS for Cars in Access. But I got a problem. In my previous question I got this code and it solved my first problem. But now I only get the columns TotalPrijs, AutoNR and Klasse. But I also need: Factuur.Dagen, Factuur.KlantNR etc. I usually did this with the SELECT but now the code is more complicated and I don't know how to get it without getting a error.

This is my code.

SELECT 
SUM(A.TotaalPrijs) As TotaalPrijs,
A.AutoNR,
A.AutoKlasse
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, (((([Factuur]![EindKMStand]-[Factuur]![BeginKMStand])-([Factuur]![Dagen]*125))*[Prijzen]![ExtraKM])+([Prijzen]![dag125KM]*[Factuur]![Dagen])) AS TotaalPrijs, Gegevens.voorletters, Gegevens.tussenvoegsel, Gegevens.achternaam, Gegevens.straatnaam, Gegevens.huisNR, Gegevens.Postcode, Gegevens.rekeningNR, Gegevens.Plaats, (([Factuur]![EindKMStand]-[Factuur]![BeginKMStand])-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
GROUP BY 
    A.AutoNR, A.AutoKlasse

how to add the Factuur.Dagen and Factuur.KlantNR and my other columns?


Solution

  • When you have grouped data in hand, and your SELECT expects only one of it... SOmehow you have take out one logical value.

    This is why we use AGGREGATE function. Just like SUM() did an addition of all TotaalPrijs in your grouped data.. Taking a MAX(Dagen) would one value from that group.

    SELECT 
    SUM(A.TotaalPrijs) As TotaalPrijs,
    A.AutoNR,
    A.AutoKlasse,
    MAX(Factuur.Dagen) -- < you need just this..!
    

    OR add this column to your grouping condition as well!

    GROUP BY A.AutoNR, A.AutoKlasse,Factuur.Dagen