SQL Server:按列过滤查询表

时间:2017-04-04 20:48:42

标签: sql-server

我有一个我想要查询的表。它有两个UNIQUE Ids字段。对于这个例子,我将使用PersonA,PersonB,PersonC,PersonD。该表代表两个人之间的关系。

人际关系表:

Row     FieldId_01  FieldId_02 
------------------------------
1       PersonA     PersonB
2       PersonA     PersonC
3       PersonB     PersonA
4       PersonC     PersonA
5       PersonD     PersonA

Person表:

PersonID
---------
PersonA
PersonB
PersonC
PersonD

我不关心订单,我只需要参考PersonA的独特组合。因此,第1行和第3行相同,第2行和第4行相同,第5行没有匹配,但仍然是唯一的组合。

我需要选择一个独特的组合。

预期输出应为

人际关系表

Row     FieldId_01  FieldId_02 
-------------------------------
1       PersonA     PersonB
2       PersonA     PersonC
5       PersonD     PersonA

2 个答案:

答案 0 :(得分:0)

这将满足您的需求。我将样本数据包含在临时表中,以便您可以立即对其进行测试:

CREATE TABLE #T (id int not null PRIMARY KEY, companyName varchar(16) not null)

INSERT INTO #t Values 
(1,       'dogs ltd'),
(2,       'cats ltd'),
(3,       'pigs ltd'),
(4,       'pigs ltd'),
(5,       'cats ltd'),
(6,       'cats ltd'),
(7,       'dogs ltd'),
(8,       'pigs ltd')

SELECT id, CompanyName
FROM (
    SELECT *,
        LEAD(CompanyName, 1) OVER(ORDER BY id) as nc,
        LAG(CompanyName, 1) OVER(ORDER BY id) AS pc
    FROM #t t
    ) x
WHERE nc = companyName
    OR pc = companyName

我的输出:

id  CompanyName
3   pigs ltd
4   pigs ltd
5   cats ltd
6   cats ltd

答案 1 :(得分:0)

使用"dependencies": { "Microsoft.AspNetCore.Authentication.Cookies": "1.1.1", "Microsoft.AspNetCore.Diagnostics": "1.1.1", "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.1", "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.1", "Microsoft.AspNetCore.Mvc": "1.1.2", "Microsoft.AspNetCore.Routing": "1.1.1", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.1", "Microsoft.AspNetCore.Server.Kestrel": "1.1.1", "Microsoft.AspNetCore.StaticFiles": "1.1.1", "Microsoft.EntityFrameworkCore.SqlServer": "1.1.1", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.1", "Microsoft.EntityFrameworkCore.Design": "1.1.1", "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.1", "Microsoft.Extensions.Configuration.Json": "1.1.1", "Microsoft.Extensions.Configuration.UserSecrets": "1.1.1", "Microsoft.Extensions.Logging": "1.1.1", "Microsoft.Extensions.Logging.Console": "1.1.1", "Microsoft.Extensions.Logging.Debug": "1.1.1", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.1", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final", "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.0", "Microsoft.NETCore.App": "1.1.1"}, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true}, "frameworks": { "netcoreapp1.1": { "imports": [ "dotnet5.6", "portable-net45+win8"]}}, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.1.0-preview4-final", "imports": [ "portable-net45+win8" ] }, "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"}

如果not exists()永远不会与同一行中的FieldId_01相同,那么:

FieldId_02

,否则

select *
from t
where not exists (
  select 1
  from t as i
  where i.Row < t.Row
    and i.FieldId_01 in (t.FieldId_01,t.FieldId_02)
    and i.FieldId_02 in (t.FieldId_01,t.FieldId_02)
  )

rextester演示:http://rextester.com/SQL61250

都返回:

select *
from t
where not exists (
  select 1
  from t as i
  where i.Row < t.Row
    and ( (i.FieldId_01 = t.FieldId_01 and i.FieldId_02 = t.FieldId_02)
      or  (i.FieldId_02 = t.FieldId_01 and i.FieldId_01 = t.FieldId_02)
       )
  )