久久久久久久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

這是一些模擬數(shù)據(jù):

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.

注意:我已經(jīng)編輯了我的模擬腳本以在結果中包含 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模板網(wǎng)!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 91看片在线观看 | 激情91 | 久产久精国产品 | 99免费在线观看 | 男女羞羞的网站 | 四季久久免费一区二区三区四区 | 人人爽人人草 | 欧美不卡在线 | 亚洲狠狠爱 | 成人久久18免费网站图片 | 欧美极品一区二区 | 成人不卡 | 国产精品夜间视频香蕉 | 国产精品久久久久久久久久软件 | 99精品视频在线 | 国产激情在线 | 男女污网站 | 99热精品在线 | 欧美国产一区二区 | 欧美日韩一区二区在线播放 | 亚洲一区二区三区国产 | 成av人电影在线 | 国产成人综合网 | 国产精品久久久久久久毛片 | 99久久久无码国产精品 | 91亚洲精华国产 | 色资源在线视频 | 国产亚洲欧美在线视频 | 免费观看一级特黄欧美大片 | 国产精品毛片无码 | 99精品久久久久久 | 一色桃子av一区二区 | 中文字幕第一页在线 | 亚洲国产精品va在线看黑人 | 欧美精品v国产精品v日韩精品 | 欧美成人专区 | 久久黄网 | 亚洲风情在线观看 | 欧美一区二区三区视频 | 午夜久久久久 | 日韩欧美一区在线 |