How to get the records from the past 1 hour in psql - psql

SELECT * FROM auto_sync WHERE time > UNIX_TIMESTAMP(NOW() - INTERVAL 1 HOUR);
I am trying to fetch data which get added in the past 1 hour from my query but it's throwing me an error on Internal 1. Thank you

Try this.
SELECT * FROM table_name
WHERE timestamp >= NOW() - INTERVAL 1 HOUR
ORDER BY timestamp DESC

SELECT *
FROM table_name
WHERE timestamp >= NOW() - INTERVAL 1 HOUR
ORDER BY timestamp DESC
try this maybe? not sure if timestamp or time, try with both

Related

InfluxDB sum returned values with same time

I'm trying to retrieve the sum of same values that has the same timestamp.
My query is
SELECT value FROM dashboards WHERE time >= '2021-03-07T00:00:00Z' AND time <= '2021-03-09T00:00:00Z'
My returned values are
time value
---- -----
2021-03-07T00:00:00Z 1
2021-03-07T00:00:00Z 1
2021-03-07T00:00:00Z 1
2021-03-08T00:00:00Z 2
2021-03-08T00:00:00Z 2
2021-03-08T00:00:00Z 2
2021-03-09T00:00:00Z 3
2021-03-09T00:00:00Z 3
2021-03-09T00:00:00Z 3
How can I change my query the result will be
time sum
---- -----
2021-03-07T00:00:00Z 3
2021-03-08T00:00:00Z 6
2021-03-09T00:00:00Z 9
SELECT SUM(value) FROM dashboards WHERE time >= '2021-03-07T00:00:00Z' AND time <= '2021-03-09T00:00:00Z' GROUP BY time(1h) FILL(none)
GROUP BY time(1h) - group results by time column with interval of 1h
FILL(none) - ignore empty results

Select records created between a time range regardless the date of creation

I want to select a set of records created between 2 am to 3 am, regardless of the date it was created. Is there any way to achieve this?
Database: PostgreSQL
With PostgreSQL:
Model.where("created_at::time BETWEEN '14:00:00' AND '15:00:00'")
P.S. Before dealing with time queries make sure you are aware of the Rails default timezone (UTC) and issues connected to users' timezone differences.
You can make use of extract function in PostgreSQL to extract the time from date_time
SELECT * FROM users WHERE extract(hour from created_at) BETWEEN 2 and 3;
Is there any possibility to check between 23:00:00 to 01:00:00.
The same will not work with time BETWEEN 23 and 1 but you can achieve it the following way:
SELECT * FROM users WHERE extract(hour from created_at) >= 23 and extract(hour from created_at) < 1;

make start of week as monday or sunday influxdb

I am using influx db 1.0.2 version. when I query to get aggregate of 1 week. week of day starts from Thursday instead of monday/sunday.
SELECT SUM(value) FROM measurement_name WHERE time >= '2016-10-09T18:30:00Z' AND time < '2016-11-07T18:29:59Z' GROUP BY time(1w)
How it can be configured?
this ugly bug can be fixed by adding offset into grouping by time.
SELECT SUM(value) FROM measurement_name WHERE time >= '2016-10-09T18:30:00Z' AND time < '2016-11-07T18:29:59Z' GROUP BY time(1w, 4d)
for more information, click here

Get data of last days ago in rails

I want to get data of days ago.But I want that it start from 00:00
ViewsLog.where('created_at >= ?', 1.days.ago)
But it get data from the same time as from now.I want that it get data from 00:00:00
SELECT "views_logs".* FROM "views_logs" WHERE (created_at >= '2015-12-07 14:16:22.346497')
You can try some thing like
ViewsLog.where('created_at >= ?', 1.day.ago.to_datetime)
This will create SQL query exactly like
SELECT "views_logs".* FROM "views_logs" WHERE (created_at >= '2015-12-07 00:00:00.000000')
1.days.ago.beginning_of_day or
Time.now.beginning_of_day - 1.day
readme more at http://guides.rubyonrails.org/active_support_core_extensions.html#extensions-to-datetime

How to Group by last 20 days and do an aggregate function?

I can't seem to figure this one out. I'm trying to get the standard deviation of a column for the past 20 days. Here is what I have
Model.where('date < ?','2013-03-25')
.group('date')
.order('date DESC')
.limit(20)
.select('stddev_samp(percent_change) as stdev')
However all I'm getting is 20 entries of Nil. I was expecting 1 entry of the standard deviation.
After switching the stddev_samp to sum, I see that I'm getting nil because you can't have a standard deviation on 1 entry. I.e. It is not grouping the 20 as I expected, but calculating standard deviation on each date.
So my question is, how do I get stddev of the last 20 days? I know it's possible to simply choose select percent_change and then calculate the standard deviation in ruby, but I assume that the aggregate function stddev_samp should be usable in this case.
I am using rails 3.2 and Postgresql 9.2
I'm not a Ruby guy so I'll explain it in normal SQL:
What you're doing is:
SELECT stddev_samp(percent_change) as stdev
FROM tbl
WHERE date < '2013-03-25'
GROUP BY date
ORDER BY date DESC
LIMIT 20;
This calculates the deviation for each day seperately, not for the sum of them, and when you try to get the deviation of only one element you get NULL.
Removing the GROUP BY would fix it but also would return the result for the whole table not just last 20 entries so we need a subquery:
SELECT stddev_samp(percent_change) as stdev
FROM
(SELECT percent_change
FROM tbl
WHERE date < '2013-03-25'
ORDER BY date DESC
LIMIT 20) AS q
No need to 'Group By', 'Order by' or sub-selects. Just get the records for the last 20 days and run the aggregate function on them.
Ruby:
Model.where('date >= ?', Date.today - 20.days).select('stddev_samp(percent_change) as stdev').first['stdev']
SQL:
select stddev_samp(percent_change) as stdev
from <table>
where date >= now() - interval 20 day;
If you want to use the LAST 20 RECORDS, not last 20 days:
Ruby:
Model.order('date desc').limit(20).select('stddev_samp(percent_change) as stdev').first['stdev']
SQL:
select stddev_samp(percent_change) as stdev
from <table>
order by date desc
limit 20;
you don't need the group by since you don't want one value for each date.
also your limit might not work if you have multiple values for a date or have a date missing
try this:
SELECT stddev_samp(percent_change) as stdev
FROM
(SELECT percent_change
FROM tbl
WHERE date > now() - interval '20 days') AS q

Resources