Quantcast
Channel: T-SQL question
Viewing all articles
Browse latest Browse all 5

T-SQL question

0
0

Hello All,

I have a table called TOTAL_DEPTH and here is the script for it

CREATE TABLE #TOTAL_DEPTH( well_id int,Report_dt date, source_id int,totl_dpth int, tvd_dpth int)

INSERT INTO #TOTAL_DEPTH VALUES (11, '9/25/2017',16,0, 0)
INSERT INTO #TOTAL_DEPTH VALUES (11,'9/26/2017',16,1897,1897)
INSERT INTO #TOTAL_DEPTH VALUES (11,'9/27/2017',16,1895.52,1895.52)
INSERT INTO #TOTAL_DEPTH VALUES (11,'12/27/2017',8284,17885,10253)
INSERT INTO #TOTAL_DEPTH VALUES (11,'2/21/2018',16,11870,10248 )
INSERT INTO #TOTAL_DEPTH VALUES (11,'2/21/2018',8284,17885,10253)

The data looks like this

well_id      report_dt      source_id        totl_dpth       tvd_dpth

11           9/25/2017        16                 0              0
11           9/26/2017        16                 1897           1897
11           9/27/2017        16                 1895.52        1895.52
11           12/27/2017       8284               17885          10253
11           2/21/2018        16                 11870          10248
11           2/21/2018        8284               17885          10253  


I need to update the TOTL_DPTH and TVD_DPTH columns for SOURCE_ID : 8284 based on the previous REPORT_DT of the SOURCE_ID : 16. Here for WELL_ID : 11, REPORT_DT : 12/27/2017, SOURCE_ID : 8284 the previous REPORT_DT for this for SOURCE_ID : 16 is 9/27/2017. So, I need to update the TOTL_DPTH and TVD_DPTH as 1895.52 and 1895.52 for REPORT_DT  :12/27/2017 and SOURCE_ID  =8284 for well_id = 11.

If the REPORT_DT is same for both the SOURCE_ID's 8284 and 16, then update TOTL_DPTH and TVD_DPTH whatever we have on SOURCE_ID : 16

So, the output should be

well_id      report_dt      source_id        totl_dpth       tvd_dpth

11           9/25/2017        16                 0              0
11           9/26/2017        16                 1897           1897
11           9/27/2017        16                 1895.52        1895.52
11           12/27/2017       8284             1895.52        1895.52
11           2/21/2018        16                 11870          10248
11           2/21/2018        8284              11870          10248

I am not sure how I can go to the previous report_dt for of source_id : 16 for each report_dt of source_id : 8284. Please help me with this requirement.

Thank you so much for all the help.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images