如何找到表三行总行数的前三个工资总和

时间:2014-02-24 07:06:59

标签: sql

如何找到表中六行总行数的前三个工资的总和。

id      Salary
01       100
02       200
03       300
04       400
05       500
06       600

3 个答案:

答案 0 :(得分:1)

尝试使用这个......

SELECT SUM(salary) 
FROM
    (SELECT
        ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER, salary
     FROM tablename) AS foo
WHERE 
    ROW_NUMBER <= 3

答案 1 :(得分:0)

你的意思是:

SELECT SUM(Salary)
FROM (
    SELECT Salary FROM your_table ORDER BY id LIMIT 3
) A

答案 2 :(得分:0)

假设它是SQL Server:

 SELECT SUM(Salary) As SalaryTotal
 (    
     SELECT TOP 3 * FROM your_table ORDER BY ID
 )

如果你想要最高3薪水的工资,那么你可以按工资订单:

SELECT SUM(Salary) As SalaryTotal
(    
    SELECT TOP 3 * FROM your_table ORDER BY  salary DESC    
)