| Author |
Topic |
|
svicky9
Posting Yak Master
232 Posts |
Posted - 2007-05-29 : 07:01:31
|
| Hi FriendsI have a table like thisid variables ----------- ---------- NULL aNULL aNULL aNULL aNULL bNULL bNULL bNULL bNULL cNULL cNULL cNULL cI need to update the id field so that it assigns number 1 for a, number 2 for b and number 3 for c.I want the query without cursors.Vic |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-05-29 : 07:11:01
|
| Change the ID column to have the IDENTITY attribute?Kristen |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-29 : 07:26:12
|
| Update <your table> setid = Case <column has null> when 'a' then 1 when 'b' then 2 when 'c' then 3 end |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-05-29 : 07:29:00
|
quote: Originally posted by Kristen Change the ID column to have the IDENTITY attribute?Kristen
i think it will not work, coz o/p will beid variables1 a2 a3 a4 a5 b....likewise. n as per the req, he needs the o/p as:id variables1 a1 a1 a1 a2 b....3 cthanks,Mahesh |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-05-29 : 07:32:44
|
quote: Originally posted by svicky9 Hi FriendsI have a table like thisid variables ----------- ---------- NULL aNULL aNULL aNULL aNULL bNULL bNULL bNULL bNULL cNULL cNULL cNULL cI need to update the id field so that it assigns number 1 for a, number 2 for b and number 3 for c.I want the query without cursors.Vic
try out this:create table #Temp(ids numeric,variables varchar(2))goinsert into #temp (variables) values ('a')insert into #temp (variables) values ('a')insert into #temp (variables) values ('a')insert into #temp (variables) values ('a')insert into #temp (variables) values ('b')insert into #temp (variables) values ('b')insert into #temp (variables) values ('b')insert into #temp (variables) values ('b')insert into #temp (variables) values ('c')insert into #temp (variables) values ('c')insert into #temp (variables) values ('c')insert into #temp (variables) values ('c')goupdate #temp set ids = (case when variables = 'a' then 1when variables = 'b' then 2when variables = 'c' then 3 end)godrop table #tempgothanks,Mahesh |
 |
|
|
svicky9
Posting Yak Master
232 Posts |
Posted - 2007-05-29 : 08:30:46
|
| what if i have some 5 million distinct records in the variables columns???Vichttp://vicdba.blogspot.com |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-29 : 08:37:21
|
| try this...declare @t table (id int, var1 char(1))insert @tselect NULL, 'a' union allselect NULL, 'a' union allselect NULL, 'a' union allselect NULL,'a' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'c' union allselect NULL, 'c' union allselect NULL, 'c' union allselect NULL, 'c'declare @t1 table(id int identity(1,1), variable char(1))insert @t1Select distinct var1 from @tUpdate a setid = b.idfrom @t a join @t1 b on a.var1 = b.variable |
 |
|
|
vladimir.stokic
Starting Member
25 Posts |
Posted - 2007-05-29 : 09:09:51
|
| Can three queries do instead of one? If the answer is yes, then try:update <your_table> set Id = 1 where variables = 'a'update <your_table> set Id = 2 where variables = 'b'update <your_table> set Id = 3 where variables = 'c'And might I ask why you do not want cursors?Being a genius has its advantages... |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-29 : 09:54:02
|
quote: And might I ask why you do not want cursors?
And might I ask why you NEED cursors, especially in this case? Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
svicky9
Posting Yak Master
232 Posts |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-05-29 : 10:14:11
|
quote: Originally posted by pbguy try this...declare @t table (id int, var1 char(1))insert @tselect NULL, 'a' union allselect NULL, 'a' union allselect NULL, 'a' union allselect NULL,'a' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'b' union allselect NULL, 'c' union allselect NULL, 'c' union allselect NULL, 'c' union allselect NULL, 'c'declare @t1 table(id int identity(1,1), variable char(1))insert @t1Select distinct var1 from @tUpdate a setid = b.idfrom @t a join @t1 b on a.var1 = b.variable
Very nice!- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-05-29 : 18:11:57
|
Just for grins, here are 2 (very similar) ways do do it in 2005:-- Version 1UPDATE #tempSET ids = NumFROM ( SELECT variables, ROW_NUMBER() OVER (ORDER BY variables) AS Num FROM #temp GROUP BY Variables ) tWHERE #temp.variables = t.variables-- Version 2UPDATE tempSET ids = NumFROM #temp temp INNER JOIN ( SELECT variables, ROW_NUMBER() OVER (ORDER BY variables) AS Num FROM #temp GROUP BY Variables ) t ON temp.variables = t.variables -Ryan |
 |
|
|
vladimir.stokic
Starting Member
25 Posts |
Posted - 2007-05-30 : 02:41:56
|
quote: Originally posted by harsh_athalye
quote: And might I ask why you do not want cursors?
And might I ask why you NEED cursors, especially in this case? 
Maybe because this is a problem of repetitive nature. Any suggestions on how to solve it?Being a genius has its advantages... |
 |
|
|
|