SQL (Hive) fill in missing dates with values from last non empty day

I have a table with 5 columns: date – date of temperature measurement, process_id – identifier of the process for which the temperature is measured, batch_id – identifier of the upload batch, code – upload code, value – temperature value for the process.

hive table

Some dates are missed. It may be the case that several days in a row have been missed.

I need to fill in the missing dates so that each date has a fixed set of rows (all unique combinations of process_id, batch_id, code). For example, for the date 15.01.2021, the following combinnations of process_id, batch_id, code are present (orange color):

records of 2021-01-15

On the date 16.01.2021, only the following process_id values are present:

records of 2021-01-16

In the example above missing values with process_id=18 and process_id=19 should be added:

fillin on 2021-01-16

After adding records by missing processes, I need to fill in the missed values in columns
process_id, batch_id, code, value with the last not null value on previous date by combination process_id, batch_id, code. See the picture above.

Also there may be situations where a date is missing. In the example above, measurements for the date 17.01.2021 are missing.

In this case, missing records for the date 17.01.2021 should be added. A total of 5 records will be added (process_id: 15, 16, 17, 18, 19).

enter image description here

After adding the records for the missing date, the last non-null temperature value for each process from the previous date should be propagated.

My current query:

with cvc as (
    select t.code,
           t.process_id,
           d.next_date,
           t.batch_id
    from (select date_add(from_date, i) as next_date
                 from ( select min(date) as from_date,
                               max(date) as  to_date
                        from measures
                        where year(date)=2021 and month(date)>=1
                        ) p
            lateral view posexplode(split(space(datediff(p.to_date, p.from_date)))) pe as i, x
         ) d 
    cross join (
        select distinct code,
                        process_id,
                       batch_id
        from measures
        where year(date)=2021 and month(date)>=1
    )  c              
    left join (
        select   code,
                 process_id,
                 date,
                 batch_id
        from    measures
        where year(date)=2021 and month(date)>=1
    )   t 
    on t.code=c.code
    and t.process_id=c.process_id
    and t.date=d.next_date
    and t.batch_id=c.batch_id
)
select 
      next_date,
      coalesce(code, last_value(code, true) 
      over(partition by code, process_id, order by next_date 
      rows between unbounded preceding and current row)) as code,
       
      ... 
        ...
     coalesce(process_id, last_value(process_id, true) 
     over(partition by code, process_id, order by next_date 
     rows between unbounded preceding and current row)) as process_id
from cvc
order by
next_date,
code,
process_id,
batch_id

The query presented above correctly fills in the majority of values. However, in some cases, there are duplicates for the same date. For example, for process_id=19 on 2021-01-17, there are two entries with different values (highlighted in red).

enter image description here

The desired output:

enter image description here

This needs to be done in Hive.
Please, help

New contributor

bogdan eleseev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật