PostgreSQL: UNIX Time Dynamic query - postgresql

I'm making a Power BI report where data that I need to show is stored in a PostgreSQL database.
In the table where I query there is data from 4 years ago until today, but for my report I only need the last week of records (I know that I can filter using Power BI but my goal is make the lighter query as possible).
The fields of the database related to time, are in UNIX Timestamp, so I'm filtering it now by this way:
SELECT
DATABASE.INCIDENT_NUMBER
,DATABASE.SUBMITTER
,DATABASE.CREATE_DATE
,DATABASE.MODIFIED_DATE
,DATABASE.CLOSED_DATE
,DATABASE.SUBJECT
FROM
DATABASE
WHERE
1643670000 < DATABASE.CREATE_DATE
ORDER BY DATABASE.INCIDENT_NUMBER, DATABASE.CREATE_DATE ASC
That is fine, but I want to improve it making a dynamic query which returns the records from last week till today, without putting a constant of UNIX timestamp.
How can I make that?

That's an excellent example why it is a bad idea to store timestamps as numbers. The correct data type is timestamp or timestamp with time zone.
If you had used the correct data type, your condition would be as simple as
WHERE current_date - 7 < database.create_date
But with numbers, you have to convert back and forth:
WHERE to_timestamp(EXTRACT('epoch' FROM current_date) - 7) < database.create_date

Related

Get truncked data from a table - postgresSQL

I want to get truncked data over the last month. My time is in unix timestamps and I need to get data from last 30 days for each specific day.
The data is in the following form:
{
"id":"648637",
"exchange_name":"BYBIT",
"exchange_icon_url":"https://cryptowisdom.com.au/wp-content/uploads/2022/01/Bybit-colored-logo.png",
"trade_time":"1675262081986",
"price_in_quote_asset":23057.5,
"price_in_usd":1,
"trade_value":60180.075,
"base_asset_icon":"https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"qty":2.61,
"quoteqty":60180.075,
"is_buyer_maker":true,
"pair":"BTCUSDT",
"base_asset_trade":"BTC",
"quote_asset_trade":"USDT"
}
I need to truncate data based on trade_time
How do I write the query?
The secret sauce is the date_trunc function, which takes a timestamp with time zone and truncates it to a specific precision (hour, day, week, etc). You can then group based on this value.
In your case we need to convert these unix timestamps javascript style timestamps to timestamp with time zone first, which we can do with to_timestamp, but it's still a fairly simple query.
SELECT
date_trunc('day', to_timestamp(trade_time / 1000.0)),
COUNT(1)
FROM pings_raw
GROUP BY date_trunc('day', to_timestamp(trade_time / 1000.0))
Another approach would be to leave everything as numbers, which might be marginally faster, though I find it less readable
SELECT
(trade_time/(1000*60*60*24))::int * (1000*60*60*24),
COUNT(1)
FROM pings_raw
GROUP BY (trade_time/(1000*60*60*24))::int

sql (or postgres) how to set the date as single year and month?

I am compiling data of multiple years but I only need time not date and year. How can I set the year and date data into a single year and a single date so that I can use the time information as accumulative data?
2020-01-01 + time value.
Thanks!!!!
The information you provided is a little light. Still with certain assumptions:
create table ts_sum (ts_fld timestamptz);
insert into ts_sum values ('2020-04-14 08:15:32'), ('2021-09-27 18:45:01'), ('2022-01-09 20:21:05');
select sum(to_char(ts_fld, 'HH24:MI:SS')::interval) from ts_sum;
sum
----------
47:21:38
Needs to be tested with your data. The procedure is extract the time portion out of the timestamp using to_char then cast that to an interval and then sum the time intervals.

Add Filter to extract rows where the timestamp falls in between yesterday at 4 AM and today at 3 AM in Cognos

I am new to Cognos and I am trying to add a filter to a column that only allows rows that are in between Yesterday at 4 AM and today at 3 AM. I have a working query in db2 but when I try to add it to the filter in Cognos I get a parsing error. Also, I found in the properties that the data type for the column I am trying to filter to be Unknown (Unsupported) type. I started off by creating two Data Item Expressions for each time frame I am trying to limit the data by. But I got a parsing error on the first one:
[Presentation Layer].[Cr dtime]=timestamp(current date) - 1 day + 4 hour
This works in my db2 local test database but doesn't even compile in Cognos. I also tried casting the column into a timestamp but that isn't working either. Any help is appreciated. I also tried using the _add_days function but I still get a parsing error. Also sampling the column I get values that appear to be timestamps as this string: 2016-01-02T11:11:45.000000000
Eventually if I get the two filters working I expect the original filter to be close to this syntax:
[Presentation Layer].[Cr dtime] is between [Yesterday 4AM] AND [Today 3AM]
Here is your filter:
[Presentation Layer].[Cr dtime] between
cast(_add_hours(_add_days(current_date,-1),4),timestamp)
and
cast(_add_hours(current_date,3),timestamp)
This works because current_date in Cognos does not have a time component. If you were to cast it directly to a timestamp type you would see the time part of the date as 12:00:00.000 AM, or midnight. Knowing this we can then simply add how much time after midnight we want, cast as a timestamp type and use this in the filter.

How to get the data of last five days from amazon simple db in iphone application

I want to get the data from table last 5 days records I have seen examples they work fine for other data base but i am using amazon simple db so and sql razor I am using same query but it does not work and shows error
SELECT *
FROM UserContentUsage
WHERE ViewDateTime BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND CURDATE()
Amazon SimpleDB stores everything as UTF-8 strings. All comparisons are done lexicographically. The dates should be in ISO 8601 format so that they can be properly compared in lexicographical order.
See page for more detail.

Is there a simple way to fetch records from the last N days in MongoDB?

If my records have a created_date field, is there a simple way to fetch all records that were created within the 7 days without calculating and adding the start and end date manually?
Mongo doesn't have a symbolic date like SQL NOW() which you can use on the client and have evaluated on the server.
The best way to solve your problem is to evaluate calculate the dates on the client. In your specific case, since records aren't created in the future, likely you only need to calculate "now minus 7 days" in your client, and put that date in your query.
Sort of an aside, if you need the exact date from the server, you can fetch it like this:
db.eval( function() { return new Date() } );
Using most of the drivers you should be able to accomplish that using the $eval command.
Finally, you could accomplish this entirely on the server using a where() expression. However, I don't recommend this approach. Such queries can't take advantage of indexes and therefore must deserialize every record in the collection. They're very slow unless your collection is tiny. If you want to take this approach, though, you can use new Date() in the JavaScript for your where() expression.
Try something like this in the mongo console to return records created in the last 7 days (assuming created_date is the insertion date):
> startDate = new Date() // Current date
> startDate.setDate(startDate.getDate()-7) // Subtract 7 days
> startDate.setHours(0) // Set the hour, minute and second components to 0
> startDate.setMinutes(0)
> startDate.setSeconds(0)
> db.mycollection.find({created_date:{$gte: startDate}})
Quite a few lines of code, but at least you won't have to do date arithmetics.