get first record from different databasename data in sql server -


i have data in emp table:

id  |dbname 1   |db1 2   |db1 1   |db1 2   |db2 3   |db2 5   |db2 1   |db2 4   |db3 1   |db3 3   |db3 

emp table has database related records (db1,db2,db3... etc) , if same id exists in different database(db1,db2,db3 etc) need retrieve first dbname. based on above table data want output below

id  |  dbname 1   |  db1 2   |  db1 3   |  db2 5   |  db2 4   |  db3 

please try solution:

create table #emptable (id int, dbname varchar(10))  insert #emptable values (1,'db1'), (2,'db1'), (1,'db1'), (2,'db2'), (3,'db2'), (5,'db2'), (1,'db2'), (4,'db3'), (1,'db3'), (3,'db3')  select a.id, a.dbname (     select e.*, row_number() on (partition id order id) rnk     #emptable e ) a.rnk = 1 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -