如何选择查询中特定行符合特定条件的所有行?

时间:2017-03-03 16:33:17

标签: sql

我有一个查询来访问三个表中的数据:

this

表格如下:

SELECT fg.factGroupName, mc.CI, mt.Config
                    FROM MetricTypes mt
                    INNER JOIN MetricCollection mc ON mt.TypeId = mc.MetricType
                    INNER JOIN factGroup fg ON fg.factGroupId = mc.Factgroup
                    WHERE mt.ToolName = 2 AND mt.TypeName = 'inputs' AND mt.Deploy = 'Y' AND mc.Deploy = 'Y';

查询的输出如下所示:

factGroup                     
|-------------|--------------|
|factGroupId  |factGroupName |
|-------------|--------------|
|      20     | test_servers |
|      21     | prod_servers |
|-------------|--------------|

MetricTypes
|-------------|--------------|--------------|------------|-----------|
|TypeId       |TypeName      |ToolName      |Config      |Deploy     |
|-------------|--------------|--------------|------------|-----------|
|     10      |  inputs      |     2        | foo        |   Y       |
|     11      |  inputs      |     2        | bar        |           |
|     12      |  outputs     |     4        | giggle     |           |
|     13      |  inbetween   |     6        | biz        |           |
|-------------|--------------|--------------|------------|-----------|

MetricCollection
|-------------|--------------|--------------|------------|-----------|
|MetricId     |Factgroup     |MetricType    |CI          |Deploy     |
|-------------|--------------|--------------|------------|-----------|
|    1        |     20       |     10       |  alpha     |   Y       |
|    2        |     20       |     11       |  beta      |           |
|    3        |     20       |     12       |  gamma     |           |
|    4        |     21       |     13       |  theta     |           |
|-------------|--------------|--------------|------------|-----------|

我如何调整查询以获得反映给定|-------------|--------------|--------------| |factGroupName| CI | Config | |-------------|--------------|--------------| | test_servers| alpha | foo | |-------------|--------------|--------------| 的所有CIConfig字段的结果,而不仅仅是{{1} factGroupName字段中的标志?换句话说,我想让我的输出看起来像这样:

Y

澄清:我希望保持条件以检查两个Deploy字段中是否存在“Y”标志,因为与此查询关联的脚本使用该标志作为触发器来执行其他工作。

1 个答案:

答案 0 :(得分:2)

INNER JOIN Deploy = 'Y'似乎是问题所在。

表示例:

create table #factgroup
(
factgroupid int,
factgroupname varchar(20)
)

create table #metrictypes
(
typeid int,
typename varchar(10),
toolname int,
config varchar(10),
deploy varchar(1)
)

create table #metriccollection
(
metricid int,
factgroup int,
metrictype int,
ci varchar(10),
deploy varchar(1)
)

insert into #factgroup values (20, 'test_servers')
insert into #metrictypes values (10, 'inputs', 2, 'foo', 'Y')
insert into #metrictypes (typeid, typename, toolname, config) values (11, 'inputs', 2, 'bar')
insert into #metriccollection values (1, 20, 10, 'alpha', 'Y')
insert into #metriccollection (metricid, factgroup, metrictype, ci) values (2, 20, 11, 'beta')

尝试此操作(删除了主题标签):

SELECT fg.factGroupName, mc.CI, mt.Config

FROM #MetricTypes mt
INNER JOIN #MetricCollection mc ON mt.TypeId = mc.MetricType
INNER JOIN #factGroup fg ON fg.factGroupId = mc.Factgroup

WHERE mt.ToolName = 2 AND mt.TypeName = 'inputs'

and mt.toolname in (
                      select distinct mt.ToolName
                      from #metrictypes mt
                      where mt.deploy = 'y'
                   )

and mc.factgroup in (
                      select distinct mc.factgroup
                      from #metriccollection mc
                      where mc.deploy = 'y'
                    )