SQL查询,如何解决?

SQL查询,如何解决?,sql,postgresql,Sql,Postgresql,这是一个家庭作业,但我不能独自做 1) 列出代码名称、汽车销售量和转售利润总额 对于利润高达5000美元的转售 resale: cod | name | city | state --------+-----------------+------------+-------- 01 | Paraiso | Sao Paulo | SP 02 | Alameda | Taubate | SP 03 | Cabana | Macae | RJ 04 | Sant

这是一个家庭作业,但我不能独自做

1) 列出代码名称、汽车销售量和转售利润总额 对于利润高达5000美元的转售

resale:
cod | name    | city      | state
--------+-----------------+------------+--------
01  | Paraiso | Sao Paulo | SP
02  | Alameda | Taubate   | SP
03  | Cabana  | Macae     | RJ
04  | Santana | Betim     | MG


Automotive:
cod | manufacturer | model      | year | country   | price
--------+------------+-----------------+------+-----------+----------
01  | 01           | Gol        | 2000 | Brasil    | 25000.00
02  | 01           | Golf       | 2005 | Argentina | 39000.00
03  | 04           | Ford Ka    | 1990 | Brasil    | 15000.00
04  | 03           | Corsa Seda | 1995 | Brasil    | 12500.00
05  | 04           | Fiesta     | 2003 | Argentina | 20000.00
06  | 03           | Corsa Seda | 1995 | Argentina | 10000.00
07  | 05           | Palio      | 2002 | Brasil    | 15000.00
08  | 05           | Siena      | 2006 | Brasil    | 26000.00

sale:
customer| resale | automotive | date       | value
---------+---------+-----------+------------+----------
02      | 01     | 03         | 2010-02-05 | 17500.00
04      | 02     | 01         | 2010-01-07 | 28000.00
01      | 03     | 08         | 2010-02-15 | 28000.00
02      | 03     | 02         | 2010-03-12 | 42000.00
03      | 04     | 06         | 2010-02-06 | 11500.00
03      | 02     | 05         | 2010-01-25 | 22100.00
01      | 01     | 04         | 2010-01-21 | 15500.00
03      | 01     | 08         | 2012-02-05 | 17500.00
我的SQL:

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;
选择automotive.cod、resale.name、COUNT(sale.resale)作为amount,SUM(sale.value-automotive.price)作为sale、automotive、resale的总计
其中sale.resale=resale.cod和automotive.cod=sale.automotive
按sale.resale、automotive.cod、resale.name分组
拥有SUM(sale.value-automotive.price)你几乎拥有了它:

SELECT r.cod, r.name
     , count(s.resale) AS amount
     , sum(s.value - a.price) AS total
FROM   sale s
JOIN   automotive a ON a.cod = s.automotive 
JOIN   resale r     ON r.cod = s.resale
GROUP  BY r.cod, r.name 
HAVING sum(s.value - a.price) <= 5000;
选择r.cod,r.name
,将(转售)计入金额
,总金额(s.价值-a.价格)
从出售
在a.cod=s.automotive上加入automotive a
在r.cod=s.resale上加入转售r
按r.cod、r.name分组

拥有sum(s.value-a.price)你应该等待这个结果-交叉发布是非常有害的。好吧,我删除了另一个帖子。详细说明“我的答案是错误的”。我只是不知道如何写一个正确的答案。引导我去做。诚实地说它是家庭作业的要点:-)哇,它成功了。。。非常感谢。我的问题几乎是肯定的。。。谢谢