在表格中选择人员并排除妻子,但要结合他们的名字

时间:2017-04-10 10:46:12

标签: sql sql-server tsql

我有一张表Person

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

Relationship表:

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。此外,没有特别标准需要丈夫/妻子。列表中首先被提取的人不应包括相关配偶。

4 个答案:

答案 0 :(得分:14)

如果必须包括一个而另一个被排除在外,请尝试添加一个子句

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

答案 1 :(得分:3)

这可能有效

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 and r.Type = 3
LEFT JOIN Person spouse on r.RelatedPersonID = spouse.PersonID
WHERE  coalesce(r.PersonID, 1) < coalesce(r.RelatedPersonID, 2)

这样您只考虑PersonID低于RelatedPersonID的关系行,或者根本不考虑Relationships表的行(因为1 < 2总是如此)

答案 2 :(得分:0)

如果我理解正确,您可以删除WHERE子句中的配偶:

SELECT PersonId, ClientName, RelatedPersonID
FROM (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,
             spouse.LastName as spouse_lastname
      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 
     ) prs
WHERE spouse_LastName <> 'Wife';

答案 3 :(得分:0)

试试这个

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 AND r.Type = 3 
LEFT JOIN Person spouse on r.RelatedPersonID = spouse.PersonID
WHERE r.RelationshipID IS NULL OR  r.RelationshipID < r.RelatedPersonID

drop table Relationship
drop table Person

演示链接:Rextester