行的等分布取决于其条件

时间:2018-05-29 14:48:54

标签: sql-server stored-procedures sql-server-2014 isqlquery

我是SQL的新手,我不熟悉存储过程,我目前正在使用SQL 2014.我有3个表;

> tblTransaction
    ID  Owner     Country   Region    Vendor    Description
    1   NULL      Country1  Region1   Vendor2   
    4   NULL      Country1  Region1   NULL  
    7   NULL      Country1  Region1   Vendor2   
    10  NULL      Country1  Region1   NULL  
    13  NULL      Country1  Region2   Vendor3   
    16  NULL      Country1  Region2   Vendor3   Description1
    19  NULL      Country1  Region2   Vendor3   Description1
    2   NULL      Country2  Region1   Vendor1   Description1
    5   NULL      Country2  Region2   Vendor1   
    8   NULL      Country2  Region1   Vendor1   
    11  NULL      Country2  Region1   Vendor2   Description1
    14  NULL      Country2  Region1   NULL  
    17  NULL      Country2  Region1   Vendor2   
    20  NULL      Country3  Region1   NULL  
    3   NULL      Country3  Region2   Vendor3   
    6   NULL      Country3  Region2   Vendor3   Description1
    9   NULL      Country3  Region2   NULL  
    12  NULL      Country3  Region2   NULL  
    15  NULL      Country3  Region1   Vendor1   
    18  NULL      Country3  Region2   Vendor1

> tblSkillMatrix
 Owner   IsTeamLead  Country    Vendor   Region     Description
Person1      N      Country1    Vendor1  Region1    
Person2      Y      Country2    Vendor2  Region2    Description1
Person3      N      Country2    Vendor3  Region2    
Person4      Y      Country1    Vendor4  Region1    Description1
Person5      N      Country1    Vendor4  Region1    

> tblAssignment(Output)
ID  Owner     Country   Region    Vendor    Description
1   Person1   Country1  Region1   Vendor2   
4   Person1   Country1  Region1   NULL  
7   Person5   Country1  Region1   Vendor2   
10  Person5   Country1  Region1   NULL  
13  Person4   Country1  Region2   Vendor3   
16  Person2   Country1  Region2   Vendor3   Description1
19  Person2   Country1  Region2   Vendor3   Description1
2   Person2   Country2  Region1   Vendor1   Description1
5   Person2   Country2  Region2   Vendor1   
8   Person2   Country2  Region1   Vendor1   
11  Person4   Country2  Region1   Vendor2   Description1
14  Person3   Country2  Region1   NULL  
17  Person3   Country2  Region1   Vendor2   
20  Person4   Country3  Region1   NULL  
3   Person3   Country3  Region2   Vendor3   
6   Person4   Country3  Region2   Vendor3   Description1
9   Person4   Country3  Region2   NULL  
12  Person2   Country3  Region2   NULL  
15  Person1   Country3  Region1   Vendor1   
18  Person3   Country3  Region2   Vendor1   

需要根据tblSkillMatrix中的条件更新tblTransaction,完成后,将其插入表tblAssignment。

以下是要求:

  • 为TeamLead分配ID(如果IsTeamLead =' Y')。说明=
    '说明1'
  • 如果tblSkillMatrix中不存在Country且Vendor为NULL, 无论国家/地区如何,都只向TeamLeads分配ID。
  • 将最多2个交易分配给每个国家/地区的TeamLead。

  • 每个国家/地区必须同等地支持非TeamLead。对于
    非TeamLead,如果tblSkillMatrix中不存在Country,则使用Region 代替。需要平等分配。

0 个答案:

没有答案