criteria query for adding interval to a column in spring boot - spring-data-jpa

I need to add an interval to a column value in using a criteria query.
My raw query is something like
select * from classes where classtime + INTERVAL '0.3 hour' <= CURRENT_TIME
I have tried with
builder.sum(root
.get(classtime), builder.literal("INTERVAL 0.5 HOUR"));
builder.function("INTERVAL", LocalDateTime.class, "0.5 HOUR");
Both are not working.

Related

How to get the latest value of a time duration (let's say for 1 minute) in Kusto table

I want to find the latest value of a column for particular time duration(1 minute in my case) from Kusto table.
I have timeseries data in PostgreSQL table and I am using last() function (https://docs.timescale.com/api/latest/hyperfunctions/last/)to find the latest value of scaled_value for 1 minute time bucket of PostgreSQL and I want to use same function in Kusto Table to get the latest value of scaled_value . What will be correct function to use in Kusto corresponding to last() function in Postgresql
Code I am using in PostgreSQL :
SELECT CAST(EXTRACT(EPOCH FROM time_bucket('1 minutes', timestamp) AT TIME ZONE 'UTC') * 1000 AS BIGINT) as timestamp_epoch,
vessel_telemetry.timeSeries,
last(vessel_telemetry.scaled_value, vessel_telemetry.timestamp) as scaled_value,
FROM shipping.vessel_telemetry
WHERE vessel_telemetry.ingested_timestamp >= '2022-07-20T10:10:58.71Z' AND vessel_telemetry.ingested_timestamp < '2022-07-20T10:15:33.703985Z'
GROUP BY time_bucket('1 minutes', vessel_telemetry.timestamp), vessel_telemetry.vessel_timeSeries
Corresponding code I am using in ADX
VesselTelemetry_DS
| where ingested_timestamp >= datetime(2022-07-20T10:10:58.71Z) and ingested_timestamp < datetime(2022-07-20T10:15:33.703985Z)
| summarize max_scaled_value = max(scaled_value) by bin(timestamp, 1m), timeSeries
| project timestamp_epoch =(datetime_diff('second', timestamp, datetime(1970-01-01)))*1000, timeSeries, max_scaled_value
The data that i am getting using PostgreSQL is not matching with the data I am getting from ADX Query. I think the functionality of last() function of Postgre is different from max() function of ADX. Is there any function in ADX that we can use to perform same as last() of PSQL
arg_max()
arg_max (ExprToMaximize, * | ExprToReturn [, ...])
Please note the order of the parameters, which is opposite to Timescale's last() -
First the expression to maximize, in your case timestamp and then the expression(s) to return, in your case scaled_value

How to query by date and time in postgresql

i have a table where my date/time is of form: 2020-03-10 22:54:08
This is a timestampped object. I tried the following query but didn't return any rows:
select ts from table1
where cast(ts as timestamp) = '2020-03-10 22:54:08'
returns nothing.
How do i query based on date and time in postgressql?
A timestamp has microsecond resolution, so you have to use same techiques as when testing floating point numbers: Round it or use only < and > for comparison.
To retrieve data from a database, you need to refer to SQL SELECT syntax. In your situation, the ts column is already a timestamp, so there is no need to use cast(). Bear in mind, however, that a timestamp type contains fractions of a second (i.e., 2020-03-10 22:54:03.xxx), so you would be better off using a comparison operator (>,<,>=,or <=)
You can retrieve all columns by using the * syntax:
select *
from my_table
where ts >= '2020-03-10 22:54:08';
tymestamp type by default contains also microseconds, so, now() which, for example, is 2020-03-11 01:56:27.593985 here obviously is not equal to 2020-03-11 01:56:27. If you do not want to have microseconds precision in your data then declare your field like ts timestamp(0) NOT NULL DEFAULT now() which means "0 decimal digits for microseconds":
select
current_timestamp::timestamp as ts,
current_timestamp::timestamp(2) as ts2,
current_timestamp::timestamp(0) as ts0;
ts | ts2 | ts0
---------------------------+------------------------+---------------------
2020-03-11 02:02:52.98298 | 2020-03-11 02:02:52.98 | 2020-03-11 02:02:53
Actually this worked.
select distinct(ts) from my_table where ts >= '2020-03-10 22:54:08' and ts <= '2020-03-10 22:54:09'
But this doesn't give me the whole rows
But then i tried this and this worked:
select ts from table1
where to_char(ts,'YYYY-MM-DD HH24:MI:SS') = '2020-03-10 22:54:08'

how to use query parameters with limit and interval in spring data jpa with Query annotation

I have a native query to be used in #Query annotation in spring. The problem is that I cannot pass parameters for non standard clauses like LIMIT AND INTERVAL.
1) Is it possible to do pass the parameters for LIMIT in the annotation.
2) What is the programmatic equivalent in JPA. Does it have any query interface which can be used ?
#Query(value = "SELECT * FROM scheduler sch where timestamp < (CURRENT_TIMESTAMP + interval '1 seconds') FOR UPDATE SKIP LOCKED LIMIT 5",nativeQuery = true)
Also, the limit and interval clause are not to be changed for every query but should be set when the jvm starts up.
Two Step
1. Use Pageable pageable as a parameter in your method signature
2. Create object of PageRequest and call repository method by passing the pageRequest Object.
PageRequest pageRequest=new PageRequest(0,5); // 0 is page (used to calculate offset) and 5 is size (limit).
Pass the pageRequest object to the method.
For INTERVAL, if you are using seconds, use like below.
#Query(value = "SELECT * FROM scheduler sch where timestamp < (CURRENT_TIMESTAMP + ((interval '1 seconds') * :seconds)) FOR UPDATE SKIP LOCKED",nativeQuery = true)
Pass :seconds as another parameter.
For the parameters to be constant, you can create a public static final variable.

How to write a date range condition

How would I write the date range condition correctly for the following query: list all instruments from table "asset", where the "maturity_dt" > 1 year:...
in English it sounds like :" AND asset.maturity_dt >= Today + 365.."
If you are using MySQL you will be needing a query like,
Select * from asset where DATEDIFF(maturity_dt, now())>365;

Mysql function to DBIx::Class translation

I want to convert this mysql/select query with function to DBIx::Class but I can't construct it the right way.
Mysql/query with function:
mysql> select * from sold_products
where date_sub(curdate(), interval 100 day) <= date;
Result query on DBIC_TRACE:
SELECT me.fk_product, me.fk_customer, me.amount, me.quantity, me.date, me.pk_sold
FROM sold_products me WHERE ( date > date_sub(curdate(), interval 100 day ):
P.S DBIx is already upgraded.
thanks,
lupin
In your DBIx query you want the hash key to be '>=', not '>'. date => {'>=', whatever_here }? Did you try that already?