Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Please note: my copy of Access is in Dutch, so any English parts are translated from Dutch.

I'm currently working on a database in Access using SQL, with the exercise being:

Create a query that will list all students who have more than €25 of penalty.

I created this query:

SELECT L.LLNR, U.LLNR, L.VOORNAAM, L.TUSSENVOEGSEL, L.ACHTERNAAM, SUM(U.BOETE)

FROM 
    LEERLINGEN L
    INNER JOIN UITLENINGEN U ON L.LLNR = U.LLNR

WHERE (((U.BOETE)>25));

When I click Execute, I get the following error (translated from Dutch)

You are attempting to execute a query in which the selected expression LLNR is no part of a statistical function.

What should I do?

Translations: | Leerlingen = students | Boete = penalty |

share|improve this question
2  
I think you forgot a GROUP BY for your select statement. –  Marian Nov 13 '12 at 17:11

1 Answer 1

up vote 5 down vote accepted

When you have an aggregate function, you need a GROUP BY statement. In your case, it would be

SELECT 
    LEERLINGEN.LLNR, 
    UITLENINGEN.LLNR, 
    LEERLINGEN.VOORNAAM, 
    LEERLINGEN.TUSSENVOEGSEL, 
    LEERLINGEN.ACHTERNAAM, 
    SUM(UITLENINGEN.BOETE)
FROM 
    LEERLINGEN 
    INNER JOIN UITLENINGEN ON LEERLINGEN.LLNR = UITLENINGEN.LLNR
WHERE 
    (((UITLENINGEN.BOETE)>25))
GROUP BY
    LEERLINGEN.LLNR, 
    UITLENINGEN.LLNR, 
    LEERLINGEN.VOORNAAM, 
    LEERLINGEN.TUSSENVOEGSEL, 
    LEERLINGEN.ACHTERNAAM;

The GROUP BY clause comes after a WHERE clause but before an ORDER BY (or HAVING) clause on a SELECT statement. In this case, you want to get the sum of "Boete" for each combination of your other columns, hence the need to group by all of those other rows.

In short, if you have an aggregate function (like SUM, AVG, MIN, or MAX), you'll need to tell the database engine what to do with those non-aggregated columns. That's a simplistic way of explaining what GROUP BY does for you there.

Incidentally, if you want to find out cases where the sum of all Uitleningen.Boete is greater than 25 (rather than only including the records in which Uitleningen.Boete is greater than to 25), you actually have to use the HAVING clause. Then it would look like this:

SELECT 
    LEERLINGEN.LLNR, 
    UITLENINGEN.LLNR, 
    LEERLINGEN.VOORNAAM, 
    LEERLINGEN.TUSSENVOEGSEL, 
    LEERLINGEN.ACHTERNAAM, 
    SUM(UITLENINGEN.BOETE)
FROM 
    LEERLINGEN 
    INNER JOIN UITLENINGEN ON LEERLINGEN.LLNR = UITLENINGEN.LLNR
GROUP BY
    LEERLINGEN.LLNR, 
    UITLENINGEN.LLNR, 
    LEERLINGEN.VOORNAAM, 
    LEERLINGEN.TUSSENVOEGSEL, 
    LEERLINGEN.ACHTERNAAM
HAVING
    SUM(UITLENINGEN.BOETE)>25;

Think of HAVING as a WHERE for your groups. You filter rows using WHERE, and you filter groups using HAVING.

share|improve this answer
    
The second query with HAVING gives me the following error: "You are attempting to execute a query in which the selected expression UITLENING.BOETE>25 is no part of a statistical function." Anything I could've done wrong? –  Tanno Nov 13 '12 at 19:46
1  
Sorry, corrected the query. You need the summation function a second time. –  Kevin Feasel Nov 13 '12 at 19:59
    
That seems to have worked, thank you! –  Tanno Nov 13 '12 at 20:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.