Selecting the records with maximum date value - java

String sql1="SELECT MAX(date),epf_rte_emp,epf_rte_com,etf_rte FROM Fixed_Rates1";
I'm using this query to select the record with the maximum date value, but it gives the latest updated record. I want the record with the maximum date.This query gives the record with maximum date in the SQLiteManager, but doesn't gives the required output in netbeans. Could someone please help me in this?

If you want one record, then something like this shoudl work:
select fr.*
from fixed_rates1 fr
order by date desc
limit 1;
Note: I am guessing you are using MySQL, because your query would fail in most other databases. The method for limiting results depends on the database.
If you want all rows with the maximum date:
select fr.*
from fixed_rates1 fr
where fr.date = (select max(fr2.date) from fixed_rates1 fr2);

Related

SQL/JPA want to select data for a list of ids filtering by a specific timestamp range for each id

I have a table with some data, there are record id, userIds, timestamp and data columns. Receive from the client a list of userIds, initially, I just had to fetch data by the same timestamp range for all useIds, just using userId IN (list). However, now I'm required to get data by different timestamp ranges for each userId, let's say userID 1 needs data from 1643580000000 to 1646431200000 and userId 2 from 1626418800000 to 1647500400000 (utcTimestamp in mills).
Most probably I'll receive a list of [userId, startTime, endTime], so, I was considering to loop the main query for each userId with its respective timestamp range (I've seen it's a bad idea due to performance, but if I have to, I have to), but I also found out about cursors (not much experience here).
I wanted to know if it's possible to get what I want without loops or cursors, and if not, best way with each one.
Thanks in advance!
Notes: using MariaDB. SQL query will be used as nativeQuery in a Java service repository.
You can run a query laike this.
Of course you have to add for every user his own time range
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
OR (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
If you have a lot of rows and not that many id to process, you can do it with
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
UNION
SELECT * FROM mytable
WHERE (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
Here also you need to add for every id another UNION

I want to fetch 30 days back results from sqlite DB

I am trying to fetch 30 days back results from today in order to see who have not paid last month.
I tried something
SELECT * FROM MemberInformation WHERE DateOfAd = 'DATEADD(Day,-30,getdate())'
and query successfully executed but did not fetch the values I wanted.
My database:
please guys help me out...
DATEADD is not work in sqlite you but you can use its sqlLite equivalent DateTime like code below :
select * from TableName where DateColumn < DateTime('Now', 'LocalTime', '-10 Day');
http://www.sqlite.org/lang_datefunc.html
SQLite equivalent of SQL Server DateAdd function
Try
SELECT * FROM MemberInformation WHERE DateOfAd BETWEEN datetime('now', '-30 days') AND datetime('now', 'localtime');
or Try to change where condition as,
WHERE DateOfAd BETWEEN datetime('now', 'localtime', '-30 days') AND datetime('now', 'localtime');

hql query to get last 1 month records

I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.
Here is Mysql query:
SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM
F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
Frankly, I don't have any idea about how to write above query in HQl.
Can anyone please help me.
Could you try this, I couldn't test, if you it has a error please inform me;
String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()";
query = session.createSQLQuery(hqlQuery);

show all records from mysql database table when date is not selected in java swing form

I am using java swing and mysql database for my desktop based application.
And i am having following query while giving date filter on reports.
If I have not enter To_date to From_date in JTextField then it must show all records from my mysql database table and if i have entered date then it must show records in between that two dates.
So i have tried (date between To_date and From_date) But its showing records only when i give dates to query otherwise empty.
Would you please suggest me something?
You should try some static value for date column if its null.
I hope below query will help you to resolve your issue.
SELECT * FROM YourTable WHERE IFNULL(#StartDateParameter, IFNULL(DateColumn,'1900-01-01')) <= IFNULL(DateColumn,'1900-01-01')
AND IFNULL(#EndDateParameter, IFNULL(DateColumn,'1900-01-01')) >= IFNULL(DateColumn,'1900-01-01')
Please let me know if you cant solve your issue with this query.
You can use IFNULL ( http://www.mysqltutorial.org/mysql-ifnull/ ) and form your query such that if your parameter is null then you should use your column value for comparison.
e.g.
SELECT * FROM YourTable WHERE IFNULL(DateColumn,#YourParameter) = #YourParameter

Select record based on date with MS Access

Initially I have a specific column to store age in my Table and I use the following to perform the select query
select count(*) from DonorDetails where age < 25
Now I have removed the age column and used dob column (with Date/Time datatype) instead. How should I write the count query which gives the same result as before ?
In the Immediate window, you can use DateSerial with a mix of other date functions to give you the date 25 years ago from today.
? DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))
2/22/1989
So in your query maybe you want something like ...
WHERE [DOB] > DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))
Here is a sample of how your SQL should look:
SELECT count(*)
FROM DonorDetails
WHERE (((DateDiff("yyyy",[dob],Date()))>25));

Categories

Resources