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

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

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

問題描述

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

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

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

預(yù)期結(jié)果:

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

但結(jié)果是:

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)編輯了我的模擬腳本以在結(jié)果中包含 3, NoSpouse Morales, null.此外,對于丈夫/妻子,也不需要特別的標(biāo)準(zhǔn).列表中最先被提取的人不應(yīng)包括相關(guān)配偶.

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

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標(biāo)記轉(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)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 欧美成人二区 | 欧美日韩精品免费 | 亚洲精品一 | 欧美天堂| 美女视频.| 欧美啪啪 | aaaaa毛片 | 成人在线观看免费爱爱 | 一区二区三区不卡视频 | 91在线视频网址 | 激情视频中文字幕 | 国产视频一区在线 | 精品无码久久久久久国产 | 美女视频一区 | 精品欧美一区二区精品久久久 | 99精品久久久国产一区二区三 | 欧美激情精品久久久久久变态 | av中文字幕网站 | 欧美日韩亚洲国产综合 | 久久精品欧美视频 | 国产精品久久久久久福利一牛影视 | 日日骚av | 欧美高清性xxxxhd | 中文字幕亚洲区一区二 | 色综合天天综合网国产成人网 | 久久久久一区二区三区 | 中文字幕一区二区三区在线观看 | 日产精品久久久一区二区福利 | 久久精品一区二区三区四区 | 色性av | 日韩av在线中文字幕 | 亚洲欧美日韩中文在线 | 日韩免费网站 | 国产成人99久久亚洲综合精品 | 免费看爱爱视频 | 美女天堂在线 | 国产婷婷色一区二区三区 | 久久精品一级 | 二区三区在线观看 | 欧美综合视频在线 | 亚洲精品乱码久久久久久按摩观 |