SQL Update row column with random lookup value

三世轮回 提交于 2019-12-25 03:34:33

问题


I am trying to update a lead table to assign a random person from a lookup table. Here is the generic schema:

TableA (header),
ID int,
name varchar (30)

TableB (detail),
ID int,
fkTableA int, (foreign key to TableA.ID)
recordOwner varchar(30) null
other detail colums..

TableC (owners),
ID int,
fkTableA int (foreign key to TableA.ID)
name varchar(30)

TableA has 10 entries, one for each type of sales lead pool. TableB has thousands of entries for each row in TableA. I want to assign the correct recordOwners from TableC to and even number of rows each (or as close as I can). TableC will have anywhere from one entry for each row in tableA or up to 10.

Can this be done in one statement? It doesn't have to be. I can't seem to figure out the best approach for speed. Any thoughts or samples are appreciated.

Updated:
TableA has a 1 to many relation ship with TableC. For every record of TableA, TableC will have at least one row, which represents an owner that will need to be assigned to a row in TableB.

TableA
int  name
1    LeadSourceOne
2    LeadSourceTwo

TableC
int(id) int(fkTableA) varchar(name)
1       1             Tom
2       1             Bob
3       2             Timmy
4       2             John
5       2             Steve
6       2             Bill

TableB initial data
int(id) int(fkTableA) varchar(recordOwner) (other detail columns)
1       1             NULL                 ....
2       1             NULL                 ....
3       1             NULL                 ....
4       2             NULL                 ....
5       2             NULL                 ....
6       2             NULL                 ....
7       2             NULL                 ....
8       2             NULL                 ....
9       2             NULL                 ....

TableB end result
int(id) int(fkTableA) varchar(recordOwner) (other detail columns)
1       1             TOM                  ....
2       1             BOB                  ....
3       1             TOM                  ....
4       2             TIMMY                ....
5       2             JOHN                 ....
6       2             STEVE                ....
7       2             BILL                 ....
8       2             TIMMY                ....
9       2             BILL                 ....

Basically I need to randomly assign a record from tableC to tableB based on the relationship to tableA.


回答1:


UPDATE TabB SET name = (SELECT TOP 1 coalesce(tabC.name,'') FROM TabC  INNER JOIN TabA ON TabC.idA  = TabA.id  WHERE tabA.Id = TabB.idA )

Should work but its not tested.




回答2:


Try this:

UPDATE TableB
SET recordOwner = (SELECT TOP(1) [name] 
                   FROM TableC
                   ORDER BY NEWID())
WHERE recordOwner IS NULL



回答3:


I ended up looping thru and updating x percent of the detail records based on how many owners I had. The end result is something like this:

create table #tb_owners(userId varchar(30), processed bit)

    insert into #tb_owners(
        userId, 
        processed)
    select userId = name, 
        processed = 0
    from tableC 
    where fkTableA = 1

    select @percentUpdate = cast(100 / count(*) as numeric(8,2))
    from #tb_owners

    while exists(select 1 from #tb_owners o where o.processed = 0)
        begin
            select top 1 
                @userFullName = o.name
            from #tb_owners o
            where o.processed = 0
            order by newId()


            update tableB
            set recordOwner = @userFullName
            from tableB ptbpd
                inner join (
                            select top (@percentUpdate) percent 
                                id
                            from tableB
                            where recordOwner is null
                            order by newId()
                    ) nd on (ptbpd.id = nd.id)


            update #tb_owners
            set processed = 1
            where userId = @oUserId
        end

    --there may be some left over, set to last person used
    update tableB
    set recordOwner = @userFullName
    from tableB
    where ptbpd.recordOwner is null


来源:https://stackoverflow.com/questions/30141181/sql-update-row-column-with-random-lookup-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!