Home > Software engineering >  SQL | I want to compare a sum of two or more rows and then make a select statement
SQL | I want to compare a sum of two or more rows and then make a select statement

Time:04-08

So I have this table in MySQL:

datec.masini

enter image description here

So, a proprietar (person unique ID) can have one or more cars with a valoare (value $) and I want to show by a SELECT statement all the persons (proprietar) with cars above 190000$. I tried this but doesn't work:

SELECT proprietar FROM datec.masini WHERE sum(valoare)>190000

and I want this result:

1990724 5780623

What am I missing here?

CodePudding user response:

One approach, using aggregation:

SELECT proprietar
FROM datec.masini
GROUP BY proprietar
HAVING SUM(valoare) > 190000;
  •  Tags:  
  • sql
  • Related