In some special case we could use ORDER BY clause instead of using MAX and MIN function. it makes the SQL more simple.

See following sample, the emphasis line is the row wanted.

select * from zytst.t@

part_num    group    plan_date
---------------------------------------
ADB            1         2006-02-12
ABB            1         2006-02-13
ACB           1          2006-02-13
AGB            2         2006-02-16

Following 2 SQLs get the same result in this case.

select part_num from zytst.t where group = 1
order by plan_date desc, part_num desc
fetch first 1 row only@

part_num    group    plan_date
---------------------------------------
ACB           1          2006-02-13

 

select max(part_num) 
from (select t1.part_num from zytst.t t1
        where t1.group = 1   
        and t1.plan_date =
            (select max(plan_date) from zytst.t t2
            where t2.group = t1.group) ) as t3
@

part_num    group    plan_date
---------------------------------------
ACB           1          2006-02-13

Post a comment

mail.png


相似文章|Related Entries

最近更新|Recent Entries

不定期更新|Handy Entries

相似标签|Related Tags

分类栏目|Categories

按月归档|By Month

2008
01
2007
12
10
07
06
05
04
03
02
01
2006
12
11
10
09
08
07
06
05
04
03
02
01
2005
11
10
09
08
07
04
03
2004
12
11
10
09
08
07
06
05
04
03
02
01
2003
12
10
09
08
06
2002
09
08
04
03
02
2001
12
09
07
06
05

站内链接|Site Links

Powered by
Movable Type 3.34