久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

選擇表中的人并排除妻子但合并他們的名字

Selecting Persons in table and exclude wife but combine their names(選擇表中的人并排除妻子但合并他們的名字)
本文介紹了選擇表中的人并排除妻子但合并他們的名字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一張桌子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

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

So basically, I want to combine the names of the spouse and client, but I want to exclude the spouse:

預期結果:

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

提前感謝您的幫助和時間.

thanks in advance for your help and time.

注意:我已經編輯了我的模擬腳本以在結果中包含 3, NoSpouse Morales, null.此外,對于丈夫/妻子,也不需要特別的標準.列表中最先被提取的人不應包括相關配偶.

NOTE: I've edited my mock script to include 3, NoSpouse Morales, null in the results. Also, there is no particular criteria needed to which is husband/wife. Whoever was fetched first in the list should not include the related spouse.

推薦答案

如果必須包含一個而另一個排除,請嘗試添加子句

If one has to be included while the other excluded, try adding a clause

AND r.PersonID < r.RelatedPersonID 

因為 ID 不相等,這將只包括其中一個:

since the IDs will not be equal and this will include only either one:

 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

這篇關于選擇表中的人并排除妻子但合并他們的名字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲免费视频播放 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 欧美日韩激情 | 欧美日韩国产在线观看 | 日韩欧美一区二区三区免费观看 | 春色av| 国产四区 | 欧美激情欧美激情在线五月 | 久久久久久国产精品 | 国产98色在线 | 日韩 | 久久久久国产精品一区 | 成年人在线视频 | 欧美一区二区三区四区五区无卡码 | 欧美在线观看免费观看视频 | 亚洲视频一区在线观看 | 国产成视频在线观看 | 国产探花在线精品一区二区 | 欧美日本在线观看 | 久久视频一区 | 国产精品高 | 国产a区 | 日本一二区视频 | av福利网站 | 日韩av成人 | 99精品一区 | 伊人一区 | 黄色大片免费网站 | 欧美日韩综合一区 | 国产精品欧美一区二区 | 亚洲一区视频在线 | 亚洲福利在线视频 | 亚洲精品久久久久久久久久吃药 | 日韩在线不卡 | 狠狠综合久久av一区二区小说 | 国产成年人视频 | 欧美精品在线免费 | 我爱操 | 久久精品天堂 | 亚洲午夜精品一区二区三区他趣 | 日韩福利视频 | 伊人中文字幕 |