sql - 选择表中的人员并排除妻子,但合并他们的名字

标签 sql sql-server tsql

我有一张 table :

PersonID | FirstName | LastName
-------------------------------
1        |   John    |  Doe
2        |   Jane    |  Doe
3        |  NoSpouse | Morales
4        | Jonathan  | Brand
5        | Shiela    | Wife

还有一个关系表:

RelationshipID | PersonID | Type | RelatedPersonID
1              |    1     |  3   |     2
2              |    2     |  3   |     1
3              |    4     |  3   |     5
4              |    5     |  3   |     4

所以基本上,我想结合配偶和客户的名字,但我想排除配偶:

预期结果:

1,  John and Jane Doe, 2
----------------------
3, NoSpouse Morales, null
-----------------------
4, Jonathan and Shiela Brand, 5

我已经尝试过:

SELECT p.PersonID,
    Case when spouse.PersonID is not null 
        THEN p.FirstName + ' and ' + spouse.FirstName + ' ' + p.LastName
    ELSE p.FirstName + ' ' + p.LastName END as ClientName,
    spouse.PersonID as RelatedPersonID
FROM Person p
LEFT JOIN Relationship r on p.PersonID = r.PersonID
LEFT JOIN Person spouse on r.RelatedPersonID = spouse.PersonID
WHERE r.Type = 3 OR spouse.PersonID is null

但结果是:

1,  John and Jane Doe, 2
----------------------
2,  Jane and John Doe, 1
----------------------
3, NoSpouse Morales, null
-----------------------
4, Jonathan and Shiela Brand, 5
-------------------------------
5, Shiela and Jonathan Wife, 4

这是一些模拟数据:

create table Person(
    PersonID int primary key,
    FirstName varchar(max),
    LastName varchar(max)
)
insert into Person values 
(1, 'John', 'Doe'), 
(2, 'Jane', 'Doe'), 
(3, 'NoSpouse', 'Morales'), 
(4, 'Jonathan', 'Brand'), 
(5,'Shiela','Wife')

create table Relationship (
    RelationshipID int,
    PersonID int references Person(PersonID),
    Type int,
    RelatedPersonID int references Person(PersonID)
)
insert into Relationship values 
(1, 1, 3, 2),
(2, 2, 3, 1),
(3, 4, 3, 5),
(4, 5, 3, 4)

SELECT p.PersonID,
    Case when spouse.PersonID is not null 
        THEN p.FirstName + ' and ' + spouse.FirstName + ' ' + p.LastName
    ELSE p.FirstName + ' ' + p.LastName END as ClientName,
    spouse.PersonID as RelatedPersonID
FROM Person p
LEFT JOIN Relationship r on p.PersonID = r.PersonID
LEFT JOIN Person spouse on r.RelatedPersonID = spouse.PersonID
WHERE r.Type = 3 OR spouse.PersonID is null

drop table Relationship
drop table Person

提前感谢您的帮助和时间。

注意: 我编辑了模拟脚本,在结果中包含 3, NoSpouse Morales, null 。此外,对于丈夫/妻子没有特定的标准。列表中第一个提取的人不应包括相关配偶。

最佳答案

如果必须包含其中一个而排除另一个,请尝试添加一个子句

AND r.PersonID < r.RelatedPersonID 

因为 ID 不相等,并且仅包含其中一个:

 SELECT p.PersonID,
 Case when spouse.PersonID is not null 
    THEN p.FirstName + ' and ' + spouse.FirstName + ' ' + p.LastName
 ELSE p.FirstName + ' ' + p.LastName END as ClientName,
 spouse.PersonID as RelatedPersonID
FROM Person p
LEFT JOIN Relationship r on p.PersonID = r.PersonID
LEFT JOIN Person spouse on r.RelatedPersonID = spouse.PersonID
WHERE (r.Type = 3 AND r.PersonID < r.RelatedPersonID)  OR spouse.PersonID is null

关于sql - 选择表中的人员并排除妻子,但合并他们的名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43321150/

相关文章:

mysql - 如何使用 UNION 选择所有字段(包括 NULL)?

c# - 将sql转换为linq

sql-server - 从数百个存储过程中解析出完整的动态SQL表达式

sql - 不将查询存储为字符串的动态数据透视查询

sql - SELECT DISTINCT 的第一行

sql - 仅获取当前行与下一行之间的时间差小于 5 分钟的行

sql-server - 如何在SQL Server 2012中部署现有的SSIS包?

sql - 子查询返回多个值,但我使用 "IN"作为我的运算符

tsql - SQL Server Management Studio 的最佳 T/SQL 格式化插件

c# - 从 C# 确定 SQL Server 数据库列的默认值