我有下表:
ID Key Value
1 From x
1 To Y
1 CC a
2 From Z
2 To X
2 CC b
3 From X
3 To Y
3 CC c
4 From X
4 To Z
4 CC d
我想要Id 1和3作为结果,其中key =“From”和value =“x”,其中key =“To”和value =“Y”(From-> To应该是x-> Y) 它应该是这样的
SELECT Id
FROM table_name
WHERE REGEXP_LIKE(KEY, '(^|\s)from(\s|$)', 'i')
AND REGEXP_LIKE(value, '(^|\s)x(\s|$)', 'i')
AND
WHERE
WHERE REGEXP_LIKE(KEY, '(^|\s)to(\s|$)', 'i')
AND REGEXP_LIKE(value, '(^|\s)y(\s|$)', 'i')
有可能解决这个问题吗?
答案 0 :(得分:2)
使用条件SUM
SELECT ID
FROM Table1
GROUP BY ID
HAVING
SUM(CASE WHEN "Key" = 'From' and LOWER("Value") = 'x' THEN 1 ELSE 0 END) = 1
AND SUM(CASE WHEN "Key" = 'To' and LOWER("Value") = 'y' THEN 1 ELSE 0 END) = 1
<强>输出强>
| ID |
|----|
| 1 |
| 3 |
答案 1 :(得分:0)
将表格连接到自身并进行相应过滤。
SELECT ID
FROM
Table t1
INNER JOIN Table t2
ON t1.ID = t2.ID
WHERE
t1.Key = 'From' AND
t1.Value = 'x' AND
t2.Key = 'To' AND
t2.Value = 'y'