Query to get data for past X months in JPQL - java

I have constructed following JPQL query where result is being sent based on the timestamp being passed in.
I am looking to modify this and pass in a number instead which is the month value and get all results from past 6 months for example.
I am using Oracle DB if that is relevant. I tried passing in datediff method in this query but it is throwing compilation errors.
Is there a way to do this?
My current query.
#Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType " +
"AND urlCount.updateTimeStamp <=:timeStamp")
Integer getTotalCount(#Param("urls") List<String> urls, #Param("eventType")String eventType, #Param("timeStamp")Timestamp timeStamp);
Looking to modify the line -> "AND urlCount.updateTimeStamp <=:timeStamp")
#Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType ")
// "AND urlCount.updateTimeStamp <=:timeStamp") // instead of this, pass in the numOfMths value to calculate how much data to show (example past 3 mths)
Integer getTotalCount(#Param("urls") List<String> urls, #Param("eventType")String eventType, #Param("numOfMths")int numOfMths);

With Oracle you can use ADD_MONTH with a negativ value to subtract.
ADD_MONTHS(SYSDATE, -6)
You can call this functions like this:
function('ADD_MONTH', CURRENT_DATE, -6)
So your query could look like this:
#Query("SELECT SUM(urlCount.referEventTotalCount) " +
"FROM UrlCountEntity urlCount " +
"JOIN urlCount.eventTypes et " +
"WHERE urlCount.url.urlTx IN :urls " +
"AND et.referEventTypeDs =:eventType " +
"AND urlCount.updateTimeStamp >= function('ADD_MONTH', CURRENT_DATE, :month)")

Related

Users wanted a Calendar change from Saturday to Wednesday csv file export

I started off by replicating the issue that the users were having, used that in SQL developer to determine the query that would need to be changed. Then I found a query that would resolve the issue. Now I'm getting a syntax error, after I have applied the changed logic to the old query.
Oracle Data base, and a JPQL
Original Query
#Query("SELECT DISTINCT new UiIdDescriptionDto( "
+ " c.weekId, "
+ " c.weekDate "
+ ") "
+ "FROM CalendarHierarchy c "
+ "WHERE c.weekId BETWEEN :weekId1 AND :weekId2 "
+ "ORDER BY c.weekId ASC "
New Query that retrieves proper data users wanted
#Query("SELECT DISTINCT new UiIdDescriptionDto( "
+ " c.weekId, "
+ " to_char(to_date(c.weekDate,'MM/DD/YYYY')-3,'MM/DD/YYYY') "
+ ") "
+ "FROM CalendarHierarchy c "
+ "WHERE c.weekId BETWEEN :weekId1 AND :weekId2 "
+ "ORDER BY c.weekId ASC "
[UPDATE]
So now, I don't get any errors at compile time and it query's properly to csv... however, in the ide it complains that I have 2 errors, both underlining in red the to_char open and close parenthesis. Is this something I should worry about? or leave as is?

Bind #param in a Jpa repository native query inside single quotations

I am looking for a way to bind a given param in a native query where the value has to be inside single quotations, like so:
#Transactional(readOnly = true)
#Query(value = " SELECT c.ID " +
" FROM table1 clh " +
" LEFT JOIN table2 nks " +
" on clh.SERIAL = nks.SERIAL_REF " +
" WHERE clh.CREATED_DATE >= :now - interval ':timeThreshold' HOUR " +
" AND nks.SERIAL_REF IS NULL" , nativeQuery = true)
List<Long> getIdsWithoutAnswer (#Param("timeThreshold") Integer timeThreshold, #Param("now") LocalDateTime now);
However, when I try to run this, it results in hibernate not being able to bind the timeThreshold value as it is provided inside the single quotations ''.
Does anyone know how this can be resolved?
The problem you are having with your native Oracle query has to do with trying to bind a value to an interval literal. You can't do that. Instead, use the NUMTODSINTERVAL() function:
#Transactional(readOnly = true)
#Query(value = " SELECT c.ID " +
" FROM table1 clh " +
" LEFT JOIN table2 nks " +
" on clh.SERIAL = nks.SERIAL_REF " +
" WHERE clh.CREATED_DATE >= :now - numtodsinterval(:timeThreshold, 'hour') " +
" AND nks.SERIAL_REF IS NULL" , nativeQuery = true)
List<Long> getIdsWithoutAnswer (#Param("timeThreshold") Integer timeThreshold, #Param("now") LocalDateTime now);

Jpa native query doesnt work with group by case/when statement with binding values

Iam trying to use Jpa repository to get data from my sql server using a native query
This is a simple call from my service
repo.testData("h3","h3");
This is my repository query.
Select statement can read the binding variable :level but group by is unable to read it.
#Query(value="SELECT sum(pos.total_weekly_sales) as curr_yr_sales, sum(pos.total_weekly_qty) as curr_yr_qty, pos.vendor_nbr,pos.gmm_id,\n" +
"case \n" +
"when :level = 'h3' then pos.category_id\n" +
"else 0\n" +
"end category_id\n" +
"from dbo.agg_sams_data pos\n" +
"join dbo.calendar_dim cal on pos.wm_year_wk_nbr = cal.wm_year_wk_nbr\n" +
"WHERE \n" +
"cal.calendar_date BETWEEN '2019-09-11' and '2020-09-09'\n" +
"and pos.vendor_nbr = 68494\n" +
"and pos.gmm_id = 45\n" +
"and (:h3Flag = 'N' or pos.category_id = 52)\n" +
"GROUP by pos.vendor_nbr,pos.gmm_id,\n" +
"case \n" +
"when :level='h3' then pos.category_id\n" +
"else 0\n" +
"end",nativeQuery = true)
List<List<Double>> testData(String level,String h3Flag);
And i get the following error
com.microsoft.sqlserver.jdbc.SQLServerException: Column 'dbo.agg_sams_data.category_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If i pass the hardcoded value in the group by clause it works fine(as below)
"GROUP by pos.vendor_nbr,pos.gmm_id,\n" +
"case \n" +
"when 'h3'='h3' then pos.category_id\n"
You should try putting pos.category_id into the group by instead of the whole case when statement. The problem is, that SQL Server can't be sure that the parameter in both cases will have the same value so the expressions could be different.

Problems with aggregate SQL functions in Spring Boot application

I'm working on a website in a Spring Bootwhich is connected to a MySQL db. In the db i have two tables: Player and Match and i created a query that should return me a list of players with count of matches they already played. The problem is that typed aggregate function count(M) doesn't and I don't know that I'm doing wrong. In db I have e.g. Player with id = 1 and two played Matches, another with one Match, and another with 0. What I get as a result is one Player with 3 played Matches. If i type M.id instead of count(M), I get two rows for Player 1 (one for Match id), and onw row for the second. What is wrong with my code?
#Query( "select new dto.PlayerDTO(" +
" P.id, " +
" P.nickname, " +
" count(M), " +
"from " +
" Player P left join Match M on P = M.player " +
"where " +
" P.games like %?1% ")
List<PlayerDTO> findPlayersForGame(String game);
When you count() on the joined table, you have to use group by statement:
#Query( "select new dto.PlayerDTO(" +
" P.id, " +
" P.nickname, " +
" count(M), " +
"from " +
" Player P left join Match M on P = M.player " +
"where " +
" P.games like %?1% " +
"group by P.id ")
List<PlayerDTO> findPlayersForGame(String game);

Sqlite Android Optimization

I have an application that works well. But I suspect that I can improve it if I optimize queries to the database. And I need suggestions.
This is part of my "select query":
private static final String SELECT = "SELECT " +
"dz.first_id AS first_id, " +
"dz._id AS _id, " +
"dz.att1 AS att1, " +
"dz.att2 AS att2, " +
"dz.att3 AS att3, " +
"dz.att4 AS att4, " +
"dz.att5 AS att5, " +
"d.var1 AS var1, " +
"d.name AS name, " +
"d.last_update AS last_update, " +
"d.image_url AS image_url, " +
"d.image_highlighted_url AS image_highlighted_url, " +
"d.var2 AS var2, " +
"d.type AS type, " +
"d.state AS state, " +
"d.sync AS sync, " +
"d.var3 AS var3 " +
"FROM table1 dz INNER JOIN table2 d " +
"ON d._id = dz.first_id ";
Cursor result = conn.rawQuery(SELECT, null);
*table1 and table2 have simple creation: only one _id integer PRIMARY KEY AUTOINCREMENT NOT NULL
It is useful to use views? Any other suggestion?
Thanks.
This query looks as cut and dry and they can get, I think your options are really either to see if you can somehow leave some unnecessary columns out of your select or alternatively to see that both dz.first_id and d._id have indexes setup. Perhaps add a index to dz with the following
CREATE INDEX index1 ON table1 (first_id);

Categories

Resources