Sql 行号和附加的单个员工
SQL Server 2000,因此没有可用的行号 我需要将员工连接到免费线路 我有一个数据集1,它告诉我每个国家和地区的免费线路组合 表A–可使用的行号:Sql 行号和附加的单个员工,sql,sql-server,sql-server-2000,Sql,Sql Server,Sql Server 2000,SQL Server 2000,因此没有可用的行号 我需要将员工连接到免费线路 我有一个数据集1,它告诉我每个国家和地区的免费线路组合 表A–可使用的行号: Country Region Line Number Employee --------------------------------------------------- A 1 1 Null A 1
Country Region Line Number Employee
---------------------------------------------------
A 1 1 Null
A 1 2 Null
A 2 1 Null
表B–哪些员工可以填写缺失的行号:
Country Region Employee
----------------------------------------
A 1 Dave Smith
A 1 Johnny Cash
A 1 Peter Seller
A 2 David Donald
所以所需的输出是
表C-将单个员工添加到国家、地区、行号的每个组合中:
Country Region Line Number Employee
-------------------------------------------------------------
A 1 1 Dave Smith
A 1 2 Johnny Cash
A 2 1 David Donald
我在SQLServer2000中尝试了很多连接,包括自连接和交叉连接,但是没有得到所需的输出
这是我最后一次尝试:
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
您需要在国家/地区和地区之后为分配添加一个附加的联接键。为此,您可以使用行号:
您需要在国家/地区和地区之后为分配添加一个附加的联接键。为此,您可以使用行号:
把所有的建议、答案和评论放在一起
--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;
把所有的建议、答案和评论放在一起
--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;
在分区上按区域顺序按任意顺序向B添加一个行号,并在a中的行号上加入。如果行号不连续,则必须添加另一个行号。幸运的是,在sql server 2000上不可用,这会使操作更加困难。这是一个传统系统,您可以使用从选项卡中选择计数*获取行数,如t2,其中t1.employee@dnoeth-将其作为答案弹出,我可以给您打分……您考虑升级了吗?SQL2000已经失去支持将近十年了。从那时起,已经发布了两个主要版本,它们也不受支持。在分区上按区域顺序按任意顺序添加一个行号到B,并在a中的行号上加入。如果行号不连续,则必须添加另一个行号幸运的是,sql server 2000上不可用,这样做会更加困难。这是一个传统系统,您可以使用从选项卡中选择计数*获取行数,如t2,其中t1.employee@dnoeth-将其作为答案弹出,我可以给您打分……您考虑升级了吗?SQL2000已经失去支持将近十年了。从那时起,已经发布了两个主要版本,它们也都不受支持。不幸的是,sql server 2000上没有提供这两个版本,所以这会使它变得更加困难。不幸的是,它是一个遗留系统,在SQLServer2000上不可用,因此使它变得更加困难。这是一个遗留系统
+---------+--------+------------+--------------+
| Country | Region | LineNumber | Employee |
+---------+--------+------------+--------------+
| A | 1 | 1 | Dave Smith |
| A | 1 | 2 | Johnny Cash |
| A | 2 | 1 | David Donald |
+---------+--------+------------+--------------+