我有一张桌子:
RuleID | Code | Value
654 Branch 54
654 MessageType MT103
654 Currency USD
654 SourceSys xyz
<!--Comment-Another set of RuleID data-->
656 Branch 54
656 MessageType MT102
656 Currency INR
--------So ON -------------
<!--Comment-Another set of RuleID data-->
658 Branch 54
658 MessageType MT103
658 Currency INR
每个RuleID最多都有5到6个代码和它们的值。它也可能有1个也是2个。例如,我为规则ID 654和其他656提供了2个设定值。
现在需要一个查询,我可以获得所有RuleID,其中Branch = 54,MessageType = MT102
我在下面尝试过: - (但这显然不起作用)
select RuleID
from MyTable
where (CODE='Branch' and Value='54')
and (CODE='MessageType' and Value='MT102')
阅读联盟,但这也不适用。
认为加入应该在这里,但不能得到结果。
预期结果:我想查询Branch为54且MessageType为MT103的所有RuleID。所以预期结果是Ruleid-654,658
答案 0 :(得分:0)
使用OR
。
SELECT RuleID
FROM MyTable
WHERE (CODE = 'Branch' AND Value = '54')
OR (CODE = 'MessageType' AND Value = 'MT102')
答案 1 :(得分:0)
也许是这样的事情?
SQL> WITH test (ruleid, code, VALUE)
2 AS (SELECT 654, 'Branch', '54' FROM DUAL
3 UNION
4 SELECT 654, 'MessageType', 'MT103' FROM DUAL
5 UNION
6 SELECT 654, 'Currency', 'USD' FROM DUAL
7 UNION
8 SELECT 654, 'SourceSys', 'xyz' FROM DUAL
9 UNION
10 --
11 SELECT 656, 'Branch', '54' FROM DUAL
12 UNION
13 SELECT 656, 'MessageType', 'MT102' FROM DUAL
14 UNION
15 SELECT 656, 'Currency', 'INR' FROM DUAL)
16 SELECT ruleid
17 FROM test
18 WHERE ( ( code = 'Branch'
19 AND VALUE = '54')
20 OR ( code = 'MessageType'
21 AND VALUE = 'MT102'))
22 GROUP BY ruleid
23 HAVING COUNT (*) = 2;
RULEID
----------
656
SQL>
答案 2 :(得分:0)
您可以使用交叉点
WITH MyTable as
(
SELECT 654 RuleId, 'Branch' code, '54' value FROM DUAL
UNION
SELECT 654, 'MessageType', 'MT103' FROM DUAL
UNION
SELECT 654, 'Currency', 'USD' FROM DUAL
UNION
SELECT 654, 'SourceSys', 'xyz' FROM DUAL
UNION
SELECT 656, 'Branch', '54' FROM DUAL
UNION
SELECT 656, 'MessageType', 'MT102' FROM DUAL
UNION
SELECT 656, 'Currency', 'INR' FROM DUAL
)
select RuleID
from MyTable
where CODE='Branch'
and Value='54'
intersect
select RuleID
from MyTable
where CODE='MessageType'
and Value='MT102'