Skip to content
Advertisement

MS SQL : Need to get row wise value from cell for similar rows, Code not working correctly

I have 100s of excel sheets(CSV files) I need to use for the historical load. I am going to load all the CSVs to a stage table using ETL.

I am trying to get the row-wise value from the cell for similar rows. Please see the snapshot in the link.

snapshot of the output here: black tick is correct, red crosses are wrong enter image description here

I got the 1st three sections correct but others I got wrong. the red cross sections are wrong as I need to get the date in the row section.

I need to get the date info to the row for each row with date value along the column.

Advertisement

Answer

Using a CTE and a double use of the window function for MAX works for this.

And then it can be done in 1 update statement.

In the SQL, the first MAX calculates the highest previous/current Id with a valid column1 value.
The second MAX uses that calculated id to get the valid column1 value.

Sample data:

Update & select

Returns:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement