在 SQL Server 中水平连接两个以上的表
Posted
技术标签:
【中文标题】在 SQL Server 中水平连接两个以上的表【英文标题】:Concatenate more than two tables horizontally in SQL Server 【发布时间】:2015-02-24 08:03:47 【问题描述】:以下是架构
+---------+---------+
| Employee Table |
+---------+---------+
| EmpId | Name |
+---------+---------+
| 1 | John |
| 2 | Lisa |
| 3 | Mike |
| | |
+---------+---------+
+---------+-----------------+
| Family Table |
+---------+-----------------+
| EmpId | Relationship |
+---------+-----------------+
| 1 | Father |
| 1 | Mother |
| 1 | Wife |
| 2 | Husband |
| 2 | Child |
+---------+-----------------+
+---------+---------+
| Loan Table |
+---------+--------+
| LoanId | EmpId |
+---------+--------+
| L1 | 1 |
| L2 | 1 |
| L3 | 2 |
| L4 | 2 |
| L5 | 3 |
+---------+--------+
Employee Table 和 Family Table 是一对多的关系
Employee Table 和 Loan Table 有一个多关系
我尝试过加入,但它给出了多余的行。
现在所需的输出将是
+---------+---------+--------------+---------+
| EmpId | Name | RelationShip | Loan |
+---------+---------+--------------+---------+
| 1 | John | Father | L1 |
| - | - | Mother | L2 |
| - | - | Wife | - |
| 2 | Lisa | Husband | L3 |
| - | - | Child | L4 |
| 3 | Mike | - | L5 |
| | | | |
+---------+---------+--------------+---------+
【问题讨论】:
贷款属于员工(1:N),家庭关系也属于员工(1:N)。您如何将 L2 和 L4 贷款归于非员工 - 这些信息不包含在数据中? 一个选择没有设置顺序,也没有排序来产生这个。现有列无法做到这一点。 【参考方案1】:您似乎正试图将贷款“按顺序”分配给家庭表中的行。解决这个问题的方法是首先获取正确的行,然后将贷款分配给行。
正确的行(和前三列)是:
select f.EmpId, e.Name, f.Relationship
from family f join
Employee e
on f.empid = e.empid;
请注意,这不会将连字符放在重复值的列中,而是放入实际值。尽管您可以在 SQL 中安排连字符,但这是一个坏主意。 SQL 结果采用表格的形式,表格是无序集,每列和每行都有值。当您开始输入连字符时,您取决于顺序。
现在的问题是加入贷款。这实际上很简单,通过使用row_number()
添加一个join
键:
select f.EmpId, e.Name, f.Relationship, l.LoanId
from Employee e left join
(select f.*, row_number() over (partition by f.EmpId order by (select NULL)) as seqnum
from family f
) f
on f.empid = e.empid left join
(select l.*, row_number() over (partition by l.EmpId order by (select NULL)) as seqnum
from Loan l
) l
on f.EmpId = l.EmpId and f.seqnum = l.seqnum;
请注意,这并不能保证给定员工的贷款分配顺序。您的数据似乎没有足够的信息来处理更一致的分配。
【讨论】:
@VladimirBaranov 。 . .现在应该。我用错误的表开始left join
s 链。【参考方案2】:
下面概述的方法允许轻松地将更多表“连接”到结果集。不限于两张表。
我将使用表变量来说明解决方案。在现实生活中,这些表当然是真实的表,而不是变量,但我会坚持使用变量,以使这个示例脚本易于运行和尝试。
declare @TEmployee table (EmpId int, Name varchar(50));
declare @TFamily table (EmpId int, Relationship varchar(50));
declare @TLoan table (EmpId int, LoanId varchar(50));
insert into @TEmployee values (1, 'John');
insert into @TEmployee values (2, 'Lisa');
insert into @TEmployee values (3, 'Mike');
insert into @TFamily values (1, 'Father');
insert into @TFamily values (1, 'Mother');
insert into @TFamily values (1, 'Wife');
insert into @TFamily values (2, 'Husband');
insert into @TFamily values (2, 'Child');
insert into @TLoan values (1, 'L1');
insert into @TLoan values (1, 'L2');
insert into @TLoan values (2, 'L3');
insert into @TLoan values (2, 'L4');
insert into @TLoan values (3, 'L5');
我们需要一个数字表。
SQL, Auxiliary table of numbers
http://web.archive.org/web/20150411042510/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
http://dataeducation.com/you-require-a-numbers-table/
同样,在现实生活中,您将拥有一个适当的数字表,但对于本示例,我将使用以下内容:
declare @TNumbers table (Number int);
insert into @TNumbers values (1);
insert into @TNumbers values (2);
insert into @TNumbers values (3);
insert into @TNumbers values (4);
insert into @TNumbers values (5);
我的方法背后的主要思想是首先创建一个帮助表,该表将包含每个 EmpId
的正确行数,然后使用该表有效地获得结果。
我们将从计算每个EmpId
的关系和贷款数量开始:
WITH
CTE_Rows
AS
(
SELECT Relationships.EmpId, COUNT(*) AS EmpRows
FROM @TFamily AS Relationships
GROUP BY Relationships.EmpId
UNION ALL
SELECT Loans.EmpId, COUNT(*) AS EmpRows
FROM @TLoan AS Loans
GROUP BY Loans.EmpId
)
然后我们计算每个EmpId
的最大行数:
,CTE_MaxRows
AS
(
SELECT
CTE_Rows.empid
,MAX(CTE_Rows.EmpRows) AS MaxEmpRows
FROM CTE_Rows
GROUP BY CTE_Rows.empid
)
上面的 CTE 对于每个 EmpId
: EmpId
本身都有一行,并且此 EmpId
的最大关系或贷款数。现在我们需要扩展这个表并为每个EmpId
生成给定的行数。这里我使用Numbers
表:
,CTE_RowNumbers
AS
(
SELECT
CTE_MaxRows.empid
,Numbers.Number AS rn
FROM
CTE_MaxRows
CROSS JOIN @TNumbers AS Numbers
WHERE
Numbers.Number <= CTE_MaxRows.MaxEmpRows
)
然后我们需要将行号添加到所有包含数据的表中,我们稍后将用于连接。您可以使用表格中的其他列对行号进行排序。对于这个例子,没有太多选择。
,CTE_Relationships
AS
(
SELECT
Relationships.EmpId
,ROW_NUMBER() OVER (PARTITION BY Relationships.EmpId ORDER BY Relationships.Relationship) AS rn
,Relationships.Relationship
FROM @TFamily AS Relationships
)
,CTE_Loans
AS
(
SELECT
Loans.EmpId
,ROW_NUMBER() OVER (PARTITION BY Loans.EmpId ORDER BY Loans.LoanId) AS rn
,Loans.LoanId
FROM @TLoan AS Loans
)
现在我们准备好将这一切结合在一起。 CTE_RowNumbers
有我们需要的确切行数,所以简单的LEFT JOIN
就足够了:
,CTE_Data
AS
(
SELECT
CTE_RowNumbers.empid
,CTE_Relationships.Relationship
,CTE_Loans.LoanId
FROM
CTE_RowNumbers
LEFT JOIN CTE_Relationships ON CTE_Relationships.EmpId = CTE_RowNumbers.EmpId AND CTE_Relationships.rn = CTE_RowNumbers.rn
LEFT JOIN CTE_Loans ON CTE_Loans.EmpId = CTE_RowNumbers.EmpId AND CTE_Loans.rn = CTE_RowNumbers.rn
)
我们差不多完成了。主Employee
表可能有一些EmpIds
没有任何相关数据,例如您的示例数据中的EmpId = 3
。为了在结果集中获得这些EmpIds
,我将把CTE_Data
加入到主表中,并将NULLs
替换为破折号:
SELECT
Employees.EmpId
,Employees.Name
,ISNULL(CTE_Data.Relationship, '-') AS Relationship
,ISNULL(CTE_Data.LoanId, '-') AS LoanId
FROM
@TEmployee AS Employees
LEFT JOIN CTE_Data ON CTE_Data.EmpId = Employees.EmpId
ORDER BY Employees.EmpId, Relationship, LoanId;
要获得完整的脚本,只需将这篇文章中的所有代码块按照它们在此处显示的顺序放在一起。
这是结果集:
EmpId Name Relationship LoanId
1 John Father L1
1 John Mother L2
1 John Wife -
2 Lisa Child L3
2 Lisa Husband L4
3 Mike - L5
【讨论】:
【参考方案3】:Vladimir Baranov 已经写了一个很好的解决方案,但它相当长(并且有一个小问题:您想要 Husband-L3 和 Child-L4,但此解决方案返回 Child-L3 和 Husband-L4)。
Gordon Linoff 编写了一个较短的解决方案,但它无法正常工作。
我可以修复 Gordon 的解决方案,如下所示:
SELECT e.EmpId, e.Name, f.Relationship, l.LoanId
FROM @TEmployee e
LEFT JOIN (
SELECT f.*, ROW_NUMBER() OVER (PARTITION BY f.EmpId ORDER BY (SELECT NULL)) AS seqnum
FROM @TFamily f
) f ON f.empid = e.empid
LEFT JOIN (
SELECT l.*, ROW_NUMBER() OVER (PARTITION BY l.EmpId ORDER BY (SELECT NULL)) AS seqnum
FROM @TLoan l
) l ON l.EmpId = e.EmpId AND (f.seqnum = l.seqnum OR f.seqnum IS NULL)
但是,我宁愿说这个问题是不正确的,因为它要求我们将家庭成员任意匹配特定的贷款(当不存在真正的关系时)。
我宁愿说正确的问题是具有以下答案的问题:
SELECT e.EmpId, e.Name,
SUBSTRING((
SELECT ', '+f.Relationship AS '*'
FROM @TFamily f
WHERE f.EmpId=e.EmpId
FOR XML PATH(''), TYPE
).value('.','nvarchar(4000)'),3,4000) AS FamilyMembers,
SUBSTRING((
SELECT ', '+l.LoanId AS '*'
FROM @TLoan l
WHERE l.EmpId=e.EmpId
FOR XML PATH(''), TYPE
).value('.','nvarchar(4000)'),3,4000) AS Loans
FROM @TEmployee e
【讨论】:
以上是关于在 SQL Server 中水平连接两个以上的表的主要内容,如果未能解决你的问题,请参考以下文章
如何在不使用pivot和unpivot的情况下在SQL Server中水平显示数据?
如何使用大小类和自动布局在 Xib 中水平对齐两个 UI 按钮