Author |
Topic |
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 05:17:54
|
Dear reader,See the table below:There are 2 colums.You see, The Mutatie_id has more than 1 record per Mutatie_id.Id Mutatie_id18 390319 390320 390321 390422 3904 23 390424 390525 390526 390627 390628 390629 390730 390831 390932 3909I want to add an extra column (that’s not the problem ;-) ) and in that column I want to place numbers(count) per Mutatie_IdSo I want a result like this:Id Mutatie_id Nw_column18 3903 119 3903 220 3903 321 3904 122 3904 223 3904 324 3905 125 3905 226 3906 127 3906 228 3906 329 3907 130 3908 131 3909 132 3909 2So I want a count per Mutatie_Id. So Mutatie_Id 3903 has three records, so the counting is: first record 1, second record 2, third record 3. Mutatie_Id 3909 for example has tow records, so the first is 1 and the second is 2Maybe I want to give the numbers in a sort order: ascend on MutatieDatumThank you very much!Greetings, GNGN |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 05:20:31
|
row_number() is your friend No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 05:22:22
|
select Id, Mutatie_id, row_number() over (partition by Mutatie_id order by Id ASC) as Nw_column from table No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 06:34:16
|
dear webfred,thank you!!! it works perfectly!!only one question: he shows the result but hij doesn't place the result in the column.I can do that myself by some actions but maybe there's a quicker manner to do this (in one command?)greetings, GNGN |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 06:42:21
|
update tset Nw_column = dt.Nw_columnfrom table as tjoin(select Id, Mutatie_id, row_number() over (partition by Mutatie_id order by Id ASC) as Nw_column from table)dton dt.Id = t.Id No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 07:00:45
|
t= table and dt= new table?because, I don't have a 'dt'..a new table is not created yetmaybe I misunderstand..GN |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 07:08:52
|
trust me it will work.dt is the alias for the dereived table:(select Id, Mutatie_id, row_number() over (partition by Mutatie_id order by Id ASC) as Nw_column from table)dtYou only have to change "table" by the name of your table. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 07:35:29
|
yes you're right!I got the whole formula now.however he gives an error on the server-name so I go try some things. If I can't get out, I'll post here again!thanks a lot for so far!!!!GN |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 08:08:41
|
So strange..Here below, ‘he’ recognizes the server: (everything works)insert [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen]select * from [ABFPDB01].[Intellact].dbo.autRegForm_VerzekeringenIn the next, ‘he’ also recognizes the server (everything Works):select ID,Mutatie_ID,MutatieDatum, row_number() over (partition by Mutatie_ID order by MutatieDatum ASC) as Nummer from [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen]But in the last, he doesn’t:update [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen]set Nummer = dt.Nummerfrom [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen] join(select ID,Mutatie_ID,MutatieDatum, row_number() over (partition by Mutatie_ID order by MutatieDatum ASC) as Nummer from [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen])dt on dt.ID=[ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen].IDhe gives error:Msg 7202, Level 11, State 2, Line 1Could not find server 'ABFTDB01' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.GN |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 08:34:02
|
Not sure about the cause but try using an alias:update tset Nummer = dt.Nummerfrom [ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen] as tjoin(select ID,Mutatie_ID,MutatieDatum, row_number() over (partition by Mutatie_ID order by MutatieDatum ASC) as Nummer from[ABFTDB01].[ABR_NAV50SP1_TST_GOOF].[ABRONA\gnaus].[dbo.Oefenomgeving$autRegForm_Verzekeringen])dt on dt.ID=t.ID No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 08:39:24
|
thanks,bu,it's a pity. still the same error...GN |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-06 : 09:12:46
|
can you do it directly on the destination server without the need for a linked server? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-06 : 09:33:31
|
yes I'll try thatGN |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-07 : 03:36:22
|
it still doesn't word :(update Oefenomgeving$autRegForm_Verzekeringenset Nummer = dt.Nummerfrom Oefenomgeving$autRegForm_Verzekeringen join(select ID,Mutatie_ID,MutatieDatum, row_number() over (partition by Mutatie_ID order by MutatieDatum ASC) as Nummer from Oefenomgeving$autRegForm_Verzekeringen)dt on dt.ID=Oefenomgeving$autRegForm_Verzekeringen.IDGN |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-07 : 03:40:25
|
sorry I mean: "doesn't worK"Msg 208, Level 16, State 1, Line 1Invalid object name 'Oefenomgeving$autRegForm_Verzekeringen'. and If I do this: update [ABRONA\gnaus].dbo.Oefenomgeving$autRegForm_Verzekeringenset Nummer = dt.Nummerfrom [ABRONA\gnaus].dbo.Oefenomgeving$autRegForm_Verzekeringen join(select ID,Mutatie_ID,MutatieDatum, row_number() over (partition by Mutatie_ID order by MutatieDatum ASC) as Nummer from [ABRONA\gnaus].dbo.Oefenomgeving$autRegForm_Verzekeringen)dt on dt.ID=[ABRONA\gnaus].dbo.Oefenomgeving$autRegForm_Verzekeringen.IDthen error:Msg 208, Level 16, State 1, Line 1Invalid object name 'ABRONA\gnaus.dbo.Oefenomgeving$autRegForm_Verzekeringen'.GN |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-07 : 04:15:45
|
sorry sorry! it's all my fault!I was performing on the wrong server!!thank you for help it works perfectly!!! :) :)GN |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-07 : 04:40:43
|
I'm glad to hear (...to read)  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
gnaus
Starting Member
41 Posts |
Posted - 2011-10-07 : 04:46:30
|
:)GN |
 |
|
|