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
;
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 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 7 years ago.
Improve this question
What is join in HQL and where shall it be used?
Have gone through tutorial.Majorly three of them used as follows:
Inner Join
Left Outer Join
Right Outer Join
Read few tutorials yet unable to understand.
Join in HQL is same like the SQL joins. In SQL join is used to join two tables to return matching data. In HQL join is between Parent entity and the child entity (Usually on a ForeignKey of a parent primary key).
Person table
person_id | name | dob
Address table
address_id | address_line1 | state | zip | person_id
A SQL Join looks like
Select * from person p join address a on p.person_id = a.person_id and p.person_id = 10
This will return data like following if a person has more than one address
person_id | name | dob | address_id | address_line1 | state | zip
----------------------------------------------------------------------
10 Jack 10/25 22 223 elk blvd AZ 54444
10 Jack 10/25 244 223 NY blvd TX 54344
A HQL join looks like
from Person p join p.address
and it returns Person entity with a list of addresses in it.
Person{
List address = [ Address{ (this is Object) }, Address{(This is object)}]
}
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 8 years ago.
Improve this question
I am new to Spring MVC and hibernate, I have two Entity Category and Sub_Category, Sub_category contains Foreign Key(Category_id) how can i insert value in Sub_Category only with the value of Foreign Key.
I my case values are inserting in Sub_category and as well creating new row in Category table and getting the value of that newly creating row id and saving in Sub_Category table but instead i want the value of already existed id value of Category table and store it in sub_Category Foreign Key column.
Let's assume your object is category
after saving it as
category.save(); // after the database insertion
create another object of Sub_Category as
if(category.save()){ // I assume, saved returns true on successful insertion
Sub_Category subCategory = new Sub_Category();
subCategory.setCategory(category); // category that you saved above
subCategory.save();
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