2017-07-07 23 views
1

鉴于这些表:查找具有相同的确切关系,所有的行,另一个表中提供


表:测试

列:

  • testID INT PK
  • 名称为nvarchar (128)UNIQUE NOT NULL

表:[试验输入]

  • inputsTableName为nvarchar(128)PK UNIQUE
  • testID INT PK FK

临时表:## TestSearchParams

列:

  • inputsTableName为nvarchar(128)UNIQUE NOT NULL

我需要找到一个在试验输入项测试与精确匹配的所有条目在## TestSearchParams inputsTableNames ;得到的测试关系必须与## TestSearchParams中列出的完全一样。

本质上,我只用给定的关系找到测试,不多也不少。我使用LIKE和通配符匹配名称,但是这是一个我认为可以在核心逻辑完成匹配后解决的旁注。

这是我当前的查询:

Select * 
From Tests As B 
Where B.testID In (
         Select ti 
         From (
            Select (
               Select Count(inputsTableName) 
               From [Test-Inputs] 
               Where [Test-Inputs].testID = B.testID 
              ) - Count(Distinct i1) As delta, 
              ti 
            From  (
               Select [Test-Inputs].inputsTableName As i1, 
                 [Test-Inputs].testID As ti 
               From ##TableSearchParams 
               Join [Test-Inputs] 
                On [Test-Inputs].inputsTableName Like ##TableSearchParams.inputsTableName 
                 And B.testID = [Test-Inputs].testID 
              ) As A 
            Group By ti 
          ) As D 
         Where D.delta = 0 
        ); 

目前的问题是,他似乎与匹配任何条目的## TableSearchParams检索测试。在此之前,我尝试了其他几个查询,以获得不同程度的成功。我有查找测试匹配任何参数,所有的参数,并没有任何参数工作查询 - 我只是不能让这个查询工作。

下面是一些示例表值:

测试

  • 1,测试1
  • 2,的Test2
  • 3,Test3的

[试验输入]

  • 表1,1
  • 表2,2
  • 表1,3
  • 表2,3个

TestSearchParams

  • 表1
  • 表2

给定的值应该只返回(3,Test3的)

+2

编辑您的问题,并提供样本数据和所需的结果。 –

+0

好的例子应该发布 – DeceitfulEcho

+0

这里是一个很好的开始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

回答

-1

这里是一个可能的解决方案,通过获取全套TestInputs的用于测试每条记录的作品,左加入到设定的搜索参数,然后汇总结果通过测试,使两个观察:

首先,如果从测试一个记录包括TestInput不是搜索参数中,则该记录必须从结果集中排除。我们可以通过查看是否存在上述左连接未在搜索参数表中产生匹配的情况来检查此问题。其次,如果来自Test的记录满足第一个条件,那么我们知道它没有任何多余的TestInput记录,所以它唯一可能遇到的问题是如果存在不在其TestInput中的搜索参数。如果是这样,那么我们为该测试汇总的记录数将少于搜索参数的总数。

我所做的假设,在这里,您不必重复的TestInputs测试记录,你也不要使用重复的搜索参数。如果这些假设无效,那么这变得更加复杂。但如果是,那么这应该工作:

declare @Tests table (testID int, [name] nvarchar(128)); 
declare @TestInputs table (testID int, inputsTableName nvarchar(128)); 
declare @TestSearchParams table (inputsTableName nvarchar(128)); 

-- Sample data. 
-- 
-- testID 1 has only a subset of the search parameters. 
-- testID 2 matches the search parameters exactly. 
-- testID 3 has a superset of the search parameters. 
-- 
-- Therefore the result set should include testID 2 only. 
insert @Tests values 
    (1, 'Table A'), 
    (2, 'Table B'), 
    (3, 'Table C'); 
insert @TestInputs values 
    (1, 'X'), 
    (2, 'X'), 
    (2, 'Y'), 
    (3, 'X'), 
    (3, 'Y'), 
    (3, 'Z'); 
insert @TestSearchParams values 
    ('X'), 
    ('Y'); 

declare @ParamCount int; 
select @ParamCount = count(1) from @TestSearchParams; 

select 
    Tests.testID, 
    Tests.[name] 
from 
    @Tests Tests 
    inner join @TestInputs Inputs on Tests.testID = Inputs.testID 
    left join @TestSearchParams Search on Inputs.inputsTableName = Search.inputsTableName 
group by 
    Tests.testID, 
    Tests.[name] 
having 
    -- If a group includes any record where Search.inputsTableName is null, it means that 
    -- the record in Tests has a TestInput that is not among the search parameters. 
    sum(case when Search.inputsTableName is null then 1 else 0 end) = 0 and 

    -- If a group includes fewer records than there are search parameters, it means that 
    -- there exists some parameter that was not found among the Tests record's TestInputs. 
    count(1) = @ParamCount; 
+0

这工作完全(有位摆弄得到它,以适应我的数据库和项目的全部范围)。非常感谢!我没有意识到你可以在where子句中使用诸如case语句之类的东西 - 使生活变得更容易。 – DeceitfulEcho

相关问题