Click here to Skip to main content
15,888,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,
I have a table with records like below :

SQL
ID    SKU     Price
1     ABC1    200
2     ABC1    500
3     ABC1    0
4     XYZ     550
5     XYZ     100
6     LMN     600
7     LMN     300
8     LMN     10


I want to get the LAST (top 1 order by desc) record of the SKU.
So my result set would look something like this:

SQL
ID    SKU     Price
3     ABC1    0
5     XYZ     100
8     LMN     10


Can you help me in writing a query for this?

Any help appreciated.
Thanks,
Lok..
Posted

It should works:
SQL
SELECT t1.ID, t1.SKU, t1.Price
FROM YourTable AS t1 RIGHT JOIN (
	SELECT 	SKU, MAX(ID) AS ID
	FROM YourTable
	GROUP BY SKU
	) AS t2 ON t1.ID = t2.ID
ORDER BY ID
 
Share this answer
 
v2
Comments
Lokesh Zende 28-May-13 2:29am    
Worked..
Thanks Maciej Los :)
Maciej Los 28-May-13 2:50am    
You're welcome ;)
gvprabu 28-May-13 3:33am    
Good Job My frnd :-)
Maciej Los 28-May-13 4:19am    
Thank you, My friend ;)
Hi,
I tried this query and it also worked.

SQL
Select * from MyTable where ID in
(select MAX(ID) from MyTable group by SKU)


What will be the performance effect?
Please correct me if you think I am doing something wrong.
 
Share this answer
 
Comments
Maciej Los 28-May-13 3:03am    
Your query is absolutely correct.
In my opinion, using JOINs is much, much faster than using IN cluase.
Have a look here: http://www.bennadel.com/blog/940-SQL-Optimization-Case-Study-JOIN-Clause-vs-IN-Clause.htm[^]
Lokesh Zende 28-May-13 4:44am    
Thanks a TON...
It helped me in my other Time Out issue as well ..
:)
SQL
select Id,SKU,Price from
(
    select 
    Row_Number() over(partition by sku order by sku,price ) as sr,Id, SKU, Price
    from tblName
) as temp 
where sr = 1

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Maciej Los 28-May-13 2:57am    
Good!
I'm sure that price inside ORDER BY for ROW_NUMBER instruction is not necessary...
Correct me if i'm wrong and i'll re-vote ;)
Aarti Meswania 28-May-13 3:06am    
When I read question I thought OP wants record that have lower price for each group

so, that I have not write query based on ID field but by considering Price field and for that I have do order by to get minimum price at top

thanks for reading,
have a good day! :)
Maciej Los 28-May-13 3:28am    
But OP wants to fetch data with higher ID ;)
In my opinion ROW_NUMBER() OVER(PARTITION BY SKU ORDER BY ID DESC) will do the job ;) What you think about it?
So, i suggest you to change your solution to avoid down-voting...
Aarti Meswania 28-May-13 3:40am    
nope, no one has downvote yet :)
I think you want do, don't you?, kidding :D
Actully in question it's not mention that he want the hieghest ID or Lowest Price for each group, right?
so, how you decide some one will downvote?
and it's a method by using OP can get answer,
and OP can also do that small chage if he want o/p concern by ID
Maciej Los 28-May-13 4:21am    
OK, decision belongs to you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900