我正在尝试加入两个查询,其中唯一的区别在于子句 RECIP like '44%' ==> RECIP not like '44%'
并具有唯一的输出
select FIELD1, FIELD2, FIELD3-PART, sum(c) from
( select FIELD1, FIELD2, SUBSTR(FIELD3, 1, 2) as FIELD3-PART, count(*) as c from
table
where timestamp between sysdate - interval '120' minute and sysdate - interval '2' minute
FIELD3 is not null and FIELD3 like '44%'
group by FIELD1, FIELD2, FIELD3)
group by FIELD1, FIELD2, FIELD3-PART
HAVING ((sum(c) > '150' and SUBSTR(ORIG, 1,6)='333441')
or (sum(c) > '100' and SUBSTR(FIELD2, 1,6)='333442')
or (sum(c) > '250' and SUBSTR(FIELD2, 1,6)='333443')
or (sum(c) > '10' and SUBSTR(FIELD2, 1,6)='333444'));
select FIELD1, FIELD2, FIELD3-PART, sum(c) from
( select FIELD1, FIELD2, SUBSTR(FIELD3, 1, 2) as FIELD3-PART, count(*) as c from
table
where timestamp between sysdate - interval '120' minute and sysdate - interval '2' minute
FIELD3 is not null and FIELD3 not like '44%'
group by FIELD1, FIELD2, FIELD3)
group by FIELD1, FIELD2, FIELD3-PART
HAVING ((sum(c) > '150' and SUBSTR(ORIG, 1,6)='333441')
or (sum(c) > '100' and SUBSTR(FIELD2, 1,6)='333442')
or (sum(c) > '250' and SUBSTR(FIELD2, 1,6)='333443')
or (sum(c) > '10' and SUBSTR(FIELD2, 1,6)='333444'));
我知道肯定有更“优雅”的方式来执行查询并获得我需要的输出,但我不是 sql 专家,实际上这两个查询工作得很好。 谢谢 卢卡斯
答案 0 :(得分:0)
您可以将查询更新为 -
select FIELD1,
FIELD2,
FIELD3-PART,
sum(c)
from (select FIELD1,
FIELD2,
SUBSTR(FIELD3, 1, 2) as FIELD3-PART,
count(*) as c
from table
where timestamp between sysdate - interval '120' minute and sysdate - interval '2' minute
-- AND FIELD3 is not null -- No need to use this line as LIKE predicate automatically filters null values.
and FIELD3 like '44%'
group by FIELD1, FIELD2, SUBSTR(FIELD3, 1, 2))
group by FIELD1, FIELD2, FIELD3-PART
HAVING sum(c) > '50'
and SUBSTR(ORIG, 1,6) IN ('333441', '333442', '333443', '333444');
我仍然怀疑您的查询是否有效,因为没有任何东西叫做 ORIG(在 HAVING 子句中使用),我可以在您的查询中看到。