我需要返回一个具有最大值和另一个值的行的行

时间:2015-12-02 16:05:39

标签: sql-server

这个例子就是这个

20443204    0.00    10030089    4

20443204    9.40    10030324    3

20443204    13.00   10011672    2

20443204    13.00   10030324    1

结果应为

20443204    13.00   10030324    1

所以第4列需要最小但这并不像第2列那样重要。所以拉完后

20443204    13.00   10011672    2

20443204    13.00   10030324    1

查询应该带来正确的结果,第4列为1

我尝试了什么

    select job_employee.job_no, MAX(job_employee.act_hours) hours , employee.emp_no, MIN(job_employee.seqno)  from masdb.dbo.job_employee
    join masdb.dbo.employee on employee.emp_no = job_employee.emp_no
    where job_no = 20443204--example
   group by
    job_employee.job_no, employee.emp_no
    order by seqno desc

1 个答案:

答案 0 :(得分:1)

您的查询可能是这样的:

    ;WITH MaxValue (H) AS
        (SELECT MAX(Hour) FROM Table_2)
        ,MinValue(S) AS
        (SELECT MIN(segno) FROM Table_2 WHERE hour IN (SELECT H FROM MaxValue))
    SELECT * 
    FROM Table_2 
    WHERE segno IN (SELECT S FROM MinValue) AND job_no = 20443204