如何在同一个表中进行比较(SQL Server)

问题描述 投票:-2回答:1

这是一个简单的问题,但我似乎无法弄明白。我的模型中有两个表:

Citizens (PK: CitizenID; Name; City) 
Interactions (PK: InteractionID; Role; ProcessNm; FK: CitizenID)

我想要的是获得参与同一过程的法院的证人和被告,他们来自同一个城市。

注意:

我在交互表中有更多“角色”,比如“原告”。因此,我想确保参与同一过程的被告城市和证人城市之间的比较。例如,对于在A市生活有“原告”的过程,居住在A市的“被告”和居住在B市的“证人”。本案不应输出任何内容。

.

参与者表

╔═══════════╦══════════════╦══════════╗
║ CitizenID ║  Name        ║ CityName ║
╠═══════════╬══════════════╬══════════╣
║  1        ║ Jeff Atwood  ║ miwaki   ║
║  2        ║ Geoff Dalgas ║ jeffer   ║
║  3        ║ Jarrod Dixon ║ miwaki   ║
║  4        ║ Joel Spolsky ║ jeffer   ║
║  5        ║ Karl Sapolsk ║ jeffer   ║
╚═══════════╩══════════════╩══════════╝

交互表

╔═══════════════╦══════════════╦══════════╦════════════╗
║ InteractionID ║  Role        ║ Process  ║ CitizenID  ║
╠═══════════════╬══════════════╬══════════╬════════════╣
║  1            ║ defendant    ║  1       ║  1         ║
║  2            ║ witness      ║  1       ║  2         ║
║  3            ║ defendant    ║  1       ║  3         ║
║  4            ║ defendant    ║  1       ║  4         ║
║  5            ║ defendant    ║  1       ║  5         ║
║  6            ║ witness      ║  2       ║  1         ║
║  7            ║ witness      ║  2       ║  3         ║
╚═══════════════╩══════════════╩══════════╩════════════╝

结果:

╔═══════════════╦══════════════╦══════════╦══════════╗
║   Name        ║  Role        ║ Process  ║ City     ║
╠═══════════════╬══════════════╬══════════╬══════════╣
║  Geoff Dalgas ║ witness      ║  1       ║  jeffer  ║
║  Joel Spolsky ║ defendant    ║  1       ║  jeffer  ║
║  Karl Sapolsk ║ defendant    ║  1       ║  jeffer  ║
╚═══════════════╩══════════════╩══════════╩══════════╝
sql sql-server
1个回答
1
投票

我认为这应该是你正在寻找的:

SELECT
    p.Name
    , i.Role
    , i.Process
    , p.CityName
FROM Participants p
    INNER JOIN Interactions i
        ON p.citizenID = i.citizenID
WHERE EXISTS (SELECT 1 
              FROM Participants p2
                  INNER JOIN Interactions i2
                      ON p2.citizenID = i2.citizenID                
              WHERE p.citizenID <> i2.citizenID
                  AND i.process = i2.process
                  AND p.cityname = p2.cityName
                  AND i2.role <> 'accuser'
                  )
    AND i.role <> 'accuser'

另外,这是测试它的完整代码:

declare @participants table (citizenid int, name nvarchar(100), cityname nvarchar(100))
declare @interactions table (id int identity(1,1), role nvarchar(100), process int, citizenid int)

insert @participants values (1, 'Jeff', 'miwaki')
    , (2, 'Geoff', 'jeffer')
    , (3, 'Jarrod', 'miwaki')
    , (4, 'Joel', 'jeffer')
    , (5, 'Karl', 'jeffer')

insert @interactions (role, process, citizenid) values ('defendant', 1, 1)
    , ('witness', 1, 2)
    , ('defendant', 1, 3)
    , ('defendant', 1, 4)
    , ('defendant', 1, 5)
    , ('witness', 2, 1)
    , ('witness', 2, 3)

SELECT
    p.Name
    , i.Role
    , i.Process
    , p.CityName
FROM @Participants p
    INNER JOIN @Interactions i
        ON p.citizenID = i.citizenID
WHERE EXISTS (SELECT 1 
              FROM @Participants p2
                  INNER JOIN @Interactions i2
                      ON p2.citizenID = i2.citizenID                
              WHERE p.citizenID <> i2.citizenID
                  AND i.process = i2.process
                  AND p.cityname = p2.cityName
                  AND i.role <> i2.role
                  AND i2.role <> 'accuser'
                  )
    AND i.role <> 'accuser'
© www.soinside.com 2019 - 2024. All rights reserved.