I am using this query
select * from table_nm where table_nm_date > NOW() - INTERVAL '24 hour'
But giving today's records too. Please help me.
Output : "2016-03-20 19:31:11.896159",
"2016-03-21 08:24:58.223245",
"2016-03-21 09:13:59.768953",
"2016-03-21 09:51:25.161428",
"2016-03-21 11:35:07.378706"
I only want 2016-03-20 data.
If you want yesterday's data, filter for date only:
SELECT *
FROM table_nm
WHERE table_nm_date BETWEEN CURRENT_DATE - 1 AND CURRENT_DATE
(which is an index-friendly variant of:)
WHERE table_nm_date::date = CURRENT_DATE - 1
Assuming table_nm_date is a usual date-time like data type
then your query turns down to select "any entry from the last 24 hours"
If you want to exclude "todays" records you need to exclude those in an appropriate way, e.g by using table_nm_date between START_OF_WINDOW and END_OF WINDOW, setting both borders as it suits your needs.
Related
I need to create a condition that will filter for dates in the next 8 quarters worth of data, based on the current quarter. The issue is this needs to be rolling. For example, as I'm writing this, it is 2/2/2023. The data condition would need to be such that it takes the data in the current quarter (i.e., Q1), and subsequently the current quarter +1, + 2, etc. The issue is when I get to a new year, as PostgreSQL does not allow you to do datefield + interval '1 quarter'. So, when I need an automated way of pulling the data for Q1 in 2023, I can't simply use the interval. This is also an issue when, say, I get to Q4. I can't do datefield + interval '1 quarter', because that gives Q5, which does not exist.
Any tips for getting this taken care of? My current thinking is that i need to create conditional logic where, if current quarter is Q4, filter where data is in Q1 of current year + 1, but am wondering if there are more efficient ways of doing this.
My current (and incomplete) solution is as follows:
select *
from mytable
where extract(quarter from datefield) = extract(quarter from current_date + interval '1 quarter')
and datefield >= concat(extract(year from current_date), '-01-01')::date
and datefield <= current_date + interval '2 years'
Thanks!
After another hour or so of digging, I found another post which answered this question:
see here
The solution relies on date_trunc, which makes lots of sense.
I want to subract additional time in $__timeFilter in grafana. Like if I have selected Last 7 days, I want to run 2 queries which do a comparison like one query gives me avg cpu utilization for last 7 days and another one gives me avg cpu utilzation for now() - 14d to now() - 7d. And this is dynamic. I can get for 6hrs, 2days or anything selected.
My database is TimescaleDB and grafana version in 8.3.5
Edit
Query is
select avg(cpu) from cpu_utilization where $__timeFilter(timestamp)
Whatever is selected in the time filter in grafana, the query is manipulated accordingly
Now with grafana understands this query becomes as following. if I select last 24hrs
select avg(cpu) from cpu_utilization where timestamp BETWEEN '2022-09-07 05:32:10' and '2022-09-08 05:32:10'
This is normal behaviour. Now I wanted that if I select last 24hrs, this query to behave as it is but an additional query becomes
select avg(cpu) from cpu_utilization where timestamp BETWEEN '2022-09-06 05:32:10' and '2022-09-07 05:32:10'
(I just don't want it for last 24hrs, but any relative time period selected in the filter)
Answer : https://stackoverflow.com/a/73658919/14817486
You can use the global variables $__to and $__from.
For example, ${__from:date:seconds} will give you a timestamp in seconds. You can then subtract 7 days (= 604800 seconds) from it and use it in your query's WHERE clause. Depending on your SQL dialect, that might be by using TIMESTAMP(), TO_TIMESTAMP() or something similar. So it would look similar to this:
[...] WHERE timestamp BETWEEN TO_TIMESTAMP(${__from:date:seconds}-604800) AND TO_TIMESTAMP(${__to:date:seconds}-604800) [...]
Interesting question! If I understood correctly, you could use the timestamp column as the reference as the grafana is already filtering by this to the comparison query. So you can get the min(timestamp) and max(timestamp) to know the limits of your period and then build something from it.
Like min(timestamp) - INTERVAL '7 days' would give you the start of the previous range, and max(timestamp) - INTERVAL '7 days' would offer the final scope.
I recently switched to using postgreSQL and I am having same difficulties finding an alternative for timestampiff
My original query was something like
select a.column_a, a.column_b, TIMESTAMPDIFF(SQL_TSI_MINUTE, a.TIME_START, now()) AS "Time_diff"
from table as a
where
...
I can do just ( now() - a.TIME_START) but i want to show the result in minutes. Is there any better alternatives in postgresql to do the subtraction from now and show the result just as minutes ?
now() - a.TIME_START returns an interval which can be converted to seconds using extract() and those can be converted to minutes:
extract(epoch from now() - a.time_start) / 60 as diff_minutes
If you don't need it as a number, another option is to simply format the interval to show minutes and seconds
to_char(now() - a.time_start, 'mi:ss')
Note that this will hide the information if the interval was bigger than 60 minutes (or bigger than a day). If that can happen as well, maybe you want to use 'dd hh24:mi:ss' as a format mask instead
I want to set day or hour time interval between 2 columns to get lates actual records, if I dont set this I got outdated information when I filter by day.
TableA: create_date
TableB: last_access_date
SO I did this
SELECT *
FROM acc_zone_person
WHERE create_time::date = last_access_time::date
I get daily information no outdated information, but at midnight all information between 23:00 - 00:00
disgarded I want to put interval so that my evening information from 19:00 will be present till 06:00 data.
I used
SELECT *
FROM acc_zone_person
WHERE last_access_time::date >= now() - interval '12 hours'
this time when I switch to older dates, I dont get any data, only data within 12 hours
so I need to find a way do something like this
SELECT *
FROM acc_zone_person
WHERE create_date::date >= last_access_time - interval '12 hours'
It should take create_date as NOW and get 12 hours interval data of last_access_time
According to your own statement: "It should take create_date as NOW ... "
SELECT *
FROM acc_zone_person
WHERE now()::date >= last_access_time - interval '12 hours'
I am a little confused here. Why couldn't you just use the last query you posted? Does it not work? Do you just need to convert the last_update_time to a date?
I need to go through every row in a table and set every date in a particular column to the date before its current value (minus 14 hours, previous day, etc).
I could write a script to do this but I was wondering if there was a better SQL method?
Thanks!
UPDATE yourtable SET thefield = thefield - interval '14 hour';
relevant docs here, which should have been your first place to check.