Comparing current column with next column and generating values dynamically

Database
Enthusiast

Comparing current column with next column and generating values dynamically

--Source:
===============
id|MainVal|st_tm|end_tm|subval1|st_tm|end_tm|subval2|st_tm|End_tm|subval3|st_tm|end_tm
01|xxx    |07:30|16:00 |aaa    |12:00|12:30 |bbb    |13:00|14:00 |null   |null |null
02|yyy    |07:30|16:00 |aaa    |13:00|14:30 |null   |null |null  |null   |null |null
03|zzz    |08:00|17:00 |aaa    |08:00|14:30 |ccc    |12:30|13:00 |ddd    |15:00|16:30

--Output:
==============
id|st_tm1|val1|st_tm2|val2|st_tm3|val3|st_tm4|val4|st_tm5|val5|st_tm6|val6|st_tm7|val7
01|07:30 |xxx |12:00 |aaa |12:30 |xxx |13:00 |bbb |14:00 |xxx |16:00 |null|null |null
02|07:30 |yyy |13:00 |aaa |14:30 |yyy |16:00 |null|null  |null|null  |null|null |null
03|08:00 |aaa |12:30 |ccc |13:00 |aaa |14:30 |zzz |15:00 |ddd |16:30 |zzz |17:00|null

Hi All,

i have a scenario like below.

A table has values like main value and sub values with respective start and end time. wherever the gaps in the time for subvalues i have to fill up with main value.

The process happens within the same row by comparing subsequent columns.

Is there any way to achive this in SQL?

1 REPLY
Enthusiast

Re: Comparing current column with next column and generating values dynamically

Hi All,

Any input on this. Is there any way to do it in sql.

Please help to solve this. Thanks in advance .

Thanks