SQL consult, how to solve?

brvheart

New Member
It is a homework, but I can not do alone.1) List the code name, the amount of cars sold and the total profit for resalefor resales who have profit up to $5000.\[code\]resale:cod | name | city | state--------+-----------------+------------+--------01 | Paraiso | Sao Paulo | SP02 | Alameda | Taubate | SP03 | Cabana | Macae | RJ04 | Santana | Betim | MGAutomotive:cod | manufacturer | model | year | country | price--------+------------+-----------------+------+-----------+----------01 | 01 | Gol | 2000 | Brasil | 25000.0002 | 01 | Golf | 2005 | Argentina | 39000.0003 | 04 | Ford Ka | 1990 | Brasil | 15000.0004 | 03 | Corsa Seda | 1995 | Brasil | 12500.0005 | 04 | Fiesta | 2003 | Argentina | 20000.0006 | 03 | Corsa Seda | 1995 | Argentina | 10000.0007 | 05 | Palio | 2002 | Brasil | 15000.0008 | 05 | Siena | 2006 | Brasil | 26000.00sale:customer| resale | automotive | date | value---------+---------+-----------+------------+----------02 | 01 | 03 | 2010-02-05 | 17500.0004 | 02 | 01 | 2010-01-07 | 28000.0001 | 03 | 08 | 2010-02-15 | 28000.0002 | 03 | 02 | 2010-03-12 | 42000.0003 | 04 | 06 | 2010-02-06 | 11500.0003 | 02 | 05 | 2010-01-25 | 22100.0001 | 01 | 04 | 2010-01-21 | 15500.0003 | 01 | 08 | 2012-02-05 | 17500.00\[/code\]My SQL:\[code\]SELECT automotive.cod, resale.name, COUNT(sale.resale) AS ammount, SUM(sale.value - automotive.price) AS total FROM sale, automotive, resale WHERE sale.resale = resale.cod AND automotive.cod = sale.automotive GROUP BY sale.resale, automotive.cod, resale.name HAVING SUM(sale.value - automotive.price) <= 5000;\[/code\]I cant do alone, my answer is wrong.Ps: I'm using PostgresSQL.
 
Top