I need a query on below table
TABLE
ClienName Bal0 Bal1 Bal2 Bal3 Bal4 Bal5 Bal6
--------------------------------------------------------------------------------
A 600 600 NULL NULL NULL NULL NULL
B Null 100 NULL NULL NULL NULL NULL
C NULL NULL 200 NULL NULL NULL NULL
D NULL NULL NULL NULL NULL 130 130
E 510 510 NULL 510 510 NULL NULL
F 170 170 NULL 170 170 NULL 170
G 210 210 NULL 210 210 210 210
H 20 20 NULL 20 NULL NULL NULL
Result: I would like to see the above TABLE as below, which query can do it? please help
ClienName Bal0 Bal1 Bal2 Bal3 Bal4 Bal5 Bal6
--------------------------------------------------------------------------------
A 600 600 600 600 600 600 600
B 100 100 100 100 100 100 100
C 200 200 200 200 200 200 200
D 130 130 130 130 130 130 130
E 510 510 510 510 510 510 510
F 170 170 170 170 170 170 170
G 210 210 210 210 210 210 210
H 20 20 20 20 20 20 20
Assuming I understand what you want from your very limited example set, just go through the table and fill it column by column.
One pass in the forward direction:
And one pass in the reverse:
This also (assuming “Bal” means balance, and the number has something to do with time) handles the case of differing values in columns as time increases.
If instead you just want the first non-null column, try the
COALESCEoperator: