My query is
SELECT * FROM email_operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() - INTERVAL '1 hour' )
to select all rows created at the last hour but it doesn't work. It throws an error at >=
SELECT * FROM email_operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() - INTERVAL '12 hour' )
> ERROR: operator does not exist: time without time zone >= timestamp with time zone
LINE 1: ..._operation WHERE cdate = CURRENT_DATE and ctime >= (NOW() ...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
> Time: 0.001s
Apparently ctime is a time column, so you can't compare it with a timestamp (which is what the error message tells you). You need to use current_time instead:
and ctime >= current_time - interval '1 hour'
Note that this won't work properly around midnight.
It seems you have split up date and time into two columns (which is a bad idea), but you can combine them to compare a timestamp:
and cdate + ctime >= current_timestamp - interval '1 hour'
If you want results in the past hour and you have separated the date and time components, then one method is:
WHERE (cdate + ctime) >= NOW() - INTERVAL '1 hour'
Note that this will even work between midnight and 1:00 a.m. Sadly, it probably won't use indexes. That might be an issue if you have lots of data. Here is one method that will at least use an index on cdate:
WHERE cdate IN (CURRENT_DATE, CURRENT_DATE - INTERVAL '1 day') AND
( (cdate + ctime) >= NOW() - INTERVAL '1 hour' )
where (cdate + ctime)::timestamp(0) >= (now() - interval '1 hour')
Related
I essentially want to run a query like:
SELECT * FROM t where date={time right now to the minute}
Use date_trunc() for your exact question:
where date = date_trunc('minute', now())
However, I suspect you really want the span of one minute:
where date >= date_trunc('minute', now()) and
date < date_trunc('minute', now()) + interval '1 minute'
I have rainfall measurements stored in a postgresql table and wish to select the min and and max value since the last occurence of 9am, whether that is 9am of the current day, if after 9am, or 9am of the previous day if before 9am. Have managed to select the values I need for the current day like so:
"select max(rain_mm) as maxrain,min(rain_mm) as minrain from weatherstation_000 where time_stamp > date_trunc('day',now());" But now want a simple way to do min and max rainfall since 9am as well, whether that be through the use of a conditional expression or perhaps a change of timezone??
You can use the OR condition as follows based of now().
select max(rain_mm) as maxrain,
min(rain_mm) as minrain
from weatherstation_000
where (now() > date_trunc('day', now()) + interval '9 hour'
and time_stamp > date_trunc('day', now()) + interval '9 hour')
OR ((now() <= date_trunc('day', now()) + interval '9 hour'
and time_stamp > date_trunc('day', now()) - interval '15 hour'))
You can subtract 9 hours:
select max(rain_mm) as maxrain,min(rain_mm) as minrain
from weatherstation_000
where time_stamp > date_trunc('day', now() - interval '9 hour');
I am trying to run this query was able to till some time ago. I don't know what went wrong and I started getting this error now?
Your database returned: ERROR: set-returning functions are not allowed in CASE Hint: You might be able to move the set-returning function into a LATERAL FROM item.
My query:
SELECT distinct
(CASE
WHEN {PERIOD} = 'Previous Quarter' AND pto.pto_start_date < (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date AND pto.pto_end_date >= (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date
THEN generate_series(pto.pto_start_date, pto.pto_end_date, '2 day'::interval)
WHEN {PERIOD} = 'Current Quarter' AND pto.pto_start_date < (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date AND pto.pto_end_date >= (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date
THEN generate_series(pto.pto_start_date, pto.pto_end_date, '1 day'::interval)
ELSE
generate_series(pto.pto_start_date, pto.pto_end_date, '1 day'::interval)
END) AS dt
FROM cust_pto pto
Start dates and end Dates:
What has gone wrong?
Why you're getting the error now: you upgraded to postgres 10. Set returning functions are no longer allowed.
What to do: there is more than one way to accomplish what you're trying to do. For the sake of keeping it as close as possible to your original query, all you have to do is put your CASE statement inside generate_series:
SELECT distinct generate_series(
pto.pto_start_date,
pto.pto_end_date,
CASE
WHEN {PERIOD} = 'Previous Quarter' AND pto.pto_start_date < (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date AND pto.pto_end_date >= (date_trunc('quarter', now() - INTERVAL '1 month') + INTERVAL '1 month')::date THEN
'2 day'::interval
ELSE
'1 day'::interval
END
) AS dt
FROM cust_pto pto
I know that I can get the date from today such as
select CURRENT_DATE - INTERVAL '1 months';
But what if I need the first date specified. Something like
select '2017-05-08 00:00:00' - INTERVAL '1 months'?
I tried different things and can't get it to work.
You could use:
select '2017-05-08 00:00:00'::DATE - '1 month'::INTERVAL
/\
|
cast as date
DBFiddle Demo
'2017-05-08 00:00:00' is a string literal. You need to convert it to a date, e.g., by using to_date:
SELECT TO_DATE('2017-05-08','yyyy-mm-dd') - INTERVAL '1 months'
I am trying to pull daily data using PostgreSQL. My problem is that the day seems to 'reset' at around 5 PM (Los Angeles Time). Is there a workaround this problem? Here is my query:
SELECT COUNT(distinct be.booking_id)AS "Number of Bookings Today"
FROM booking_events be
WHERE be.event IN ('approve') AND
be.created_at >= current_date AND
be.created_at < current_date + interval '1 day';
You can use hours to offset the current date. I think the logic is:
SELECT COUNT(distinct be.booking_id) as "Number of Bookings Today"
FROM booking_events be
WHERE be.event IN ('approve') AND
be.created_at >= current_date - interval '7 hour' AND
be.created_at < current_date + interval '1 day' - interval '7 hour';