SQL - 如何返回具有公共列值的行以及其他条件?

时间:2016-01-07 08:59:36

标签: sql sql-server

我的表格如下所示 -

╔═════════╦════════════════════════╦════════════════╗
║ QueueID ║  AttributeName         ║ AttributeValue ║
║ 123     ║      Domain            ║  Azure         ║
║ 123     ║      Area              ║  EMEA          ║
║ 123     ║      ContractType      ║  Contract1     ║
║ 123     ║      RequestType       ║  Workshop      ║
║ 124     ║      Domain            ║  .NET          ║
║ 124     ║      Area              ║  Asia-Pacific  ║
║ 124     ║      ContractType      ║  Contract2     ║
║ 124     ║      RequestType       ║  Critical      ║
╚═════════╩════════════════════════╩════════════════╝

我想找到以下组合的QueueID

Domain = .NET; Timezone = Asia-Pacific; ContractType = Contract2; RequestType = Critical

基本上,我想找到指定组合的QueueId,在上面的示例中将是124

4 个答案:

答案 0 :(得分:2)

我没有测试过这个,但请试一试:

SELECT QueueID
FROM MyTable
WHERE  ( AttributeName = 'Domain' AND AttributeValue = '.NET' )
        OR ( AttributeName = 'Area' AND AttributeValue = 'Asia-Pacific' )
        OR ( AttributeName = 'ContractType' AND AttributeValue = 'Contract2' )
        OR ( AttributeName = 'RequestType' AND AttributeValue = 'Critical' ) 
GROUP BY QueueID
HAVING Count(*) = 4

答案 1 :(得分:1)

SELECT queueid
FROM   yourtable
WHERE AttributeName = 'Domain' AND AttributeValue = '.NET' 

intersect

SELECT queueid
FROM   yourtable
WHERE  AttributeName = 'Area'
             AND AttributeValue = 'Asia-Pacific'
intersect

SELECT queueid
FROM   yourtable
WHERE AttributeName = 'ContractType'
             AND AttributeValue = 'Contract2' 
intersect

SELECT queueid
FROM   yourtable
WHERE AttributeName = 'RequestType'
             AND AttributeValue = 'Critical' 

试试这个。

答案 2 :(得分:0)

试试这个

SELECT *
FROM   yourtable
WHERE  ( AttributeName = 'Domain'
         AND AttributeValue = '.NET' )
        OR ( AttributeName = 'Area'
             AND AttributeValue = 'Asia-Pacific' )
        OR ( AttributeName = 'ContractType'
             AND AttributeValue = 'Contract2' )
        OR ( AttributeName = 'RequestType'
             AND AttributeValue = 'Critical' ) 

答案 3 :(得分:0)

此解决方案效率低,但结构紧凑,可以在一行中处理任意数量的参数。

准备数据:

Declare @TestTab TABLE (
    QueueID INT,
    AttributeName VARCHAR(100),
    AttributeValue VARCHAR(100)
);

INSERT INTO @TestTab
    (QueueID, AttributeName, AttributeValue)
VALUES
    (123, 'Domain'      , 'Azure'        ),
    (123, 'Area'        , 'EMEA'         ),
    (123, 'ContractType', 'Contract1'    ),
    (123, 'RequestType' , 'Workshop'     ),
    (124, 'Domain'      , '.NET'         ),
    (124, 'Area'        , 'Asia-Pacific' ),
    (124, 'ContractType', 'Contract2'    ),
    (124, 'RequestType' , 'Critical'     );

Declare @SearchString VARCHAR(MAX) = '; ' + 'Domain = .NET; Area = Asia-Pacific; ContractType = Contract2; RequestType = Critical' + ';';

运行查询:

Select QueueID
From @TestTab
Where @SearchString Like '%; ' + AttributeName + ' = ' + AttributeValue + ';%'
Group By QueueID
Having Count(Distinct AttributeName) = Len(@SearchString) - Len(Replace(@SearchString, '=', ''))