求助!一道面试题的ETL实现,各种数据库实现都可以
本人使用的是DB2 9.7,存储过程使用不熟练啊,请大牛给予帮助!
各种版本实现都行!
类似这样;
create table test.etltable(
name varchar(20) ,
seq integer,
money integer);
create table test.etltarget (
name varchar(20),
min_s integer,
max_s integer,
sum_money integer);
insert into test.etltable values
('A',1,100),
('A',2,200),
('A',3,300),
('A',8,400),
('A',9,500),
('B',1,100),
('B',2,500),
('B',5,600);
目标表结果应该是这样
A 1 3 600
A 8 9 900
B 1 2 600
B 5 5 600
[解决办法]
我改完之后,发现楼主自己写出来的是对的
只是没想到seq-row_num中间的-是减号的意思
哎。。。
[解决办法]
我写了一个,没用统计函数。
见笑了哈!
with tt as (select a.name, a.seq, a.money, case--若存在上连续seq,则得到上连续seq when exists (select 1 from etltable b where b.name = a.name and b.seq = a.seq - 1) then a.seq - 1 else--否则上连续seq为本身 a.seq end as mark1, case--若存在下连续seq,则得到下连续seq when exists (select 1 from etltable b where b.name = a.name and b.seq = a.seq + 1) then a.seq + 1 else--否则下连续seq为本身 a.seq end as mark2 from etltable a)select dd.name, dd.minseq, dd.maxseq,(select sum(c.money) from etltable c where c.name = dd.name and c.seq between dd.minseq and dd.maxseq) from(select name, mark1, min(mark1) as minseq, max(mark2) as maxseq from (select a.* --获取连续数据,且可获取连续数据中最小上连续seq和最大上连续seq from tt a, tt b where a.name = b.name and a.seq <> b.seq and a.mark1 = b.mark1 union all select a.* --获取孤立数据,最小上连续seq和最大上连续seq此时均为seq from tt a where mark1 = mark2) group by name, mark1 order by name, min(seq) ) dd