我试图返回一个总和,它是返回的多行数据的总和。我试图返回显示员工,帐户和部门的数据。我想要这三个数据点支付的工资总和。我还想要一份员工的款项来说明他们的工资是如何分配的。一个例子如下。任何帮助,将不胜感激。
答案 0 :(得分:0)
您可以使用SUM
分析函数在窗口子句中使用不同的组来实现此目的。
SELECT employee
, account
, dept
, SUM (salary) OVER (PARTITION BY employee) AS employee_salary
, SUM (salary) OVER (PARTITION BY dept) AS dept_salary_paid
, SUM (salary) OVER (PARTITION BY account) AS acct_salary_paid
FROM mytable
我不确定这些分组是否正是您想要的(也许我误解了您的问题),但它应该给您正确的想法。窗口子句为您提供了更大的灵活性,允许您按不同的值对总和进行分组,而GROUP BY
强制每SELECT
个单独分组。
根据您最近的更新进行修改:
您不需要GROUP BY
和窗口条款。您可以执行两个不同的OVER(PARTITION BY ....)
子句,因此它看起来像这样:
select employee
, activity
, SUM (salary_amt) OVER (PARTITION BY employee, activity) AS "total salary by employee and activity"
, SUM (salary_amt) OVER (PARTITION BY employee) AS "total salary by employee"
from mytable where employee = 'xxx'
答案 1 :(得分:0)
选择员工,活动,SUM(salary_amt),SUM(salary_amt)OVER(PARTITION BY员工)AS"员工总薪水" 来自mytable 员工=' xxx' 按员工分组,活动
我得到的错误是员工的总和不是按表达式分组
答案 2 :(得分:-1)
这将返回每位员工的工资总和
SELECT employee, SUM(salary)
FROM Table
GROUP BY employee
答案 3 :(得分:-2)
以McGothlin已发布的内容为基础:
SELECT employee
, account
, dept
, salary
, SUM (salary) OVER (PARTITION BY employee) AS "total salary by employee"
, ROUND(salary / SUM (salary) OVER (PARTITION BY employee) * 100) || '%' AS "salary distribution"
FROM mytable