Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 generate a unique key for 3 columns

Author  Topic 

zoe2003
Starting Member

17 Posts

Posted - 2013-01-07 : 05:20:10
Hello all,

I need help in generating a unique key for 3 columns in a view.
For example : I have a view that returns those columns:
f1 f2 f3
111 222 333
111 222 333
aaa bbb ccc
aaa bbb ddd
aaa bbb ccc
111 222 333

I want to add another field that will be a unique key for those 3 fields, like that :
key f1 f2 f3
1 111 222 333
1 111 222 333
2 aaa bbb ccc
3 aaa bbb ddd
2 aaa bbb ccc
1 111 222 333

How can I do it ?

I appreciate your help.

Thanks,
Z.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2013-01-07 : 05:34:04
select a.seq, b.*
from
(
select distinct f1.f2,f3 seq = row_number() from tbl
) a
join tbl b
on a.f1 = b.f1 and a.f2 = b.f2 and a.f3 = b.f3

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sivas2k8
Starting Member

1 Post

Posted - 2013-01-07 : 06:28:39
Try this. Sure you'll find the solution here..
http://sivanandsql.blogspot.in/2013/01/genenerating-unique-key-for-3-columns.html
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 09:42:04
[code]
select *
from
(
select dense_rank() over (order by f1,f2,f3) AS key,*
from table
)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

zoe2003
Starting Member

17 Posts

Posted - 2013-01-07 : 10:23:33
Thank you all !
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 10:33:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -