如何选择列的最大值

时间:2017-10-10 13:59:03

标签: sql oracle

我的问题看似简单,但我无法弄明白。

这就是我所拥有的:

TIME           MAXVERSION
13-12-31          1
13-12-31          2
14-12-31          1
14-12-31          2
14-12-31          3
15-12-31          1
15-12-31          2
15-12-31          3
16-12-31          1

这就是我想要的:

TIME           MAXVERSION
13-12-31          2
14-12-31          3
15-12-31          3
16-12-31          1

我试过这个:

select referencetid as time max(version) over (partition by referencetid order by version desc) as maxversion
from D101200.PSD_BEFOLKNING_REFERENCE
where to_char(referencetid, 'mm-dd') = '12-31'
and to_char(referencetid, 'yy-mm-dd') between ('07-12-31') and ('16-12-31')
order by referencetid;

这给了我一些我想要的,但不是完全的:

TIME           MAXVERSION
13-12-31          2
13-12-31          2
14-12-31          3
14-12-31          3
14-12-31          3
15-12-31          3
15-12-31          3
15-12-31          3
16-12-31          1

这给了我正确的版本,但数据集的大小没有缩小,这就是我想要的。

任何帮助都非常明显!

5 个答案:

答案 0 :(得分:3)

您在寻找GROUP BY吗?

     select Time,
            Max(MaxVersion) as MaxVersion  
       from MyTable
   group by Time
   order by Time

答案 1 :(得分:3)

您需要修改日期算术!始终在查询中使用ISO标准YYYY-MM-DD(或YYYYMMDD)格式。不要使用字符串进行日期比较。

您的问题的答案很简单group by

select referencetid as time, max(version)
from D101200.PSD_BEFOLKNING_REFERENCE
where to_char(referencetid, 'mm-dd') = '12-31' and
      referencetid between date '2007-12-31' and date '2016-12-31'
group by referencetid
order by referencetid;

假设你有referencetid的索引,我建议你只是明确列出这些值:

select referencetid as time, max(version)
from D101200.PSD_BEFOLKNING_REFERENCE
where referencetid in (date '2007-12-31', date '2008-12-31', date '2009-12-31',
                       date '2010-12-31', date '2011-12-31', date '2012-12-31',
                       date '2013-12-31', date '2014-12-31', date '2015-12-31',
                       date '2016-12-31')
group by referencetid
order by referencetid;

这使得查询更容易使用索引进行优化。

答案 2 :(得分:1)

今天似乎有很多问题询问31st December for the last N years

SELECT referencetid as time,
       max(version) as maxversion
FROM   D101200.PSD_BEFOLKNING_REFERENCE
WHERE  referencetid IN (
         DATE '2016-12-31',
         DATE '2015-12-31',
         DATE '2014-12-31',
         DATE '2013-12-31',
         DATE '2012-12-31',
         DATE '2011-12-31',
         DATE '2010-12-31',
         DATE '2009-12-31',
         DATE '2008-12-31',
         DATE '2007-12-31'
       )
GROUP BY referencetid
ORDER BY referencetid;

在日期列上使用TO_CHAR(或其他函数)作为过滤器的一部分将阻止查询使用该列上的任何索引(您需要基于函数的索引)。

答案 3 :(得分:0)

select 
referencetid as time, 
max(version)as maxversion
from D101200.PSD_BEFOLKNING_REFERENCE
where to_char(referencetid, 'mm-dd') = '12-31'
and to_char(referencetid, 'yy-mm-dd') between ('07-12-31') and ('16-12-31')
group by referencetid 
order by referencetid

您可以使用此代码

答案 4 :(得分:0)

正如许多人已经回答的那样,经过一些考虑,你可以查找该组,请查看this或其他示例here以了解如何使用它,这是一项很好的服务尝试查询sqlfiddle

select 
    referencetid as Time,
    max(version) as maxversion

from D101200.PSD_BEFOLKNING_REFERENCE

where to_char(referencetid, 'mm-dd') = '12-31'
   and to_char(referencetid, 'yy-mm-dd') between ('07-12-31') and ('16-12-
 31')
group by referencetid 
order by referencetid;