本帖最后由 IT渔夫 于 2016-3-1 09:44 编辑
eq_guidcycle_timecurrent_valuelately_value
A2016年2月29日10090
A2016年3月1日100
A2016年3月2日
A2016年3月3日
A2016年3月4日
A2016年3月5日
A2016年3月6日
A2016年3月7日150
A2016年3月8日152150
A2016年3月9日160152
A2016年3月10日160
A2016年3月11日
B2016年2月29日8785
B2016年3月1日9387
B2016年3月2日9593
B2016年3月3日95
B2016年3月4日
B2016年3月5日
B2016年3月6日
B2016年3月7日105
B2016年3月8日108105
B2016年3月9日117108
B2016年3月10日117
B2016年3月11日
C2016年2月29日
C2016年3月1日
C2016年3月2日
C2016年3月3日100
C2016年3月4日
C2016年3月5日
C2016年3月6日106
C2016年3月7日108106
C2016年3月8日108
C2016年3月9日
C2016年3月10日
C2016年3月11日
以上表中,数据量有上千万。我需要根据eq_guid取最大cycle_time的,前提是同一eq_guid下,current_value和lately_value不为空,且之前的cycle_time下的current_value和lately_value都不为空
以上数据中,A取的数据是2016年2月29日,B取的是2016年3月2日(B的3月8日和3月9日的的current_value和lately_value都不为空,但是3月3日、3月4日等的current_value和lately_value为空,因此只能取到3月2日,B的3月2日之前的current_value和lately_value都是不为空),C的话,取的是空(C的3月7日的current_value和lately_value不为空,但是之前2月29日、3月1日等都为空,因此C取不到任何数据)
with t as
(
select 'A' eq_guid, to_date('2016-2-29','yyyy-mm-dd') cycle_time, 100 current_value, 90 lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-1' ,'yyyy-mm-dd') cycle_time, null current_value, 100 lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-4' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-5' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-6' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-7' ,'yyyy-mm-dd') cycle_time, 150 current_value, null lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-8' ,'yyyy-mm-dd') cycle_time, 152 current_value, 150 lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-9' ,'yyyy-mm-dd') cycle_time, 160 current_value, 152 lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-10','yyyy-mm-dd') cycle_time, null current_value, 160 lately_value from dual union all
select 'A' eq_guid, to_date('2016-3-11','yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'B' eq_guid, to_date('2016-2-29','yyyy-mm-dd') cycle_time, 87 current_value, 85 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-1' ,'yyyy-mm-dd') cycle_time, 93 current_value, 87 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-2' ,'yyyy-mm-dd') cycle_time, 95 current_value, 93 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-3' ,'yyyy-mm-dd') cycle_time, null current_value, 95 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-8' ,'yyyy-mm-dd') cycle_time, 108 current_value, 105 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-9' ,'yyyy-mm-dd') cycle_time, 117 current_value, 108 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-10','yyyy-mm-dd') cycle_time, null current_value, 117 lately_value from dual union all
select 'B' eq_guid, to_date('2016-3-11','yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-2-29','yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-1' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-2' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-3' ,'yyyy-mm-dd') cycle_time, 100 current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-4' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-5' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-6' ,'yyyy-mm-dd') cycle_time, 106 current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-7' ,'yyyy-mm-dd') cycle_time, 108 current_value, 106 lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-8' ,'yyyy-mm-dd') cycle_time, null current_value, 108 lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-9' ,'yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-10','yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual union all
select 'C' eq_guid, to_date('2016-3-11','yyyy-mm-dd') cycle_time, null current_value, null lately_value from dual
)
select * from t;以上示例数据中,A取到2月29日那一条,B的结果是3月2日那一条,C无法取到任何数据。