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.
| Author |
Topic |
|
omega1983
Starting Member
40 Posts |
Posted - 2009-12-01 : 10:36:48
|
| I have a table called dbo.hobby. In this table, one ID can have many different hobbies. I want to create 4 different tables. Here is an exampleHobbyKey ID Hobby table218 111 fishing hobby1219 111 boating hobby2220 112 fishing hobby1I want to structure the tables so that ID 111 would appear in both hobby1 and hobby2 tables due to the different HobbyKeys Here is a sketch of what I haveCREATE TABLE dbo.hobby1hobbykey pk,numeric(13) not null,hobbyID fk,char(10) not null,hobbycomm varchar(4000), nullFROM dbo.hobbywhere hobbytype = 'hobby'insert into dbo.hobby1 (hobbykey,hobbyid,hobbycomm)select hobbykey,hobbyid,hobbycommfrom dbo.hobbywhere hobbytype = 'hobby'Essentially if an ID has only one hobby they appear in hobby1 table. For each additional hobby, they must appear in a separate table |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-01 : 11:22:33
|
| [code]select * from (select *,row_number () over(partition by ID order by HobbyKey) as rnfrom hobby) awhere a.rn = 1[/code]will give you all rows with the first hobby (ordered by HobbyKey)[code]select * from (select *,row_number () over(partition by ID order by HobbyKey) as rnfrom hobby) awhere a.rn > 1[/code]will give you all additional hobbies for each ID.EDIT:But I must ask, why are you doing this? |
 |
|
|
|
|
|
|
|