Before I ask my question I want to say Happy new year to everyone! But I have a problem. I currently have a school assignment to make a database for cars. To calculate the total I use this query.
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));
But I am getting double rows. (Factuur.AutoNR and Autos.AutoNR, AutoKlasse and Klasse) (picture: http://gyazo.com/c51f484617d946ae70f9446f41256bec )
Any way to only get 1 row with the information?
Have a nice day guys!
First, to get better answers, it would be helpful to tell us what RMDBS you are using (from the screenshot, I'm guessing Access). Different database systems implement SQL quite differently.
Second, what do you want each row to represent in your query? As you have written you will get one row for each Factuur (invoice [apologies my dutch is not so good...]) with the total price for that invoice.
It sounds like you want to get totals by autoNR. To do this you need something like
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
When you use group by, you choose the fields you want to aggregate on (AutoNR and AutoKlasse) and the fields you want to aggregate (TotaalPrijs) and the function you want to use to aggregate (in your case SUM, but it could be MIN or MAX) the query will then get the SUM of TotaalPrijs for each unique combination of AutoNR and AutoKlasse.