复杂的DISTINCT,ORDER BY,WHERE sql select语句

时间:2013-09-10 23:33:58

标签: sql db2

我对SQL(使用DB2)不是很熟悉,我试图选择符合以下条件的不同ID:

select distinct a.ID
from DOGTOWN a
where a.DOGTOWNID > 0
  and (a.DELIVERYDATE < (CURRENT_TIMESTAMP - 4680 DAYS))
order by a.ID ASC;

这给我留下了4条记录:

2100353550
2100353779
2100356002
2100356008

这些回报很好,但是当我试图这样做时:

select distinct a.ID 
from DOGTOWN a 
where 
    a.DOGTOWNID > 2100356002 
    and a.DELIVERYDATE < (CURRENT_TIMESTAMP - 4680 DAYS) 
order by 
    a.ID ASC 
fetch first 3 rows only

我期待在这种情况下记录2100356008但它返回一个空的结果集..

感谢这个伟大社区的所有专家。

1 个答案:

答案 0 :(得分:0)

没关系,只是比较错误的id ....答案是:

select distinct a.ID 
from DOGTOWN a 
where 
    a.ID > 2100356002 
    and a.DELIVERYDATE < (CURRENT_TIMESTAMP - 4680 DAYS) 
order by 
    a.ID ASC 
fetch first 3 rows only