gpt4 book ai didi

sql - 在 SQL Server 中水平连接两个以上的表

转载 作者:行者123 更新时间:2023-12-04 23:57:31 30 4
gpt4 key购买 nike

以下是架构

+---------+---------+
| 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表和Family表是一对多的关系
  • Employee表和Loan表有很多关系

  • 我试过 Joins 但它给出了多余的行。

    现在所需的输出将是
    +---------+---------+--------------+---------+
    | EmpId | Name | RelationShip | Loan |
    +---------+---------+--------------+---------+
    | 1 | John | Father | L1 |
    | - | - | Mother | L2 |
    | - | - | Wife | - |
    | 2 | Lisa | Husband | L3 |
    | - | - | Child | L4 |
    | 3 | Mike | - | L5 |
    | | | | |
    +---------+---------+--------------+---------+

    最佳答案

    看起来您正在尝试将贷款“按顺序”分配给家庭表中的行。解决这个问题的方法是首先获取正确的行,然后获取分配给行的贷款。

    正确的行(和前三列)是:

    select f.EmpId, e.Name, f.Relationship
    from family f join
    Employee e
    on f.empid = e.empid;

    请注意,这不会将连字符放入重复值的列中,而是放入实际值。尽管您可以在 SQL 中安排连字符,但这是一个坏主意。 SQL 结果以表格的形式存在,表格是无序集合,每列和每行都有值。当您开始输入连字符时,您取决于顺序。

    现在的问题是加入贷款。这实际上很容易,通过使用 row_number()添加 join key :
    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;

    请注意,这并不能保证给定员工的贷款分配顺序。您的数据似乎没有足够的信息来处理更一致的分配。

    关于sql - 在 SQL Server 中水平连接两个以上的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666788/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com