Sql 如何用另一个具有公共键的单元格中的值填充空单元格?

Sql 如何用另一个具有公共键的单元格中的值填充空单元格?,sql,Sql,我有一个这样的数据集,它显示了谁批准了某个报告(约翰批准了报告“AB3”,艾伦批准了报告“AB4”) 但是,我想用批准者填写所有的报告ID的相应的批准人的空白单元格。对于最终结果,它应该是这样的。 | Approver | Report ID | Action | | John | AB3 | Report approved | | John | AB3 | Report Submitted | | John | AB3

我有一个这样的数据集,它显示了谁批准了某个报告(约翰批准了报告“AB3”,艾伦批准了报告“AB4”)

但是,我想用批准者填写所有的报告ID的相应的批准人的空白单元格。对于最终结果,它应该是这样的。

| Approver | Report ID | Action            |
| John     | AB3       | Report approved   |
| John     | AB3       | Report Submitted  |
| John     | AB3       | Report Generated  |
| John     | AB3       | Report Resubmitted|
| Alan     | AB4       | Report re-opened  |
| Alan     | AB4       | Report approved   |
| Alan     | AB4       | Report Submitted  |
| Alan     | AB4       | Report Generated  |

提前感谢!

您可以使用窗口功能:

select max(approver) over (partition by report_id) as approver,
       report_id, action
from t;
如果要实际更改数据,请使用
update

update t
    set approver = (select t2.approver
                    from t t2
                    where t2.report_id = t.report_id and t2.approver is not null
                    fetch first 1 row only
                   )
     where approver is null;

注意:这使用标准的SQL语法。确切的语法可能因数据库而异。

用您正在使用的数据库标记您的问题。
update t
    set approver = (select t2.approver
                    from t t2
                    where t2.report_id = t.report_id and t2.approver is not null
                    fetch first 1 row only
                   )
     where approver is null;