[英]How to get last 24 hrs data in postgreSQL
I want to get last 24 hrs data. 我想获得最后24小时的数据。 i wrote a query in postgreSQL as follows. 我在postgreSQL中写了一个查询,如下所示。 But I couldn't get the answer as i expected. 但我无法按照我的预期得到答案。
SELECT startdate::timestamp AS startdate,
(DATE_PART('hour',startdate::timestamp)::integer) as hrs,count(guorderid)
FROM ord_entitlement
WHERE DATE_PART('Day',CURRENT_DATE::timestamp - startdate::timestamp) < 1
AND DATE_PART('hour',startdate::timestamp) <= 24
GROUP BY hrs,startdate
ORDER BY startdate
Instead of checking date parts, do the time math to get an interval. 而不是检查日期部分,做时间数学来获得间隔。 Use NOW() to get a timestamptz. 使用NOW()获取时间戳。
SELECT startdate::timestamp AS startdate,
(DATE_PART('hour',startdate::timestamp)::integer) as hrs,
count(guorderid)
FROM ord_entitlement
WHERE NOW() > startdate::timestamptz
AND NOW() - startdate::timestamptz <= interval '24 hours'
GROUP BY hrs,startdate
ORDER BY startdate
This ensures you will get the last 24 hours no matter what your time zone or daylight savings says. 无论您的时区或夏令时如何,这都可确保您获得最后24小时。 NOW() > startdate::timestamptz
ensures you don't accidentally pick up things from the future. NOW() > startdate::timestamptz
确保你不会意外地从未来拿起东西。
If you use CURRENT_DATE
you will not get the time instead use now()
function. 如果您使用CURRENT_DATE
,则不会使用now()
函数。 Try the following, 试试以下,
SELECT startdate::timestamp AS startdate,
(DATE_PART('hour',startdate::timestamp)::integer) as hrs,count(guorderid)
FROM ord_entitlement
WHERE DATE_PART('Day',now() - startdate::timestamptz) < 1
GROUP BY hrs,startdate
ORDER BY startdate
date_part()
works like extract()
, ie they will extract a subfield from the source: date_part()
工作方式类似于extract()
,即它们将从源中提取子字段 :
-- they will both yield 9 as result
select date_part('day', date '2015-01-09') "day part of 2015-01-09",
date_part('day', date '2015-02-09') "day part of 2015-02-09";
Extracting day(s)
therefore is not suited to select the last 24 hours. 因此,提取day(s)
不适合选择过去24小时。 Similarly extracting hour(s)
will (almost) always yield less than or equal to 24
. 类似地,提取hour(s)
几乎)总是小于或等于24
。
Extraction day(s)
from interval
(that's the result of substracting 2 timestamp
s) is a little different. 从interval
提取day(s)
是减去2个timestamp
的结果)有点不同。 The result might depend on, whether the interval is justified, or not: 结果可能取决于间隔是否合理:
-- they will both yield 1 as result
select date_part('day', interval '1 day') "day part of 1 day",
date_part('day', interval '1 month 1 day') "day part of 1 month 1 day";
-- they will yield 1, 32 and 397 respectively
select date_part('day', timestamp '2015-02-09' - timestamp '2015-02-08') "interval 1",
date_part('day', timestamp '2015-02-09' - timestamp '2015-01-08') "interval 2",
date_part('day', timestamp '2015-02-09' - timestamp '2014-01-08') "interval 3";
Depending on the fact, that the timestamp subtraction is not giving justified intervals is not the best option, I think. 根据事实,我认为时间戳减法没有给出合理的间隔不是最好的选择。 You could use simpler conditions to achieve your goal: 您可以使用更简单的条件来实现目标:
-- if startdate is a timestamp:
where current_timestamp - interval '1 day' <= startdate
-- if startdate is a date:
where current_date - 1 <= startdate
If you want to disallow future dates too (as your question's title suggests), you could use a single between
condition: 如果你想禁止将来的日期太(你的问题的标题所暗示的),你可以使用一个单一的between
条件:
-- if startdate is a timestamp:
where startdate between current_timestamp - interval '1 day' and current_timestamp
-- if startdate is a date:
where startdate between current_date - 1 and current_date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.