Code: |
Row id Account Cured Code1 Date field1 field2 1231 123 0 2020-01-15-22.43.12.986358 xxxx xxxx 1232 124 4 RC00 2020-01-15-22.43.12.986358 xxxx xxxx 1234 123 3 RC00 2020-01-16-22.43.12.986358 xxxx xxxx 1235 124 0 2020-01-16-22.43.13.986358 xxxx xxxx 1236 123 4 RC11 2020-01-17-22.43.14.986358 xxxx xxxx 1237 125 4 RC11 2020-01-17-22.43.14.986358 xxxx xxxx 1238 126 4 RC11 2020-01-17-22.43.14.986358 xxxx xxxx 1239 123 2020-01-18-22.43.12.986358 xxxx xxxx 1240 124 2020-01-18-22.43.12.986358 xxxx xxxx 1241 123 2020-01-19-22.43.14.986358 1242 124 2020-01-19-22.43.14.986358 |
Magesh_J wrote: |
the table loaded on 2020-01-19 got messed up, and hence I need to update the table with last known good value of fields cured, code,field1,filed2 of the table. is it possible to do it in SQL itself? Thanks Magesh |
Code: |
SELECT X.* FROM table X ,(SELECT ACCOUNT ,MAX(DATE) AS DATE FROM table WHERE ROWID < 1241 GROUP BY ACCOUNT) Y WHERE X.ACCOUNT = Y.ACCOUNT AND X.DATE = Y.DATE AND X.ROWID < 1241 ORDER BY ROWID ,ACCOUNT |
kolusu wrote: | ||
|
Code: |
SELECT X.* FROM table X ,(SELECT ACCOUNT ,MAX(DATE) AS DATE FROM table WHERE ROWID < 1241 GROUP BY ACCOUNT) Y, (SELECT ACCOUNT FROM TABLE WHERE ROWID > 1241) Z, WHERE Y.ACCOUNT = Z.ACCOUNT AND X.ACCOUNT = Y.ACCOUNT AND X.DATE = Y.DATE AND X.ROWID < 1241 ORDER BY ROWID ,ACCOUNT |
Magesh_J wrote: |
Hi Kolusu,
Thanks for the response. But we need to know last good values for accounts having row_num > 1241. |
Code: |
INSERT INTO TABLE(ACCOUNT,CURED,CODE1,FIELD1,FIELD2) SELECT Z.ACCOUNT,X.CURED,X.CODE1,X.FIELD1,X.FIELD2 FROM TABLE X, (SELECT ACCOUNT,MAX(DATE) AS DATE FROM TABLE WHERE ROWID < 1241 GROUP BY ACCOUNT) Y, (SELECT ACCOUNT FROM TABLE WHERE ROWID >= 1241) Z, WHERE Y.ACCOUNT = Z.ACCOUNT AND X.ACCOUNT = Y.ACCOUNT AND X.DATE = Y.DATE AND X.ROWID < 1241 ORDER BY ROWID ,ACCOUNT |
Code: |
DELETE FROM TABLE WHERE ROW_NUM >=1241 AND ROW_NUM =<1242 |
Code: |
INSERT INTO TABLE(ACCOUNT ,CURED ,CODE1 ,FIELD1 ,FIELD2) SELECT Z.ACCOUNT ,X.CURED ,X.CODE1 ,X.FIELD1 ,X.FIELD2 FROM TABLE X ,(SELECT ACCOUNT ,MAX(DATE) AS DATE FROM TABLE WHERE ROWID < 1241 GROUP BY ACCOUNT) Y ,(SELECT ACCOUNT FROM TABLE WHERE ROWID >= 1241) Z WHERE Y.ACCOUNT = Z.ACCOUNT AND X.ACCOUNT = Y.ACCOUNT AND X.DATE = Y.DATE AND X.ROWID < 1241 ORDER BY ROWID ,ACCOUNT ; |
output generated using printer-friendly topic mod. All times are GMT - 5 Hours