我想获得日期范围之间的差距,并且需要用MM ID来区分差距。
你可以请别人帮我解决下面的NetezzaINPUT:
ID | MM Number | From | To
12345|4444444 |2015-01-03 |2015-02-02
12346|4444444 |2015-02-03 |2015-03-02
<<缺少一条记录(相同的MM号码从2015-03-03到2015-04-02期间没有记录
12347|4444444 |2015-04-03 |2015-05-01
12348|4444444 |2015-05-02 |2015-06-01
12349|4444444 |2015-06-02 |2015-07-01
12310|4444444 |2015-07-02 |2015-07-31
<<缺少一条记录(例如:相同的MM号码从2015-08-01到2015-08-31期间没有记录
12310|4444444|2015-09-01|2015-09-30
预期结果:
MM No | Missing Start Date | Missing To Date
4444444 |2015-03-03 |2015-04-02
4444444 |2015-08-01 |2015-08-31
我有一张包含以下数据的表格
create table icr_tmp
(
ID VARCHAR2(15),
NMI VARCHAR2(50),
INVOICE_START_DATE DATE,
INVOICE_END_DATE DATE);
insert into icr_tmp values('12345','4444444','03/01/2015','02/02/2015');
insert into icr_tmp values('12346','4444444','03/02/2015','02/03/2015');
insert into icr_tmp values('12347','4444444','03/04/2015','01/05/2015');
insert into icr_tmp values('12348','4444444','02/05/2015','01/06/2015');
insert into icr_tmp values('12349','4444444','02/06/2015','01/07/2015');
insert into icr_tmp values('12310','4444444','02/07/2015','31/07/2015');
insert into icr_tmp values('12310','4444444','01/09/2015','30/09/2015');
下面的SQL完全在oracle中工作,但在Netezza中没有。
select *
from (
select nmi,max(invoice_end_date) over(partition by nmi order by invoice_start_date) + 1 start_gap,
lead(invoice_start_date) over(partition by nmi order by invoice_start_date) - 1 end_gap
from icr_tmp3
)
where start_gap <= end_gap;
NMI START_GAP END_GAP
4444444 2015-03-03 2015-04-02
4444444 2015-08-01 2015-08-31
如何更改上述SQL以在Netezza中运行?
答案 0 :(得分:0)
你的查询应该在netezza上运行得很好,只有我看到的东西是子查询的别名,因为如果子查询没有别名,netezza会抱怨。
select *
from (
select nmi
,max(invoice_end_date) over(partition by nmi order by invoice_start_date) + 1 start_gap,
lead(invoice_start_date) over(partition by nmi order by invoice_start_date) - 1 end_gap
from icr_tmp
) foo
where start_gap <= end_gap;