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 |
|
missMac
Posting Yak Master
124 Posts |
Posted - 2008-12-19 : 13:52:59
|
| Hello,I have a table of duplicate numbers, how do i update the distinct numbers and set the value of a column [isunique] = 1Thanks |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-12-19 : 15:50:36
|
| I don't know what your tables look like, but this will give you a startECLARE @Table TABLE (id int identity (1,1), Num int,isunique tinyint)INSERT INTO @TableSELECT 1,0 UNION ALLSELECT 1,0 UNION ALLSELECT 2,0 UNION ALLSELECT 3,0 UNION ALLSELECT 4,0 UNION ALLSELECT 4,0 UPDATE @TABLESET isunique = 1FROM @Table aINNER JOIN (select num,[res] = count(*) from @table group by num having count(*) = 1) bON a.num = b.numselect * from @tableJim |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-19 : 16:00:46
|
quote: Originally posted by missMac Hello,I have a table of duplicate numbers, how do i update the distinct numbers and set the value of a column [isunique] = 1Thanks
Update tablenameSet isunique = (case when col1 in(select col1 from tablename group by idHaving count(Col1)=1) then 1 else 0 end) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-19 : 22:35:46
|
| SELECT id,num,CASE WHEN num in(SELECT num FROM @table GROUP BY numHaving count(*)=1) THEN 1 ELSE 0 END AS 'isunique' FROM @tablejust using case function try this once |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-19 : 22:52:53
|
quote: Originally posted by bklr SELECT id,num,CASE WHEN num in(SELECT num FROM @table GROUP BY numHaving count(*)=1) THEN 1 ELSE 0 END AS 'isunique' FROM @tablejust using case function try this once
What did I use? Why are you repeating same thing ? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-19 : 22:55:00
|
quote: Originally posted by sodeep
quote: Originally posted by bklr SELECT id,num,CASE WHEN num in(SELECT num FROM @table GROUP BY numHaving count(*)=1) THEN 1 ELSE 0 END AS 'isunique' FROM @tablejust using case function try this once
What did I use? Why are you repeating same thing ?
UR o\p is like this so check it once so that i had modified and send the query 1 1 02 1 03 2 04 3 05 4 06 4 0 |
 |
|
|
|
|
|
|
|