Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to fetch latest date from records in oracle database by using Java.
Example:
String query = "Seclect LATEST_INSERT_DATE from test";
stmt.executeUpdate(query);
Also need latest date with latest time.
like:
ID DATE MONTH YEAR
34 04-DEC-13 Jan 1980
35 04-DEC-13 Feb 1980
Now, i have to fetch latest one .
Please give me the oracle Query.
Please help me. any help would be appreciable.
Just select the max 'INSERT_DATE_FIELD':
String query = "Select MAX(INSERT_DATE_FIELD) from test";
String query = "select * from (Select * from test order by INSERT_DATE_FIELD desc) where rownum = 1
or Select * from test order by INSERT_DATE_FIELD desc and in java part use if(rs.next())
and take only the first row.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Example :
Select * from student where roll_no in ( 1,2,3 );
In student repository (Spring Boot):
#Query(value="Select * from student where roll_no in (?)",native =true)
List selectStudents(What do I give here ?)
Or is there any other way to implement this?
If you want to use native query you can do it like below
#Query(value="select * from student where roll_no in (:rollNos)",native =true)
List<Object[]> selectStudents(#Param("rollNos") List<Integer> rollNos);
But I would recommend you to do it using JPA named query like below which is very easy to handle further as it gives you the result in entity format.
Student findByRollNo(List<Integer> rollNos);
You can just pass in a List, presumably if Integer or Long.
See: Spring CrudRepository findByInventoryIds(List<Long> inventoryIdList) - equivalent to IN clause
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I m using the database where i need to check that if that particular column of the table has same values for all the rows, as want to see yes or no... If yes then i'll go for one method other wise for other method.
It seems to me that Java is a red herring here. The SQL that you want is
select count( distinct columnName ) from tableName;
which tells you how many different values there are of columnName.
Assuming that you have a SQL connection provisioned from within your Java code already, it should be as easy as comparing a resultset against 0:
Connection con DriverManager.getConnection("jdbc:driver:host","myLogin","myPassword");
Statement statement = con.createStatement();
ResultSet result = statement.executeQuery("SELECT COUNT(DISTINCT columnName) AS columnDistinctCount FROM tableName;");
int uniqueEntryCount = result.getInt("columnDistinctCount");
if (uniqueEntryCount > 0) {
methodOne();
else {
methodTwo();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Sample Table
I want to select 6 random records from this table based off 2 columns, the topic and the taxonomy.The selection of records must be balanced and repetition of topics and taxonomy should be kept a minimum.Is there an algorithm to do this?
Any help in either SQL or java would be appreciated.Thanks in advance
You can do this with an nth sample on an ordered set. It is something like:
select t.*
from (select t.*, (#rn := #rn + 1) as rn
from t cross join
(select #rn := 0) params
order by topics, taxonomy
) t cross join
(select count(*) as cnt from t) tt
where rn % floor(cnt / 6) = 1;
The idea is to use modulo arithmetic to take every nth value to get to 6. You may have to fiddle with the exact parameters in the where, depending on the size of your data.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how to write below SQL:
select * from MYTABLE where date(MYDATE) between '2015-12-30'
and '2015-12-30';
in JOOQ where MYDATE is type of java.sql.timestamp.
Here's a 1:1 translation:
DSL.using(configuration)
.select()
.from(MYTABLE)
.where(DSL.date(MYTABLE.MYDATE).between(Date.valueOf("2015-12-30"))
.and(Date.valueOf("2015-12-30")))
.fetch();
Some criticism:
Your two dates are the same, so your query might not really make sense
You shouldn't cast/convert the MYDATE column, as this will prevent using indexes. Instead, you should work out the correct timestamp range for the two dates.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do not know what kind of data it returns this query and how to retrieve/use them
Query q1 = em.createNativeQuery("SELECT t.idb, c.value FROM trans t join com c On t.id = c.idTrans where c.date >= '2014-05-01' AND c.date <= '2014-05-31' AND currency = 'euro' ORDER BY t.idb");
return q1.getResultList();
It depends on if you use getResultList or getSingleResult. If you expect multiple rows to match the query, you would use getResultList and have a List containing an Object[] representing each row, with values matching the type returned from the driver for t.idb and c.value.
If you use getSingleResult, you would get an Object[] representing the only row that should have been returned, or a NonUniqueResultException if more than one row was returned from the query.
If I understand your question, I think you want to call getResultList() -
final List<Object> data = (List<Object>) query.getResultList();