如何比较同一个sql表中的行并获得最大值

时间:2015-09-02 03:25:36

标签: sql sql-server oracle

我有一张表如下

ID | Status ID | Value 
1  |   1       |  100
2  |   1       |  200
3  |   1       |  300
4  |   2       |  100 
5  |   2       |  150
6  |   2       |  200
7  |   3       |  500
8  |   3       |  300
9  |   3       |  150

我需要在状态中获得最大值。所以我的结果应该如下所示

ID | Status ID | Value 
3  |   1       |  300
6  |   2       |  200
7  |   3       |  500

我是SQL的新手,非常感谢您的投入

6 个答案:

答案 0 :(得分:2)

create table #temp(id int, statusid int, value int)
insert #temp(id,statusid,value)
select 1,1,100
union select 2,1,200
union select 3,1,300
union select 4,2,100 
union select 5,2,150
union select 6,2,200
union select 7,3,500
union select 8,3,300
union select 9,3,150

-- if you don't need the id
select statusid, max(value)
from #temp
group by statusid

-- if you need the id
select min(id), X.statusid, X.value
from (
    select statusid, max(value) value
    from #temp
    group by statusid
) X
inner join #temp T
on X.statusid = T.statusid
and X.value = T.value
group by X.statusid, X.value

答案 1 :(得分:1)

放手一搏:

SELECT t.*
FROM TEST t
INNER JOIN (
    SELECT STATUS,
           MAX(VALUE) AS MAX_VALUE
    FROM TEST
    GROUP BY STATUS) gt
ON t.STATUS = GT.STATUS
AND t.VALUE = gt.MAX_VALUE;

答案 2 :(得分:0)

您可以将查询编写为:

select * from data_table dt1
where dt1.value >= ALL (select value from data_table dt2 where dt2.Status = dt1.Status)

答案 3 :(得分:0)

我稍微改变了你的列名,但它会以你想要的方式工作。

   with tbl ( ID,StatusID,Val)as
              (select 1,1,100 from dual union
               select 2,1,200 from dual union
               select 3,1,300 from dual union
               select 4,2,100 from dual union
               select 5,2,150 from dual union
               select 6,2,200 from dual union
               select 7,3,500 from dual union
               select 8,3,300 from dual union
               select 9,3,150 from dual
      )          
     select T1.id,T1.statusid,T2.val
     from tbl T1,
     (SELECT statusid,max(val) AS val 
     FROM tbl GROUP BY statusid) T2
     WHERE T1.statusid=T2.statusid
     AND T1.val=T2.val

输出

    ID     StatusID    Val
    3        1         300
    6        2         200
    7        3         500

答案 4 :(得分:0)

DECLARE @a TABLE
(
    ID  int,
    StatusId int,
    Value  int
)

INSERT INTO @a
        ( ID, StatusId, Value )
VALUES  ( 1 ,   1      ,  100)
        ,(2 ,   1      ,  200)
        ,(3 ,   1      ,  300)
        ,(4 ,   2      ,  100 )
        ,(5 ,   2      ,  150)
        ,(6 ,   2      ,  200)
        ,(7 ,   3      ,  500)
        ,(8 ,   3      ,  300)
        ,(9 ,   3      ,  150)


SELECT 
    a.id, b.StatusId, b.Value
FROM 
    @a a INNER JOIN
    (
        SELECT 
            StatusId, MAX(Value) Value 
        FROM 
            @a
        GROUP BY
            StatusId
    ) b ON b.StatusId = a.StatusId AND b.Value = a.Value
ORDER BY 
    a.ID

答案 5 :(得分:0)

对于SQL Server,它是这样简单的:

select distinct 
FIRST_VALUE(ID) over (partition by Status order by Value desc) ID, 
Status, 
MAX(Value) over (partition by Status) Value
from tableName

另外,建议您不要使用ID,Status或Value等保留字。当然,SQL Server会搞清楚,但这是不好的做法。