2017-07-06 33 views
0

私は2つのテーブルSalesとEmployeesをpostgresqlに添付しています。 私は計算する必要があります。また2015年で最も高い売り上げでトップ4人の従業員を返す クエリは、すでに会社を去ってきたemployessの売上高は、従業員番号の下にあなたの助け2つのテーブルを1つのクエリに組み合わせる

フォイ事前に999 感謝しなければなりませんテスト済み

Sales Employess

+3

この記事を読んでください、その受け入れ答えます。https://meta.stacko verflow.com/questions/285551/why-not-to-upload-images-of-code-on-so-wing-asking-a-question/285557#285557 –

+0

Sooo ..あなたが私たちにあなたの宿題をして欲しい?これまで何をしていますか? –

+0

私はそれを解決せずに解決しようとしました:(私はSQLの分野とプログラミングでは新しく、答えから私は学ぶことができたと思っていました。私はそのような質問を理解するのに役立つことができるSQLの推奨コースを取得することを嬉しく思っています。ありがとう – DjangoPy

答えて

0

推奨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 
+0

この質問があなたに役立つことを願っています。ありがとう:) –

+0

あなたはこれが正しいansだと思うなら、答えとしてマークしてください。 –

+0

おかげさまで助けてください。それは私を助けてくれました。しかし、1。 2015年に質問がある場合、なぜ2014年を置いたのですか(私はあなたのコメントを完全に理解していると確信していません)? 2.私はあなたが999の最後の部分を修得しているのを見ません:... "さらに、すでに会社に残っている雇用の売却は従業員番号999の下にあるべきです。再度、感謝します – DjangoPy

関連する問題