我正在尝试在表上执行聚合。但在某些情况下,它没有正确聚合。请找到以下输入。
表t1。
CHANNEL;VALUE;STATUS;ERROR_CODE;RND_TIMESTAMP;SESSION_CD;NAR;
-------------------------------------------------------------
USD;4;12;;2-NOV-2015 11:00:00;;
USD;4;12;;2-NOV-2015 11:00:00;;
USD;2;12;;2-NOV-2015 11:00:00;;
USD;3;12;;2-NOV-2015 11:00:00;;
输出表t2
CHANNEL;VALUE;STATUS;ERROR_CODE;HOUR_TIMESTAMP;SESSION_CD;NAR;
--------------------------------------------------------------
USD;5;12;;2-NOV-2015 11:00:00;;
查询:
select
channel, sum(value),
status, error_code, rnd_timestamp, session_cd, nar
from
t1
where
rnd_timestamp > (select max(hour_timestamp) from t2)
group by
channel, status, error_code, rnd_timestamp, session_cd, nar
为什么不考虑其他2行进行聚合。是因为group by中的某些列有null吗?如何解决这个问题?
输出必须是:
USD;13;12;;2-NOV-2015 11:00:00;;
答案 0 :(得分:0)
为什么您认为您的查询存在问题?
通过将t2中的hour_timestamp切换为上午10点而不是上午11点,您的查询按预期工作:
with t1 as (select 'USD' channel, 4 value, 12 status, null error_code, to_date('02/11/2015 11:00:00', 'dd/mm/yyyy hh24:mi:ss') rnd_timestamp, null session_cd, null nar from dual union all
select 'USD' channel, 4 value, 12 status, null error_code, to_date('02/11/2015 11:00:00', 'dd/mm/yyyy hh24:mi:ss') rnd_timestamp, null session_cd, null nar from dual union all
select 'USD' channel, 2 value, 12 status, null error_code, to_date('02/11/2015 11:00:00', 'dd/mm/yyyy hh24:mi:ss') rnd_timestamp, null session_cd, null nar from dual union all
select 'USD' channel, 3 value, 12 status, null error_code, to_date('02/11/2015 11:00:00', 'dd/mm/yyyy hh24:mi:ss') rnd_timestamp, null session_cd, null nar from dual),
t2 as (select 'USD' channel, 5 value, 12 status, null error_code, to_date('02/11/2015 10:00:00', 'dd/mm/yyyy hh24:mi:ss') hour_timestamp, null session_cd, null nar from dual)
--- end of mimicking your tables t1 and t2 with data in; see SQL below:
select channel,
sum(value),
status,
error_code,
rnd_timestamp,
session_cd,
nar
from t1
where rnd_timestamp > (select max(hour_timestamp) from t2)
group by channel,
status,
error_code,
rnd_timestamp,
session_cd,
nar;
CHANNEL SUM(VALUE) STATUS ERROR_CODE RND_TIMESTAMP SESSION_CD NAR
------- ---------- ---------- ---------- --------------------- ---------- ---
USD 13 12 02/11/2015 11:00:00