提取具有最大值的行

时间:2016-05-19 15:15:51

标签: mysql sql database oracle rdbms

我运行此查询:

SELECT
    orderid AS "id", MAX(streamSeq) AS "mxseq", fillQty AS "execQty"
FROM 
    matchit_uat.matchit_messages o
WHERE 
    (o.date >= to_timestamp(1463630400)) AND 
    (o.date <= to_timestamp(1463702400)) AND 
    orderid= 2001231
GROUP BY 
    orderId, fillQty

..并得到结果:

id      mxseq       execQty
2001231 212438879   (null)
2001231 212439080   1012

我现在想要提取最大mxseq的行。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

使用派生表获取每个orderid的最大streamSeq,并将结果join获取到原始表。 <{1}} fillQty不是必需的。

group by

答案 1 :(得分:0)

在Oracle 12c +中,您将使用ANSI标准语法:

SELECT orderid AS "id", MAX(streamSeq) AS "mxseq", fillQty AS "execQty"
FROM matchit_uat.matchit_messages o
WHERE (o.date >= to_timestamp(1463630400)) AND 
      (o.date <= to_timestamp(1463702400)) AND 
      orderid = 2001231
GROUP BY orderId, fillQty
ORDER BY MAX(streamSeq) DESC
FETCH FIRST 1 ROW ONLY;

在MySQL中,您将最后一行替换为LIMIT 1

在早期版本的Oracle中,您可以使用子查询和where rownum = 1

答案 2 :(得分:0)

使用它:

.size

有关更多信息,请参阅oracle链接: http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174