#Query(value = "select * from Capping_Audit_Capture;\r\n"
+"where datepart(year, captured_time) = ?2;\r\n"
+"and datepart(month, captured_time) = ?3;\r\n"
+"and datepart(day, captured_time) <= 1 and check_point_id=?4 and line_id=?5 ", nativeQuery = true)
What is wrong with these query? I want to fetch all record of current date, getting exception
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: JPA-style positional param was not an integral ordinal; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: JPA-style positional param was not an integral ordinal
It looks like you forget a space right after the positional parameters. Try to separate the positional parameters and the semicolon like the example below.
#Query(value = "select * from Capping_Audit_Capture;\r\n"
+"where datepart(year, captured_time) = ?2 ;\r\n"
+"and datepart(month, captured_time) = ?3 ;\r\n"
+"and datepart(day, captured_time) <= 1 and check_point_id=?4 and line_id=?5 ", nativeQuery = true)
Related
I want to get data as mentioned follwing question.
Query to retrieve count per hour and zero if none
So according to that, I execute the following query in Postgresql so it is working fine.
select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h;
But my problem is I want this to execute as a spring data JPA native query like.
#Query(
value = "select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h",
nativeQuery = true
)
List<Map<String, Integer>> getPaymentDistry();
If I execute it I am getting an error like
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
Can anyone help me here?
Thanks,
The obfuscation layer chokes on the :: because it uses : to introduce named parameter. Typically the solution is to use the standard compliant cast() operator instead.
select cast('00:00' as time) + g.h * interval '1 hour' ...
alternatively use the ANSI time syntax as suggested by Laurenz:
select time '00:00' + g.h * interval '1 hour' ...
Want join the below two queries and want to give the output as following:
SELECT count(*) FROM trip_schedule_close ts
WHERE EXTRACT(MONTH FROM ts.schedule_date) = EXTRACT(MONTH FROM current_date)
AND EXTRACT(YEAR FROM ts.schedule_date) = EXTRACT(YEAR FROM current_date) AND ts.route_id='152'
This query will give_output as:
count
14
Next Query:
SELECT sum(ts.product_qty)AS Product_Quantity FROM trip_delivery_sales_lines ts
WHERE EXTRACT(MONTH FROM ts.order_date) = EXTRACT(MONTH FROM current_date)
AND EXTRACT(YEAR FROM ts.order_date) = EXTRACT(YEAR FROM current_date) AND ts.route_id='152' AND ts.product_id='432' AND ts.type='sales'
This query will output result as:
product Quantity
446565
N0w i want join these two queries and want to give result as :
446565/ 14
First rewrite your first query as :
SELECT count(*) AS nb FROM t...
Then , assuming sql1 & sql2 are your 2 queries
SELECT Product_Quantity || '/ ' || nb AS new_name FROM ( sql1 ) r1,( sql2 ) r2;
with
p(curr_month, route_id, product_id, type) as (values(
date_trunc('month', current_date),
152,
432,
'sales'::text))
select
(SELECT count(*)
FROM trip_schedule_close ts
WHERE
date_trunk('month', ts.schedule_date) = p.curr_month AND
ts.route_id = p.route_id) || '/' ||
(SELECT sum(ts.product_qty) AS Product_Quantity
FROM trip_delivery_sales_lines ts
WHERE
date_trunk('month', ts.schedule_date) = p.curr_month AND
ts.route_id = p.route_id AND
ts.product_id = p.product_id AND
ts.type = p.type)
FROM p;
My query is :
final String sSelectQuery = "SELECT COUNT(*) FROM ADMIN.SESSION_TBL WHERE lastmodified > (NOW() - INTERVAL '"
+ userSessionExpireTime + " MINUTE') AND userid= ? ";
return getJdbcTemplate().queryForObject(sSelectQuery, new Object[]{userId}, Integer.class);
where in userSessionExpireTime = 10.
If i want to achieve the same thing without appending variable userSessionExpireTime, and putting "?" instead.
How can I achieve it ?
I tried :
SELECT COUNT(*) FROM ADMIN.SESSION_TBL WHERE lastmodified > (NOW() - INTERVAL '? MINUTE') AND userid= ?
return getJdbcTemplate().queryForObject(sSelectQuery, new Object[]{userSessionExpireTime,userId}, Integer.class);
but I am getting column out of range.
Can some one please help?
You can use make_interval()
SELECT COUNT(*)
FROM ADMIN.SESSION_TBL
WHERE lastmodified > NOW() - make_interval(mins => ?)
AND userid = ?
If Spring JDBC template chokes on the => operator use the variant without named parameters:
SELECT COUNT(*)
FROM ADMIN.SESSION_TBL
WHERE lastmodified > NOW() - make_interval(0,0,0,0,0,?)
AND userid = ?
i wan't query like this:
#Query(
nativeQuery = true,
value = "select vp.id , vp.identity, vp.vehicle_id, dt.i " +
"from vehicle_plate vp" +
" inner join (select max(vp2.id) as id, vp2.vehicle_id as vId from vehicle_plate vp2 group by vp2.vehicle_id) as vpg" +
" ON vp.id = vpg.id and vp.vehicle_id = vpg.vId" +
" cross join (values :dates ) dt(i)" +
"where vp.vehicle_id in (:vehicleIds)"
)
Page<Object[]> findAllJoinByDate(
#Param("dates") List<java.util.Date> dates,
#Param("vehicleIds") List<Integer> vehicleIds,
Pageable pageable
);
my problem is the part of query cross join (values :dates ) dt(i) and date parameter unknown by jpa.
should be same as
cross join (values ('2019-10-08') , ('2019-09-07') ) dt(i)
is there solution for this?
I have the following code:
select distinct m.property_id,
m.property_size,
count(f.request_id) WOs,
round(cast(m.[property_size] as float(10)) / cast((f.[request_id] * 1000) as float(5)),2) as PER_1k_SQFT
from T1 m, T2 f
where m.property_id = f.property_id
and datepart(year,request_date) = '2015'
and datepart(month, f.request_date) = '12'
group by m.property_id, m.property_size, round(cast(m.[property_size] as float(10)) / cast((f.[request_id] * 1000) as float(5)),2)
order by count(f.request_id) desc
My last column, PER_SQ_Ft is all zeros. Why isn't it populating the result of the calculation?