Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgreSQL中获取最近的日期?_Postgresql - Fatal编程技术网

如何在postgreSQL中获取最近的日期?

如何在postgreSQL中获取最近的日期?,postgresql,Postgresql,有一个guaccount,有3个不同的guarderid值和不同的enddate。对于特定的guaccount,我只需要获取最新的enddate select guaccountid,guorderid,enddate from ord_entitlement where guaccountid ='15031021282118408' ORDER BY enddate DESC 我将使用行编号()窗口功能: SELECT guaccountid, guorderid,

有一个
guaccount
,有3个不同的
guarderid
值和不同的
enddate
。对于特定的
guaccount
,我只需要获取最新的
enddate

select guaccountid,guorderid,enddate
from ord_entitlement
where guaccountid ='15031021282118408'
ORDER BY enddate DESC 

我将使用
行编号()
窗口功能:

SELECT   
  guaccountid, 
  guorderid,
  enddate
FROM (
  SELECT
    guaccountid, 
    guorderid,
    enddate, 
    row_number() over (
      partition by guaccountid 
      order by to_timestamp(enddate, 'YYYY-MM-DD"T"HH24:MI:SS') desc
    ) r
  FROM ord_entitlement
  WHERE guaccountid = '15031021282118408'
    AND substr(enddate, 1, 4) != '9999'
  ) src
WHERE r = 1;
这将返回:

|       guaccountid |         guorderid |            enddate |
|-------------------|-------------------|--------------------|
| 15031021282118408 | 15031708485830901 | 2015-04-16T08:42:1 |
如果从
where
子句中删除
guaccountid='1503102128218408'
,您将获得任何
guaccountid
的最新记录

select guaccountid,guorderid,enddate
from ord_entitlement
where guaccountid ='15031021282118408'
ORDER BY enddate DESC 

子查询中的窗口函数不是有点过头了吗?您可能在子查询中做了很多对输出没有影响的工作。@Patrick当然,它做了更多的工作-虽然我不会说太多,但如果accountid受到约束,肯定不会说太多-但最终它只给出了包含最新日期的行的正确结果,而没有其他内容。我想获得所需的数据应该值得额外处理。