WITH company_size(comp_id, employee_count) AS (
SELECT comp_id, count(*)
FROM position NATURAL JOIN works
GROUP BY comp_id),
total_salaries(total_salaries) AS(
SELECT sum(pay_rate)
from position)
SELECT comp_id, employee_count, total_salaries
FROM company_size NATURAL JOIN total_salaries
WHERE employee_count = (SELECT max(employee_count)
FROM company_size);
所以这是我正在处理的查询。我得到了工作,以便在雇员人数方面获得最大雇主,但现在我必须找到从该特定公司支付的工资和工资总额。我有总薪资,但这只是给了我所有公司的总和。如何才能从那个公司获得总工资?
答案 0 :(得分:0)
我没有您的表格可以使用,但我希望class LoginView(View):
def get(self, request):
return render(request, "login.html", {})
def post(self, request):
login_form = LoginForm(request.POST)
if login_form.is_valid():
user_name = request.POST.get("username", "")
pass_word = request.POST.get("password", "")
user = authenticate(username=user_name, password=pass_word)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
else:
return render(request, "login.html", {"msg": "用户未激活!"})
else:
return render(request, "login.html", {"msg": "用户名或密码错误!"})
else:
return render(request, "login.html", {"login_form": login_form})
在NATURAL JOIN
和company_size
之间工作,他们都需要有一个列才能加入上。实际上,total_salaries
返回一行包含单行。也许如果它在total_salaries
上分组,你会得到更好的结果。
或者为了让它更加轻松,只需将comp_id
移出sum(pay_rate)
并进入total_salaries
查询即可。然后,您可以取消company_size
查询并加入total_salaries
:
company_size
答案 1 :(得分:0)
我认为这是你正在寻找的东西。您获得了所有公司的总和,因为您只是在total_salaries查询中选择了值,而您并未将其加入任何内容。添加id字段和group by将聚合数据,然后你只需要将它连接到某个东西。希望有所帮助。
WITH company_size(comp_id, employee_count) AS (
SELECT comp_id, count(*)
FROM position NATURAL JOIN works
GROUP BY comp_id),
total_salaries(total_salaries, comp_id) AS(
SELECT sum(pay_rate), comp_id
FROM position
GROUP BY comp_id)
SELECT comp_id, employee_count, total_salaries
FROM company_size cs INNER JOIN total_salaries ts ON cs.comp_id = ts.comp_id
WHERE employee_count = (SELECT max(employee_count)
FROM company_size);