not sure how to add the mapping_no column from the country table to the below query to get the output as expected.
with t as (select ret_code, country_no, day, day - row_number() over (order by ret_code, country_no, day) grp from imputed_country
where ret_code = ( select ret_code from retailer where retailer_name = 'd2' ) ), tst as (select ret_code,
country_no, min(day) day_start, max(day) day_end from t group by ret_code, country_no, grp )
select ret_code, country_no, day_START, day_END, ((day_end - day_start) + 1) as "No of times since last polled"
from tst where day_start <= 2272 and day_end = 2272 order by country_no, ret_code;
Current Query Output:
RET_CODE,country_no ,day_START, day_END,No of times since last polled
---,--------------------,----------,----------,-----------------------------
d2,106 , 2270, 2272, 3
d2,1075 , 2247, 2272, 26
d2,1093 , 2246, 2272, 27
d2,1096 , 2253, 2272, 20
d2,1105 , 2271, 2272, 2
Mapping_No Table
select country_no, mapping_no from country.
country_no mapping_no
106 1000
1075 2000
1093 3000
1105 4000
Expected Output
RET_CODE,country_no,mapping_no ,day_START, day_END,No of days since last polled
---,-----------,---------,----------,----------,-----------------------------
d2,106,1000 , 2270, 2272, 3
d2,1075,2000 , 2247, 2272, 26
d2,1093,3000 , 2246, 2272, 27
d2,1096,4000 , 2253, 2272, 20