行号和附加单个员工

时间:2018-03-12 19:52:35

标签: sql sql-server sql-server-2000

SQL Server 2000所以没有Country Region Line Number Employee --------------------------------------------------- A 1 1 Null A 1 2 Null A 2 1 Null 可用....

我需要将员工加入自由职业道。

我有一个数据集1告诉我每个国家和地区的自由行组合。

表A - 要使用的可用行号:

Country     Region      Employee
----------------------------------------
A             1         Dave Smith
A             1         Johnny Cash
A             1         Peter Seller 
A             2         David Donald

表B - 员工可以填写缺失的行号:

Country     Region       Line Number         Employee
-------------------------------------------------------------
A            1                 1             Dave Smith
A            1                 2             Johnny Cash                    
A            2                 1             David Donald

所需的输出是

表C - 将单个员工附加到国家,地区,行号的每个组合:

Select  
    A.Country, A.Region, A.Line Number, 
    B.Employee 
From 
    Table_A A
Inner Join 
    Table_B B On A.Country = B.Country and A.Region = B.Region

我在SQL Server 2000中尝试了很多连接,包括自连接和交叉连接,但无法获得所需的输出。

这是我的最后一次尝试:

get-job

2 个答案:

答案 0 :(得分:1)

您需要在joincountry之后使用额外的region密钥进行分配。为此,您可以使用row_number()

select a.*, b.employee
from (select a.*,
             row_number() over (partition by country, region order by linenumber) as seqnum
      from table_a a
     ) a join
     (select b.*
             row_number() over (partition by country, region order by (select null) ) as seqnum
      from b
     ) b
     on b.country = a.country and b.region = a.region and b.seqnum = a.seqnum

答案 1 :(得分:1)

将所有建议,答案和评论汇总在一起。

--Setting up the tables as given:
CREATE TABLE #e (
  Country  char(1),
  Region int,
  LineNumber int,
  Employee varchar(50));

INSERT #e 
VALUES ('A', 1, 1,NULL)
,('A',1,2,NULL)
,('A',2,1,NULL);

CREATE TABLE #r (
Country char(1),
Region int,
Employee varchar(50));

INSERT #r
VALUES 
 ('A', 1, 'Dave Smith')
,('A', 1, 'Johnny Cash')
,('A', 1, 'Peter Sellers') 
,('A', 2, 'David Donald');

--Creating a temporary table with
--a line number to join on.
CREATE TABLE #T(
LineNumber int,
Country char(1),
Region int,
Employee varchar(50));

--Populate the temporary table
--with the line number data.
INSERT INTO #T
(
  LineNumber,
  Country,
  Region,
  Employee
)
SELECT 
  (SELECT 
    COUNT(*) AS Line 
   FROM #r AS R2 
   WHERE R2.Employee <= #r.Employee 
    AND R2.Region = #r.Region
  ) AS LineNumber,
  Country,
  Region,
  Employee
FROM #r;

--Set up the final output.
SELECT  
    A.Country, 
    A.Region, 
    A.LineNumber, 
    B.Employee 
FROM 
    #e A
INNER JOIN 
    #T B 
      ON A.Country = B.Country 
      AND A.Region = B.Region
      AND A.LineNumber = B.LineNumber
ORDER BY
  A.Country, 
  A.Region, 
  A.LineNumber;

--Clean up.
DROP TABLE #r;
DROP TABLE #T;
DROP TABLE #e;

结果:

+---------+--------+------------+--------------+
| Country | Region | LineNumber |   Employee   |
+---------+--------+------------+--------------+
| A       |      1 |          1 | Dave Smith   |
| A       |      1 |          2 | Johnny Cash  |
| A       |      2 |          1 | David Donald |
+---------+--------+------------+--------------+