Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
EMPLOYEE(id,emp_name)
1, John
2, Michael
3, Philip
===================
RANK(id,emp_id,date,rank)
1,1,1583413078445,SE
2,1,1583413087085,SSE
3,1,1583413092169,PM
4,2,1583413131802,SE
5,3,1583413536655,SE
I want to show
employee id, employee name and latest Rank (whose id is maximum for emp_id in rank table or whose longdate is maximum for emp_id in rank table) from Rank table
id,name,Rank
1, John, PM
2, Michael, SE
3, Philip, SE
I am using java and my database is mysql and sqlite (in case offline)
I tried some way but could not aggregate the queries.
SELECT RK.emp_id, EP.emp_name, RK.rank
FROM RANK RK
LEFT JOIN employee EP ON RK.emp_id=EP.id
GROUP BY RK.emp_id
HAVING MAX(RK.id);
What you need is some techniques on analytic functions.
with RANK_EXPAND as (
select
id,emp_id,date,Rank,
max(date) over (partition by emp_id) as maxDate,
max(id) over (partition by emp_id, date) as maxId,
from RANK
),
select
r.id,
e.emp_name as name,
r.Rank
from
EMPLOYEE e inner join RANK_EXPAND r on e.id=r.emp_id
where
r.date=r.maxDate and r.id=r.maxId
You may also use purely group by + rejoining, but that will make the query much longer.
I agree with the solution of Aditya as the best solution (I would comment the answer, but I don't have necessary reputation).
I just want to point out another possible solution for this using a sub-query in the projection-clause.
SELECT EP.emp_id, EP.emp_name,
(SELECT RK.Rank FROM Rank RK WHERE RK.emp_id=EP.emp_id ORDER BY RK.id DESC LIMIT 1) as Rank
FROM employee EP
;
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
This question already has answers here:
Select random row from a sqlite table
(7 answers)
Closed 3 years ago.
I want to randomize some values using ID in sqlite query.
* am using quiz app and want to randomize our question. In database there is a column for category where category id is used to select proper cat and I want randomize only question with limit *
String query = "select * from quiz where id =" +id ;
SELECT *
Says to select all non-hidden columns from the table. The correct syntax for retrieving specific columns is to code the specific column names (or if there are potential ambiguities tablename.columnname e.g. quiz.question).
So you would want
SELECT question FROM quiz .....
To select a random question, assuming that question is the required column name, then you could use :-
String query = "SELECT question FROM quiz ORDER BY random();";
If you wanted to limit the number of rows returned you could use
String query = "SELECT question FROM quiz ORDER BY random() LIMIT 10;";
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing something for my friend that will involve storing some data in a database with a primary key which is an integer which will increment. For example:
id name Age
1 James 15
2 Max 24
3 Jordan 61
How would I retrieve the integer under the id column in the last row? And this would be with Java. Thanks.
If the "last" row is the one with the highest ID, your SQL (to retrieve just the ID, like you asked) will look like:
SELECT id FROM User ORDER BY id DESC LIMIT 1;
In Java, you'll probably be using JDBC; best practice is to learn how to use PreparedStatements.
is it like
id name age
1 james 15
2 max 24
3 jordan 61
PRIMARY KEY(id)
SELECT id
FROM `tablename`
ORDER BY id DESC
LIMIT 0 , 1
this will return the last row id result set from ur table