SQL 使用随机查找值更新行列
SQL Update row column with random lookup value
我正在尝试更新潜在客户 table 以从查找 table 中随机分配一个人。这是通用架构:
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 个条目,每种类型的销售线索池一个。 TableB
TableA
中的每一行都有数千个条目。我想将正确的 recordOwners
从 TableC
分配给偶数行(或尽可能接近)。 TableC
将从 tableA
中每一行的一个条目到最多 10 个条目。
这可以在一个语句中完成吗?它不一定是。我似乎无法找出提高速度的最佳方法。任何想法或样本都将受到赞赏。
已更新:
TableA
与 TableC
具有一对多关系。对于 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 ....
基本上我需要根据与tableA
的关系随机分配一条记录从tableC
到tableB
。
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 )
应该可以,但未经过测试。
试试这个:
UPDATE TableB
SET recordOwner = (SELECT TOP(1) [name]
FROM TableC
ORDER BY NEWID())
WHERE recordOwner IS NULL
我最终循环遍历并根据我拥有的所有者数量更新了 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
我正在尝试更新潜在客户 table 以从查找 table 中随机分配一个人。这是通用架构:
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 个条目,每种类型的销售线索池一个。 TableB
TableA
中的每一行都有数千个条目。我想将正确的 recordOwners
从 TableC
分配给偶数行(或尽可能接近)。 TableC
将从 tableA
中每一行的一个条目到最多 10 个条目。
这可以在一个语句中完成吗?它不一定是。我似乎无法找出提高速度的最佳方法。任何想法或样本都将受到赞赏。
已更新:
TableA
与 TableC
具有一对多关系。对于 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 ....
基本上我需要根据与tableA
的关系随机分配一条记录从tableC
到tableB
。
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 )
应该可以,但未经过测试。
试试这个:
UPDATE TableB
SET recordOwner = (SELECT TOP(1) [name]
FROM TableC
ORDER BY NEWID())
WHERE recordOwner IS NULL
我最终循环遍历并根据我拥有的所有者数量更新了 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