Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I Have an Sql table with three columns.

Id    ItemId	Item
1	1	Dog
2	1	Dog1
3	2	Cat
4	2	Cat1
5	3	Lion
6	3	Lion1

Now I have to find distinct of this table. with respect to Item Id. Please help me with it to write stored Procedure. Output should be Like this.

Id    ItemId	Item
2	1	Dog1
4	2	Cat1
6	3	Lion1
Posted
Updated 16-Apr-15 9:11am
v2

1 solution

Try this:
SQL
SELECT t1.ID, t1.ITEMID, t2.ITEM
FROM (
  SELECT MAX(ID) ID, ITEMID
  FROM A
  GROUP BY ITEMID
) AS t1 INNER JOIN A AS t2 ON t1.ID = t2.ID


SqlFiddle[^]
 
Share this answer
 

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