JPQL query to get last 10 days of data - spring-boot

I have a simple sql query
SELECT * FROM survey t WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -10 DAY);
or
SELECT * FROM survey t WHERE t.date >= ( CURDATE() - INTERVAL 10 DAY )
What is the equaivalent of getting the data using JPQL query for the where condition

Incase if it is useful to anyone
BETWEEN is the word
Call like this in service
repository.getData(new java.util.Date(System.currentTimeMillis() - 10*24*60*60*1000L), new java.util.Date());
in the repositary
#Query(SELECT * FROM survey t WHERE t.date BETWEEN :startDate
AND :endDate)
List<Object[]> data(#Param("startDate") Date startDate,#Param("endDate") Date endDate);

Related

TIMESTAMP in NamedNativeQuery using Spring Data JPA

The scenario is to fetch values from OracleDB where one of the search input is DateTime in the format "07/11/2017 10:12:16 AM" (which is a string) and the DB Column is TIMESTAMP.
The Data in the column is saved in the format "07/11/2017 10:12:16 AM" but it is a TIMESTAMP.
while querying from the Oracle DB, it is possible to convert the search input to the appropriate TIMESTAMP using TO_TIMESTAMP function
Example
select * from Table where SI_ID='12345'and COLUMN >= TO_TIMESTAMP ('07/11/2017 10:12:16 AM', 'mm/DD/YYYY HH:MI:SS AM';
I need to achieve the same in Java using Spring Data JPA and NamedQuery.
The NativeNamedQuery is called from the JPA repository is as follows
#Query(name = "Sample.findRecordByIdAndTime", nativeQuery = true)
Sample findByIdAndTime (#Param("id") Long id, #Param("timestamp") String timestamp);
But how to convert the string to TIMESTAMP in the Named query called from JPA repository which is given below:
#NamedNativeQuery(name="Sample.findRecordByIdAndTime", query="select * from TABLE where SI_ID= ? and TS_COLUMN >= TO_TIMESTAMP(?, 'mm/DD/YYYY HH:MI:SS AM')", resultClass = Sample.class)
Any help will be appreciated.
your query should work (as it native query), just add bind params : :id and :timestamp
#NamedNativeQuery(name="Sample.findRecordByIdAndTime",
query="select * from TABLE where SI_ID= :id
and TS_COLUMN >= TO_TIMESTAMP(:timestamp , 'mm/DD/YYYY HH:MI:SS AM')",
resultClass = Sample.class)

Laravel eloquent possible bug?

I want to join two tables and filter on a field in the joined table. I don't think the actual tables matter in this question, but it's a table with dates joined with a table with the event info, so there are more dates possible for 1 event.
I made this eloquent line:
Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true );})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc')->toSql());
the filter doesn't work though. So thats why i added the ->toSql() to the line.
I get the following back:
select * from `event_dates` where startdate >= curdate() OR enddate >= curdate() and exists (select * from `events` where `event_dates`.`event_id` = `events`.`id` and `approved` = ?) order by `startdate` asc, `enddate` asc
You see that the 'where("approved",true )' results in 'where ..... and and approved = ?)' Where does the questionmark come from??? I tried diferent things, like '1', 1, 'True', True, true, 'true'...everything comes back as a questionmark.
Any suggestions??
Thanks!
Erwin
This is expected behaviour. Laravel uses prepared statements. To get parameters that are put into placeholders, you can use
$query->getBindings();
so for example in your case you can use:
$query = Event_date::whereRaw('startdate >= curdate() OR enddate >= curdate()')->whereHas('Event', function($q){$q->where("approved",true );})->orderBy('startdate', 'asc')->orderBy('enddate', 'asc'));
and now
echo $query->toSql();
var_dump($query->getBindings());
to get both query with placeholders and values that will be put in place of placeholders.

Oracle - Check if there is a data in range of date

I have three tables (receipts, receiptaddinfo, shops). I have select, that gives me a data with a all receipts from all shops since the declared date:
select *
from receipts r
join receipt receiptaddinfo ri on r.receiptid=ri.receiptid and r.shop=ri.shop
join shops s on ri.shop=s.shop and shoptype=0
where ri.creationtime >= '2016-05-19 00:00:00'
order by ri.creationtime desc
The table shops, contain all shops, however, I want to check if there is a shop, which had no 'sale/receipts' since the declared date. Could somebody help?
You can try the following SQL statement.
SELECT * from shops s
WHERE s.shoptype = 0
AND NOT EXISTS
(SELECT 1
FROM receipts r,
receiptaddinfo ri
WHERE r.receiptid = ri.receiptid
AND r.shop = ri.shop
AND ri.shop = s.shop
AND ri.creationtime >= '2016-05-19 00:00:00')

Query to get list of Inventory Items for which there is no material transaction in Oracle

Have a small doubt I want to compile a SQL query in Inventory where I have to get those Items for which transactions have not been recorded during a period of at least a specified number of days.
The days could be 30 days or 2 months depends. So I want to get those items for which no transaction was recorded for lets say 30 days. Could anyone give me an idea of how to go about this thing?? I am using r12. I came up with the following query but it is giving many records. The commented portions of this query remains commented only
select distinct msi.segment1, msi.description, msi.primary_uom_code,
msi.inventory_item_id
from mtl_system_items_b msi /*,
mtl_material_transactions mmt*/
where /*msi.inventory_item_id = mmt.inventory_item_id
AND msi.organization_id = mmt.organization_id
AND NVL((SELECT SUM(transaction_quantity)
FROM mtl_onhand_quantities
WHERE inventory_item_id = msi.inventory_item_id),
0) = 0
AND TRUNC(mmt.transaction_date) <= SYSDATE - &D
AND*/
not exists
(select *
from mtl_material_transactions mmt
where msi.inventory_item_id = mmt.inventory_item_id
and msi.organization_id = mmt.organization_id
and trunc(mmt.transaction_date) < sysdate - &D)
Here is a variation of your query that will give all items with on hand inventory that have not been transacted in a determined number of days.
select distinct msi.segment1
,msi.description
,msi.primary_uom_code
--,msi.inventory_item_id
,q.organization_id
,q.quantity
from mtl_system_items_b msi
join (SELECT inventory_item_id, organization_id, SUM(transaction_quantity) quantity
FROM mtl_onhand_quantities
GROUP BY inventory_item_id, organization_id) q on msi.inventory_item_id = q.inventory_item_id and msi.organization_id=q.organization_id
where not exists (select *
from mtl_material_transactions mmt
where msi.inventory_item_id = mmt.inventory_item_id
and msi.organization_id = mmt.organization_id
and trunc(mmt.transaction_date) > sysdate - 180)
order by q.organization_id,msi.segment1;

Delete using active record

How to achieve below query in ActiveRecord ?
"delete from model where date(created_at)=#{some_date}"
where created_at is sql datetime field.
One option I can think is calculate
start_date = starting time of the day
end_date = end time of the day
and
Model.delete_all('created_at' >= start_date, 'created_at' < end_date)
Any other clean option ?
Assuming database you are using is MySql and start_date is a date object.
Use mysql DATE_FORMAT function
Model.delete_all("DATE_FORMAT(created_at, '%Y-%m-%d') >= ?",
start_date.strftime("%Y-%m-%d"))

Resources