简体   繁体   English

如何根据其他列的值更新列

[英]How to update a column based on values of other columns

I have a tables as below我有一个如下表

row_wid id code sub_code item_nbr orc_cnt part_cnt variance reporting_date var_start_date
1      1   ABC   PQR     23AB      0       1        1   11-10-2019  NULL
2      1   ABC   PQR     23AB      0       1        1   12-10-2019  NULL
3      1   ABC   PQR     23AB      1       1        0   13-10-2019  NULL
4      1   ABC   PQR     23AB      1       2        1   14-10-2019  NULL
5      1   ABC   PQR     23AB      1       3        2   15-10-2019  NULL

I have to update var_start_date column with min(reporting_date) for each combination of id,code,sub_code and item_nbr only till variance field is zero.对于 id、code、sub_code 和 item_nbr 的每个组合,我必须使用 min(reporting_date) 更新 var_start_date 列,直到方差字段为零。 Row with variance = 0 should have null var_start_date.方差 = 0 的行应该有 null var_start_date。 and next row after that should have next min(var_start_date.).之后的下一行应该有下一个分钟(var_start_date。)。 FYI, variance is calculated as par_cnt-orc_cnt仅供参考,方差计算为 par_cnt-orc_cnt

so my output should look like this -所以我的 output 应该是这样的 -

row_wid id code sub_code item_nbr orc_cnt part_cnt variance reporting_date var_start_date
1      1   ABC   PQR     23AB      0       1        1   11-10-2019  11-10-2019
2      1   ABC   PQR     23AB      0       1        1   12-10-2019  11-10-2019
3      1   ABC   PQR     23AB      1       1        0   13-10-2019  NULL
4      1   ABC   PQR     23AB      1       2        1   14-10-2019  14-10-2019
5      1   ABC   PQR     23AB      1       3        2   15-10-2019  14-10-2019

I am trying to write a function using below query to divide the data into sets.我正在尝试使用以下查询编写 function 以将数据分成组。

SELECT DISTINCT MIN(reporting_date) 
        OVER (partition by id, code,sub_code,item_nbr ORDER BY row_wid ),
        RANK() OVER (partition by id, code,sub_code,item_nbr ORDER BY row_wid)
        AS rnk,id, code,sub_code,item_nbr,orc_cnt,part_cnt,variance,row_wid
FROM TABLE T1

.But dont know how to include variance field to split the sets. .但不知道如何包含方差字段来拆分集合。

I would suggest:我会建议:

select t.*,
       (case when variance <> 0
             then min(reporting_date) over (partition by id, code, sub_code, item_nbr, grouping)
        end) as new_reporting_date
from (select t.*,
             sum(case when variance = 0 then 1 else 0 end) over (partition by id, code, sub_code, item_nbr) as grouping
      from t
     ) t;

Note that this does not use a JOIN .请注意,这不使用JOIN It should be more efficient than an answer that does.它应该比答案更有效。

Try as below尝试如下

SELECT T.*, CASE WHEN T.variance = 0 THEN NULL ELSE MIN(reporting_date) OVER (PARTITION BY T1.RANK ORDER BY T1.RANK) END AS New_var_start_date 
FROM mytbl T
LEFT JOIN (
           SELECT row_wid, variance, COUNT(CASE variance WHEN 0 THEN 1 END) OVER (ORDER BY row_wid ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) +1 AS [Rank]
           FROM mytbl 
          ) T1 ON T.row_wid = T1.row_wid

SQL FIDDLE DEMO SQL 小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM