分配记录SQL

时间:2014-07-02 19:53:36

标签: sql ssis

我的数据库中有四个表。我想要实现的是 - 基于分配给用户的组织 - 能够将订单分配给正确的用户,以及 - 能够加权(通过计算每个用户的案例)以避免向同一用户分配太多订单。

我完全不知道从哪里开始SQL ...任何想法?谢谢!

Orders
+--------------+--------------+--------------+------------+
| OrderNumber  | DateReceived | Organization | UserAssign |
+--------------+--------------+--------------+------------+
| ON2197802293 | 03.01.2014   |            6 | null       |
| ON2080463210 | 26.08.2014   |            2 | null       |
| ON8889958731 | 21.12.2013   |            7 | null       |
| ON6534106324 | 01.05.2014   |            7 | null       |
| ON5649138524 | 15.09.2014   |            6 | null       |
| ON3917853693 | 17.03.2014   |            8 | null       |
| ON5274601699 | 13.02.2014   |            2 | null       |
+--------------+--------------+--------------+------------+

Users
 +----+--------------+------------+
 | ID |   UserName   | CasesCount |
 +----+--------------+------------+
 |  1 | John Doe     | null       |
 |  2 | Adam Smith   | null       |
 |  3 | David Liu    | null       |
 |  4 | Deborah Poe  | null       |
 |  5 | Dan Bacon    | null       |
 |  6 | Dragan Tomic | null       |
 +----+--------------+------------+

+--------------+--------------+  +-----------+-------+
| Organization |              |  | UsersOrgs |       |
+--------------+--------------+  +-----------+-------+
| ID           | Organization |  | UserID    | OrgID |
| 1            | R120         |  | 1         | R120  |
| 2            | P234         |  | 1         | P234  |
| 3            | X097         |  | 2         | D982  |
| 4            | D982         |  | 3         | R120  |
| 5            | F938         |  | 3         | P234  |
| 6            | B726         |  | 3         | X097  |
| 7            | E311         |  | 3         | D982  |
| 8            | L221         |  | 4         | R120  |
| 9            | K991         |  | 4         | P234  |
| 10           | M982         |  | 5         | R120  |
| 11           | W123         |  | 5         | P234  |
+--------------+--------------+  | 5         | L221  |
                                 | 6         | R120  |
                                 | 6         | P234  |
                                 | 6         | X097  |
                                 +-----------+-------+

1 个答案:

答案 0 :(得分:0)

PSEUDO CODE:

Create Function UserAssignment(@OrgID)
declare returnvalue as same type as usersID
set returnvalue = -1
    Select top 1 count(*), userAssign  into returnValue from orders
    where organization = @orgID
    order by count desc, random()
   --if return value is -1 then no users assigned to ORG having order.
   userassignment = retvalue
end function

---现在更新所有现有订单...... 更新订单设置userAssign = UserAssignment(Organization),其中userAssign为null;

- 以及将来插入statemetns

insert into orders (Fields...,UserAssign) values (fields..., UserAssignment(@Organization)

或者不是在插入

上处理

- 在触发器中调用fucntion来更新值

- 修改表结构,根据对函数的调用设置默认值

如果您预计订单会通过非标准方式进入,那么最后2个非常有用。

- 编写更新空白订单的预定作业

Update Orders set userAssign = UserAssignment(Organization) where userAssign is null;