Mis*_*siu 7 sql t-sql sql-server-2005
我的数据库有3个表,如下所示:

订单表包含如下数据:
OrderID OperatorID GroupID OrderDesc Status Cash ...
--------------------------------------------------------------------------
1 1 1 small order 1 100
2 1 1 another order 2 0
3 1 2 xxxxxxxxxxx 2 1000
5 2 2 yyyyyyyyyyy 2 150
9 5 1 xxxxxxxxxxx 1 0
10 NULL 2 xxxxxxxxxxx 1 10
11 NULL 3 xxxxxxxxxxx 1 120
Run Code Online (Sandbox Code Playgroud)
经营者表:
OperatorID Name GroupID Active
---------------------------------------
1 John 1 1
2 Kate 1 1
4 Jack 2 1
5 Will 1 0
6 Sam 3 1
Run Code Online (Sandbox Code Playgroud)
组表:
GroupID Name
---------------
1 G1
2 G2
3 X1
Run Code Online (Sandbox Code Playgroud)
你可以看到John有3个订单,Kate 1,Will 1,Jack和Sam没有.
现在我想基于某些条件将操作员分配给订单:
这是我想得到的结果:
OrderID OperatorID GroupID OrderDesc Status Cash ...
--------------------------------------------------------------------------
1 1 1 small order 1 100 < change
2 1 1 another order 2 0
3 2 2 xxxxxxxxxxx 2 1000 < change
5 4 2 yyyyyyyyyyy 2 150 < change
9 5 1 xxxxxxxxxxx 1 0
10 4 2 xxxxxxxxxxx 1 10 < change
11 NULL 3 xxxxxxxxxxx 1 120
Run Code Online (Sandbox Code Playgroud)
我想随机播放订单并更新operatorID,以便每次调用此脚本时我都会随机分配operatorID,但每个运营商都会有相同数量或订单(接近相等,因为如果我有7个订单,一个人将有3个并且休息2).
我可以使用NTILE将订单分配到组中,但我需要将operatorID分配给该组.
我想我需要做这样的事情:
SELECT NTILE(2) OVER( order by orderID desc) as newID,*
FROM
orders(NOLOCK)
Run Code Online (Sandbox Code Playgroud)
这将给我的订单表分成相等的部分.我需要知道的是运算符表的长度(将其作为参数添加到NTILE),之后我可以将我的结果与运算符联接(使用row_number())
有更好的解决方案吗?
我的问题:如何将结果集平均分成组并使用另一个表数据更新该记录集?
编辑: 这是我的代码到目前为止:http://sqlfiddle.com/#!3/39849/25
编辑2 我更新了我的问题并添加了更多条件.
我想根据一些条件为运营商分配订单:
我正在将此查询构建为存储过程.
因此,第一步是生成具有新分配到临时表的数据,并在第二步中最终批准后基于该临时表更新主表.
我还有两个问题:
首先选择所有符合临时表条件的所有订单和所有运营商,然后进行改组或在一个大查询中完成所有操作会更好吗?
我想将数组或组作为参数传递给我的过程.哪个选项最好将数组传递给存储过程(SQL Server 2005).
我知道这次被问过多次,但我想知道创建一个单独的函数是否更好,将逗号分隔的字符串切换成表格(http://www.sommarskog.se/arrays-in-sql-2005.html)或将一切都放在一个大胖程序中?:)
最终答案:可在http://sqlfiddle.com/#!3/afb48/2获取
SELECT o.*, op.operatorName AS NewOperator, op.operatorID AS NewOperatorId
FROM (SELECT o.*, (ROW_NUMBER() over (ORDER BY newid()) % numoperators) + 1 AS randseqnum
FROM Orders o CROSS JOIN
(SELECT COUNT(*) AS numoperators FROM operators WHERE operators.active=1) op
WHERE o.cash>0 and o.status in (1,3)
) o JOIN
(SELECT op.*, ROW_NUMBER() over (ORDER BY newid()) AS seqnum
FROM Operators op WHERE op.active=1
) op
ON o.randseqnum = op.seqnum ORDER BY o.orderID
Run Code Online (Sandbox Code Playgroud)
答案基于戈登的Linoff答案.谢谢!
我不确定您是否真的想要更新查询或选择查询。以下查询根据您的条件为每个订单返回一个新运算符:
/*
with orders as (select 1 as orderId, 'order1' as orderDesc, 1 as OperatorId),
operators as (select 1 as operatorID, 'John' as name)
*/
select o.*, op.name as NewOperator, op.operatorID as NewOperatorId
from (select o.*, (ROW_NUMBER() over (order by newid()) % numoperators) + 1 as randseqnum
from Orders o cross join
(select COUNT(*) as numoperators from operators) op
) o join
(select op.*, ROW_NUMBER() over (order by newid()) as seqnum
from Operators op
) op
on o.randseqnum = op.seqnum order by orderid
Run Code Online (Sandbox Code Playgroud)
它基本上为连接的行分配了一个新的 id。顺序表的值介于 1 和随机分配的操作员数量之间。然后将其连接到运算符上的序列号。
如果您需要更新,那么您可以执行以下操作:
with toupdate as (<above query>)
update orders
set operatorid = newoperatorid
from toupdate
where toupdate.orderid = orders.orderid
Run Code Online (Sandbox Code Playgroud)
你的两个问题:
是先将所有订单和满足条件的所有运算符选择到临时表中,然后进行洗牌还是在一个大查询中完成所有操作会更好?
临时表的用户取决于应用程序的性能和要求。如果数据正在快速更新,那么是的,使用临时表是一个巨大的胜利。如果您对相同的数据多次运行随机化,那么这可能是一个胜利,特别是当表太大而无法放入内存时。否则,假设您将条件放在最里面的子查询中,则一次性运行不太可能获得很大的性能提升。但是,如果性能是一个问题,您可以测试这两种方法。
我想将数组或组作为参数传递给我的过程。哪个选项最适合将数组传递给存储过程 (SQL Server 2005)。
嗯,切换到 2008 年,其中包含表值参数。这是 Erland Sommarskog 撰写的关于该主题的高度参考文章: http://www.sommarskog.se/arrays-in-sql-2005.html。
| 归档时间: |
|
| 查看次数: |
2973 次 |
| 最近记录: |