I have a table named store_master in which store_name, manager,supervisor are the fields.
User have entered values as follows
| store_name | manager | supervisor |
| mumbai | a | z |
| mumbai | b | z |
| mumbai | c | y |
| thane | d | x |
| thane | e | x |
| navi mumbai | f | w |
Now I want distinct store_name followed by there last manager and supervisor.
Please help.
Regards,
Zuber Kazi

Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Aug 16, 2012, 3:38 AM EDT
try this
;with cte_Test as
(select *,ROW_NUMBER() over(PARTITION by storename order by storename) as rn from store)
select * from cte_Test a where rn =(select MAX(rn) from cte_Test b where b.Storename=a.Storename)
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Aug 16, 2012, 2:41 AM EDT
create table store
(Storename varchar(10),
manager char(10),
supervisor char(2))
insert into store values ('mumbai','a','x')
insert into store values ('mumbai','b','z')
insert into store values ('mumbai','c','y')
insert into store values ('thanae','d','x')
insert into store values ('thanae','e','x')
with cte_Test as
(select *,ROW_NUMBER() over(PARTITION by storename order by storename) as rn from store)
select * from cte_Test a where rn =(select MAX(rn) from cte_Test b where b.Storename=a.Storename)
Zuber Kazi38 reputationPosted Aug 16, 2012, 3:20 AM EDT
Zuber Kazi38 reputationPosted Aug 16, 2012, 4:44 AM EDT
But if I use it in stored procedure it throws same error.
please help
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Aug 16, 2012, 5:37 AM EDT