具有随机查找值

时间:2015-05-09 14:36:28

标签: sql-server sql-update

我正在尝试更新潜在客户表以从查找表中分配随机人员。这是通用架构:

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有10个条目,每种类型的销售线索池都有一个条目。 TableBTableA中的每一行都有数千个条目。我想从recordOwners分配正确的TableC到每行的偶数行(或尽可能接近)。对于TableC或最多10行中的每一行,tableA将包含一个条目。

这可以在一个声明中完成吗?它不是必须的。我似乎无法找到速度的最佳方法。任何想法或样品都表示赞赏。

更新:
TableATableC有1对多的关系。对于TableA的每条记录,TableC至少会有一行,表示需要在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                 ....

基本上我需要根据与tableC的关系,将tableBtableA的记录随机分配。

3 个答案:

答案 0 :(得分:0)

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 )

应该可行,但未经过测试。

答案 1 :(得分:0)

试试这个:

2.xml

答案 2 :(得分:0)

我最终循环并根据我拥有的拥有者数量更新了x%​​的详细记录。最终结果是这样的:

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