473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rupdating duplicate entries with random numbers

Hi ..
i am trying to update a table where if field contents any duplictaed
entries than one of the field should be updated with random number
which is unique so i can make all entries unique
i searched a lot but couldn't find any solution which i could
understand easily .
is it very difficult in sql to update duplicate entries with new
unique random values?
table example
----------------------
id | name | age
----------------------
1 a 24
2 b 24
3 c 28
4 d 12
5 e 12
in the above table id is unique but the age is same for a , b and
d,e . what i am trying to do is that in a single query i can update
table set age=age+random unique number where duplicate age exists .
that mean b will become 24+some random number and e will be 12+some
random number

hope someone reply's soon
thanks

Jul 10 '08 #1
7 2936
What version of SQL Server are you running? It makes a BIG
difference. Or, as your name ( php_mysql_begin er911) suggests are you
using MySQL but posting questions to a Microsoft SQL Server group?

Here is an answer that will only work in SQL Server, but will work in
any version.

UPDATE TheTable
SET age = RAND() * 10000
FROM (SELECT name, age, count(*) as dups, min(id) as keepme
FROM TheTable
GROUP BY name, age
HAVING count(*) 1) as X
WHERE TheTable.name = X.name
AND TheTable.age = X.age
AND TheTable.id <X.keepme

Roy Harvey
Beacon Falls, CT

On Wed, 9 Jul 2008 22:42:17 -0700 (PDT), php_mysql_begin er911
<de******@gmail .comwrote:
>Hi ..
i am trying to update a table where if field contents any duplictaed
entries than one of the field should be updated with random number
which is unique so i can make all entries unique
i searched a lot but couldn't find any solution which i could
understand easily .
is it very difficult in sql to update duplicate entries with new
unique random values?
table example
----------------------
id | name | age
----------------------
1 a 24
2 b 24
3 c 28
4 d 12
5 e 12
in the above table id is unique but the age is same for a , b and
d,e . what i am trying to do is that in a single query i can update
table set age=age+random unique number where duplicate age exists .
that mean b will become 24+some random number and e will be 12+some
random number

hope someone reply's soon
thanks
Jul 10 '08 #2
Roy Harvey (SQL Server MVP) (ro********@sne t.net) writes:
What version of SQL Server are you running? It makes a BIG
difference. Or, as your name ( php_mysql_begin er911) suggests are you
using MySQL but posting questions to a Microsoft SQL Server group?

Here is an answer that will only work in SQL Server, but will work in
any version.

UPDATE TheTable
SET age = RAND() * 10000
FROM (SELECT name, age, count(*) as dups, min(id) as keepme
FROM TheTable
GROUP BY name, age
HAVING count(*) 1) as X
WHERE TheTable.name = X.name
AND TheTable.age = X.age
AND TheTable.id <X.keepme
Nah, there are a couple of problems. First, in php_mysql_begin er911's
sample data, the name was unique, so that condition should be removed.
Next, rand() is evaluated once, so all rows get the same new age. This
can be addressed with instead using checksum(newid( )). We still need
one more thing trough, a loop that runs the update until @@rowcount is
0. That is, since new numbers are random, we may get new duplicates. So

WHILE 1 = 1
BEGIN
UPDATE TheTable
SET age = checksum(newid( ))
FROM (SELECT age, count(*) as dups, min(id) as keepme
FROM TheTable
GROUP BY age
HAVING count(*) 1) as X
WHERE TheTable.age = X.age
AND TheTable.id <X.keepme
IF @@rowcont = 0 BREAK
END

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 10 '08 #3
Thanks, Erland, for straightening that out. 8-)

Roy

On Thu, 10 Jul 2008 22:08:33 +0000 (UTC), Erland Sommarskog
<es****@sommars kog.sewrote:
>Nah, there are a couple of problems. First, in php_mysql_begin er911's
sample data, the name was unique, so that condition should be removed.
Next, rand() is evaluated once, so all rows get the same new age. This
can be addressed with instead using checksum(newid( )). We still need
one more thing trough, a loop that runs the update until @@rowcount is
0. That is, since new numbers are random, we may get new duplicates. So

WHILE 1 = 1
BEGIN
UPDATE TheTable
SET age = checksum(newid( ))
FROM (SELECT age, count(*) as dups, min(id) as keepme
FROM TheTable
GROUP BY age
HAVING count(*) 1) as X
WHERE TheTable.age = X.age
AND TheTable.id <X.keepme
IF @@rowcont = 0 BREAK
END

Jul 10 '08 #4
Hi Roy and Erland ..
thanks for the replying so quick..
i tried running that query in foxpro 6.0 but it givers error
" unrecognized command verb "
Jul 11 '08 #5
On Fri, 11 Jul 2008 01:54:31 -0700 (PDT), php_mysql_begin er911
<de******@gmail .comwrote:
>Hi Roy and Erland ..
thanks for the replying so quick..
i tried running that query in foxpro 6.0 but it givers error
" unrecognized command verb "
So? You asked your question in a group devoted to Microsoft SQL
Server. If you aren't using SQL Server you are wasting everyone's
time asking here.

Roy Harvey
Beacon Falls, CT
Jul 11 '08 #6
Hi Roy..
it's my mistake i thought foxpro uses sql so it would be ok to ask
here
my apolozies
and thanks for the help
Jul 13 '08 #7
php_mysql_begin er911 (de******@gmail .com) writes:
it's my mistake i thought foxpro uses sql so it would be ok to ask
here
Foxpro does indeed use SQL, but there are considerable differences
between SQL dialects in different products, and you cannot always expect
a query that works in one product to work in another. Had you specified
that you wanted a portable query, we might have tried to compose one
for you. Although, I think it would have been difficult in this case,
as there is no portable randomisation construct in SQL as far as I know.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 13 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
838
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran to completion. Version 4 seems to STOP when it encounters a duplicate entry, so that the records before the duplicate are inserted and the records after the duplicate are not inserted. 3.22.27.1 - previous ver MySQL that did not return error...
3
6943
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE t1.id < t2.id
5
1905
by: Jermin | last post by:
Hi, I have a database composed of a single table composed of 2 columns, an auto-numbered ID column and a column which contains 30 million random numbers. All I want to Access to do is check the numbers and let me know if there are any duplicates. Ideally I'd like Access to flag the duplicates if they exist. The easy approach that I thought would work is to go to Design view and select Indexed table, No Duplicates. This doesn't work...
5
2225
by: Chris Lasher | last post by:
Hello Pythonistas! I'm looking for a way to duplicate entries in a symmetrical matrix that's composed of genetic distances. For example, suppose I have a matrix like the following: A B C A 0.000000 0.500000 1.000000 B 0.500000 0.000000 0.500000 C 1.000000 0.500000 0.000000
5
3993
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone | ------------------------------------------------------- | mr x | 8th lane | 124364 | | mr x | 6th lane | 435783 | | mrs x | 6th lane | 435783 |
2
45881
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers and the corresponding model numbers (primary key on serial_numbers) Since Table A has no constraints duplicates may exist. Additionally, table A is actually an Excel spreadsheet that is maintained by an employee that records new hardware as...
9
11235
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field Size is set to "Long Integer", and New Values is set to "Increment".) I know that an old version of the Jet database engine can cause this problem, but my version of msjet40.dll is 4.0.8618.0, which is supposedly bug-free in this respect. I am...
12
20806
by: joestevens232 | last post by:
Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input into an array. Write a function that processes the array so that any duplicate values are eliminated. Write an output function that prints out the values of the array. You can assume that there are no more than 20 integers in the input. But there may be less. Zero...
4
2913
by: ramdil | last post by:
Hi All I have table and it have around 90000 records.Its primary key is autonumber field and it has also have date column and name, then some other columns Now i have problem with the table,as my table contains duplicate entries for a particular date.How can i delete the duplicate entries from the table for that particular column,Now i am doing manually with name column as it will be unique for that date.Can any one help me giving the query...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.