0

I have these tables:

Hraci:

  • IDHrace (int)
  • Jmeno (varchar)

Ucast_Zapas:

  • ID_Hrace_zapas (int)
  • branky (int)

I want SUM how much "branky" WHERE ID_Hrace_zapas=IDHrace and Jmeno="karel" for example...

This is my code:

SELECT 
    SUM(Ucast_Zapas.Branky), Ucast_Zapas.ID_Hrace_zapas, Hraci.Jmeno,Hraci.IDHrace
FROM 
    Hraci, Ucast_Zapas  
WHERE 
    (Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace) AND (Hraci.Jmeno='smajlik');

I don´t know, where is the problem, SQl writes this error:

Column 'Ucast_Zapas.ID_Hrace_zapas' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

2
  • rule of thumb: when using an aggregate function in SQL, always use GROUP BY on the non aggregated columns (aka, the plain ones). In this case, you would most likely need to skip selecting the ID because then you could leave the SUM() function out of play anyhow Commented Mar 27, 2013 at 10:31
  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was discontinued with the ANSI-92 SQL Standard (more than 20 years ago) Commented Mar 27, 2013 at 10:32

1 Answer 1

1
SELECT IDHrace, SUM(branky)
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace
WHERE Hraci.Jmeno='smajlik'
GROUP BY IDHrace
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.