Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Update with random number

Posted on 2013-02-06
4
Medium Priority
?
280 Views
Last Modified: 2013-02-11
Hello,

I would like to randomize values in a table.
The table we call MainTable It has columns UserId, Username, ColM (int).

Then I have another table with the random values.
The table we call PoolTable. It has columns Id, PoolCol (int)

I would like to find random values for each UserId. I would like to replace ColM with the value of PoolCol.

Example:
PoolTable  
Id=1, PoolCol=55
Id=2, PoolCol=73
Id=3, PoolCol=32

MainTable
Id=1, UserName="John", ColM=5
Id=2, UserName="Bill",ColM=5
Id=3, UserName="Howard", ColM=5
Id=4, UserName="Michael",ColM=5

One example of result could be
Id=1, UserName="John", ColM=73
Id=2, UserName="Bill",ColM=32
Id=3, UserName="Howard", ColM=55
Id=4, UserName="Michael",ColM=73

How is best to do this?
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38861990
update u
  set colm = x.poolcol
  from (select x.*,row_number() over (order by newid()) as rn
                from maintable as x) as U
  inner join pooltable as x
  on x.id=u.rn
 where u.colm<>x.poolcol

one method use the windowing function over to calculate a random row_number for each row in the main table by using newid()  to achieve a random order to the rows....
then join the row number to the id of the pool table and update...
0
 

Author Comment

by:johnkainn
ID: 38863422
Thank you. How would I modify this so that when there are more items in MainTable then in PoolTable each user would get a random item from the Pool table. (It could also be the other way around, i.e. more items in pool table then in MainTable).
I created temporary table so you can run this. I added 2 names to the Maintable . Below is also the result that comes when I run it so you understand what I mean.

DECLARE @MainTable table(UserId int, Username varchar(50),ColM int)
INSERT INTO @MainTable(UserId,Username,ColM)values(1,'John',5)
INSERT INTO @MainTable(UserId,Username,ColM)values(2,'Bill',5)
INSERT INTO @MainTable(UserId,Username,ColM)values(3,'Howard',5)
INSERT INTO @MainTable(UserId,Username,ColM)values(4,'Michael',5)
INSERT INTO @MainTable(UserId,Username,ColM)values(5,'Anne',5)
INSERT INTO @MainTable(UserId,Username,ColM)values(6,'Mary',5)

DECLARE @PoolTable table(Id int, PoolCol int)
INSERT INTO @PoolTable(Id,PoolCol)values(1,55)
INSERT INTO @PoolTable(Id,PoolCol)values(2,73)
INSERT INTO @PoolTable(Id,PoolCol)values(3,32)

update u
  set colm = x.poolcol
  from (select x.*,row_number() over (order by newid()) as rn
                from @MainTable as x) as U
  inner join @PoolTable as x
  on x.id=u.rn
 where u.colm<>x.poolcol


SELECT * FROM @MainTable

Open in new window


The result if I run this is

1      John      5
2      Bill      73
3      Howard      32
4      Michael      55
5      Anne      5
6      Mary      5

You see that the random number is only updated for 3 users.
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 38863931
a simple method would be to use the modulus of the number of rows in the pool table to conform the ranges...

update u
  set colm = x.poolcol
  from (select x.*
              ,((row_number() over (order by newid()))
                    % ((select count(*) from @pooltable) - 1))+1 as rn
                from @MainTable as x) as U
  inner join @PoolTable as x
  on x.id=u.rn
 where u.colm<>x.poolcol

so rn generates numbers in the range 1 - number of rows in @pooltable

you may need to go a row_number() over (order by id) as rid  on the pooltable
and join rn to rid if there is the possibility that the pooltable ids would be sequential from 1..
0
 

Author Closing Comment

by:johnkainn
ID: 38876197
Thank you very much:-)
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question