Postgresql 将两个表组合到一个查询中

Postgresql 将两个表组合到一个查询中,postgresql,Postgresql,我在postgresql中有两张表,分别为销售和员工。 我需要计算: 返回2015年销售额最高的前4名员工的查询。此外,已离职员工的销售额应低于员工编号999 提前谢谢你的帮助 已测试的推荐SQL: SELECT s.employee_id, emp.first_name, emp.last_name, s.department, sum(s.num_of_products) as total_product, sum(s.total_price) as total_price

我在postgresql中有两张表,分别为销售和员工。 我需要计算: 返回2015年销售额最高的前4名员工的查询。此外,已离职员工的销售额应低于员工编号999 提前谢谢你的帮助


已测试的推荐SQL:

SELECT  
s.employee_id, emp.first_name,  
emp.last_name, s.department,  
sum(s.num_of_products) as total_product,  
sum(s.total_price) as total_price,  
s.branch_id, s.branch_city,  
s.date  
FROM sales s  
JOIN employees emp  
ON s.employee_id = emp.employee_id  
WHERE date_part('year',s.date) = '2014'  
-- '2014' is year depend on the input ex. '2015'  
GROUP BY s.employee_id, emp.first_name,emp.last_name,  
s.department, s.branch_id, s.branch_city, s.date  
-- add non aggregate function (sum,count,max,etc) field to group by if you want to show the field.  
-- not add in group by cause error 'field must appear in group by'
ORDER BY total_product  
-- order by total_product based on total_product  
-- order by for choose order by product or price using alias  
-- order by without alias ORDER BY sum(s.num_of_products)  
-- order by will be error caused error if use num_of_products  
-- order by total_price if the TOP 4 based on total_price  
LIMIT 4;   
-- top 4  
-- top n => LIMIT n

请阅读这篇文章及其公认的答案:Sooo。。你想让我们帮你做作业吗?到目前为止你做了什么?我试图解决它但没有成功:(.我在SQL领域和编程方面都是新手。我希望从答案中我能学到东西。我在网上上了一门SQL课程。但是,我认为这还不够。我很高兴得到推荐的SQL课程,可以帮助我解决这类问题。谢谢。我试图解决它,但没有成功,所以请复制/粘贴您的内容我做了什么,你被卡住了。另外,我们恐怕不做你的键入::数据的图像是无用的,将被忽略。你需要复制/粘贴数据(作为格式化文本)也可以。您可以尝试使用或设置您的表和实验。请阅读本文:Top-N查询为绝对初学者解释。如果您仍然不理解,此查询将帮助您,我们可以在评论中讨论。谢谢:)如果你觉得这是正确的答案,请注明为答案。非常感谢你的帮助。这确实帮助了我很多。但是,1.如果问题是2015年,你为什么要把2014年作为答案(我也不确定我是否完全理解你的评论)?2.我看不出你已经记住999的最后一部分:。。。“此外,已经离开公司的女员工的销售额应低于员工编号999。提前感谢您的帮助”。再次感谢