![](/img/trans.png)
[英]In SQL Server 2008, how to write query to get top 10 records in group wise and I want to display that in single row column like pivot
[英]How to write a single SQL query to get aggregate of column as other column in SQL Server 2008?
我去年遇到的确切问题。 希望能有所帮助。
SELECT e1.EmpId,
e1.EmpName
e1.EmpSalary,
SUM(e2.EmpSalary) AS Aggregate_Salary
FROM Employee e1 JOIN Employee e2
ON e1.id >= e2.id GROUP BY e1.EmpId,
e1.EmpName,
e1.EmpSalary
使用相关子查询:
select EmpId, EmpName, EmpSalary, (select sum(EmpSalary) from Employee e2
where e2.EmpId <= e1.EmpId) as AggregateSalary
from Employee e1
您没有指定DBMS,所以这是ANSI SQL:
select empid,
empname,
empsalary,
sum(empsalary) over (order by empid) as aggregate_salary
from employee
order by empid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.