Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

sql - Update Table with different values depending on a column

I have a table:

ProductCode         Date                       Number
COD666AN            2020-12-18 12:02:47.330    5
COD666AN            2020-12-18 12:02:47.770    6
COD666AN            2020-12-18 12:18:05.587    7
COD666AN            2020-12-18 15:13:08.993    8
COD666AN            2020-12-18 15:13:09.400    9
COD666AN            2020-12-18 15:31:39.497    20000000
COD666AN            2020-12-23 11:12:06.140    10
COD666AN            2020-12-23 11:14:06.720    11
COD666AN            2020-12-23 11:14:52.957    12
COD666AN            2020-12-23 11:14:53.360    13
COD666AN            2020-12-23 11:16:49.673    30000000

And i was trying to update the column "Number" to have a result like this:

ProductCode         Date                       Number
COD666AN            2020-12-18 12:02:47.330    20000000
COD666AN            2020-12-18 12:02:47.770    20000000
COD666AN            2020-12-18 12:18:05.587    20000000
COD666AN            2020-12-18 15:13:08.993    20000000
COD666AN            2020-12-18 15:13:09.400    20000000
COD666AN            2020-12-18 15:31:39.497    20000000
COD666AN            2020-12-23 11:12:06.140    30000000
COD666AN            2020-12-23 11:14:06.720    30000000
COD666AN            2020-12-23 11:14:52.957    30000000
COD666AN            2020-12-23 11:14:53.360    30000000
COD666AN            2020-12-23 11:16:49.673    30000000

I would like to avoid the cursors, but if there is no way to avoid them doesn't matter.

Note: My table could have more "blocks" ( i call blocks the dates between the higher numbers), so for example.... 14, 15, 16, 17 , 40000000.....

Logic: As you can see the table is ordered by the column [Date] asc, so i would like to update the column [Number] with the same value of the max value of the column [Number] before the next higher value, in order to have all the values before 20000000 equal to 20000000 and before 30000000 but not before 20000000 equal to 30000000.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can only speculate on the logic. It looks like you want a cumulative (in reverse order) min, where number is above a threshold:

select t.*,
       min(case when number > 10000 then number end) over (partition by productcode order by date desc) as imputed_number
from t;

You can then use this in an update:

with toupdate as (
      select t.*,
             min(case when number > 10000 then number end) over (partition by productcode order by date desc) as imputed_number
      from t
     )
update toupdate
     set number = imputed_number
     where number <> imputed_number;

Here is a db<>fiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...