我想根据有序列表生成累积信息。在下面的示例中,我想根据其他 3 列生成下雨天数。因此,按城镇和日期排序,如果天气干燥,则为 null,如果天气下雨,则为累积分数。
程序上这很容易,但感觉应该有一种方法可以直接用 sql 生成它,我就是想不通。
可能您可以使用分析范围窗口,但我无法弄清楚如何跨 3 列执行此操作。如果有中间行需要重置,这意味着我不能忽略“干”行而只使用 row_number()。
town day weather days of rain
Stevenage 1 dry
Stevenage 2 dry
Stevenage 3 rain 1
Stevenage 4 rain 2
Stevenage 4 rain 3
Stevenage 5 dry
Stevenage 6 dry
Stevenage 8 rain 1
Stevenage 9 rain 2
Stevenage 10 dry
Watford 1 dry
Watford 2 dry
Watford 3 rain 1
Watford 4 rain 2
create table rain_test (town varchar2(20), day number, weather varchar2(10), days_of_rain number);
insert into rain_test(town, day, weather) values ('Stevenage', 1, 'dry');
insert into rain_test(town, day, weather) values ('Stevenage', 2, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 3, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 4, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 5, 'dry');
insert into rain_test(town, day, weather) values ('Stevenage', 6, 'dry');
insert into rain_test(town, day, weather) values ('Stevenage', 7, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 8, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 9, 'rain');
insert into rain_test(town, day, weather) values ('Stevenage', 10, 'dry');
insert into rain_test(town, day, weather) values ('Watford', 1, 'dry');
insert into rain_test(town, day, weather) values ('Watford', 2, 'dry');
insert into rain_test(town, day, weather) values ('Watford', 3, 'rain');
insert into rain_test(town, day, weather) values ('Watford', 4, 'rain');
commit;