在 SQL 中查找 MAX(DATE) 值
Finding MAX(DATE) value in SQL
我是SQL的新手,所以这个问题-
我的 table 中有一个 DATE
列,我的查询应该使用最新的 DATE
获取值。我已经意识到我可以通过以下任何一种方式实现它-
SELECT TRUNC(MAX(createddatetm)) cdt
FROM (SELECT TO_TIMESTAMP(TO_CHAR(createddate, 'Month dd, yyyy hh:mi AM'), 'Month dd, yyyy hh:mi AM') as createddatetm
FROM comments WHERE commentid = '1234');
SELECT trunc(MAX(TO_TIMESTAMP(to_char(createddate, 'Month dd, yyyy hh:mi AM'), 'Month dd, yyyy hh:mi AM')))
FROM (SELECT createddate
FROM comments
WHERE commentid = '1234');
SELECT TRUNC(MAX(createddatetm)) cdt
FROM (SELECT TO_CHAR(createddate, 'Month dd, yyyy hh:mi AM') as createddatetm
FROM comments WHERE commentid = '1234');
SELECT trunc(MAX(to_char(createddate, 'Month dd, yyyy hh:mi AM')))
FROM (SELECT createddate
FROM comments
WHERE commentid = '1234');
这是我的问题-
还有其他更简单的方法可以实现这一目标吗?或者我应该使用我提到的任何这些吗?此外,这些查询在性能方面是否存在差异?我认为没有,但我需要确认一次。
为什么要将日期转换为 varchar 以再次将其转换回 date/timestamp?您可以 compare/use 存储在数据库中的日期值。
您的问题可以使用 window 函数轻松解决:
select *
from (
select c.*,
row_number() over (order by createddate desc) as rn
from comments
where commentid = 1234
) t
where rn = 1;
或者使用 max()
select *
from (
select c.*,
max(createddate) over () as max_createddate
from comments
where commentid = 1234
) t
where max_createddate = createddate;
或者如果您需要多个 commentid 值:
select *
from (
select c.*,
row_number() over (partition by commentid order by createddate desc) as rn
from comments
) t
where rn = 1;
当您只是在寻找一个简单的值时,为什么要嵌套 Select 语句?
select trunc( max( createddate )) cdt
from comments
where commentid = '1234';
我是SQL的新手,所以这个问题-
我的 table 中有一个 DATE
列,我的查询应该使用最新的 DATE
获取值。我已经意识到我可以通过以下任何一种方式实现它-
SELECT TRUNC(MAX(createddatetm)) cdt FROM (SELECT TO_TIMESTAMP(TO_CHAR(createddate, 'Month dd, yyyy hh:mi AM'), 'Month dd, yyyy hh:mi AM') as createddatetm FROM comments WHERE commentid = '1234');
SELECT trunc(MAX(TO_TIMESTAMP(to_char(createddate, 'Month dd, yyyy hh:mi AM'), 'Month dd, yyyy hh:mi AM'))) FROM (SELECT createddate FROM comments WHERE commentid = '1234');
SELECT TRUNC(MAX(createddatetm)) cdt FROM (SELECT TO_CHAR(createddate, 'Month dd, yyyy hh:mi AM') as createddatetm FROM comments WHERE commentid = '1234');
SELECT trunc(MAX(to_char(createddate, 'Month dd, yyyy hh:mi AM'))) FROM (SELECT createddate FROM comments WHERE commentid = '1234');
这是我的问题-
还有其他更简单的方法可以实现这一目标吗?或者我应该使用我提到的任何这些吗?此外,这些查询在性能方面是否存在差异?我认为没有,但我需要确认一次。
为什么要将日期转换为 varchar 以再次将其转换回 date/timestamp?您可以 compare/use 存储在数据库中的日期值。
您的问题可以使用 window 函数轻松解决:
select *
from (
select c.*,
row_number() over (order by createddate desc) as rn
from comments
where commentid = 1234
) t
where rn = 1;
或者使用 max()
select *
from (
select c.*,
max(createddate) over () as max_createddate
from comments
where commentid = 1234
) t
where max_createddate = createddate;
或者如果您需要多个 commentid 值:
select *
from (
select c.*,
row_number() over (partition by commentid order by createddate desc) as rn
from comments
) t
where rn = 1;
当您只是在寻找一个简单的值时,为什么要嵌套 Select 语句?
select trunc( max( createddate )) cdt
from comments
where commentid = '1234';