Oracle SQL:如果两行的Col1、Col2和Col3中的值相同,则将Col4中的NULL替换为其他行中的Col4的值

omjgkv6w  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(106)

以下是我的样本数据:
| 手术日期|房间_NM|服务中心_NM|外科医生_NM|
| --|--|--|--|
| 2022-08-07 00:00:00|房间1|骨科|Json,B B|
| 2022-08-11 10:00:00|室4|心脏病学|格伦角|
| 2022-08-09 00:00:00|室3|妇科|马丁河|
| 2022-08-11 10:00:00|室2|心脏病学|格伦角|
| 2022-08-07 00:00:00|室2|血管|约瑟夫湖|
| 2022-08-07 00:00:00|房间1|麻醉学|(空值)|
| 2022-08-11 10:00:00|室4|心脏病学|(空值)|
| 2022-08-07 00:00:00|房间1|骨科|(空值)|
| 2022-08-07 00:00:00|室2|头颈|(空值)|
| 2022-08-09 00:00:00|室3|妇科|(空值)|
| 2022-08-08 00:00:00|房间1|麻醉学|(空值)|
我的预期输出:
| 手术日期|房间_NM|服务中心_NM|外科医生_NM|
| --|--|--|--|
| 2022-08-07 00:00:00|房间1|骨科|Json,B B|
| 2022-08-11 10:00:00|室4|心脏病学|格伦角|
| 2022-08-09 00:00:00|室3|妇科|马丁河|
| 2022-08-11 10:00:00|室2|心脏病学|格伦角|
| 2022-08-07 00:00:00|室2|血管|约瑟夫湖|
| 2022-08-07 00:00:00|房间1|麻醉学|(空值)|
| 2022-08-11 10:00:00|室4|心脏病学|格伦角|
| 2022-08-07 00:00:00|房间1|骨科|Json,B B|
| 2022-08-07 00:00:00|室2|头颈|(空值)|
| 2022-08-09 00:00:00|室3|妇科|马丁河|
| 2022-08-08 00:00:00|房间1|麻醉学|(空值)|
对于Oracle SQL Developer,我想知道是否有一种方法可以将SURGEON_NM中的值(null)替换为来自行的SURGEON_NM的值,其中在SURGERY_DATE,ROOM_NM和SERVICE_NM中有相同的值。
即,在上面的第一个表中,表1和表8在列Surgery_DATE、ROOM_NM和SERVICE_NM中具有相同的值。第8行的SURGEON_NM为(空),而第1行的值为“Json,B”。有没有办法将第8行的(空)替换为“Json,B”?
我感谢任何帮助!

ndh0cuux

ndh0cuux1#

使用COALESCEMAX解析函数:

SELECT surgery_date,
       room_nm,
       service_nm,
       COALESCE(
         surgeon_nm,
         MAX(surgeon_nm) OVER (PARTITION BY surgery_date, room_nm, service_nm)
       ) AS surgeon_nm
FROM   table_name
ORDER BY
       surgery_date,
       room_nm,
       service_nm,
       surgeon_nm;

其中,对于样本数据:

CREATE TABLE table_name (SURGERY_DATE, ROOM_NM, SERVICE_NM, SURGEON_NM) AS
SELECT DATE '2022-08-07', 'ROOM 1', 'Orthopedics', 'Johnson, B' FROM DUAL UNION ALL
SELECT DATE '2022-08-11', 'ROOM 4', 'Cardiology', 'Glen, D' FROM DUAL UNION ALL
SELECT DATE '2022-08-09', 'ROOM 3', 'Gynecology', 'Martin, R' FROM DUAL UNION ALL
SELECT DATE '2022-08-11', 'ROOM 2', 'Cardiology', 'Glen, D' FROM DUAL UNION ALL
SELECT DATE '2022-08-07', 'ROOM 2', 'Vascular', 'Joseph, L' FROM DUAL UNION ALL
SELECT DATE '2022-08-07', 'ROOM 1', 'Anesthesiology', null FROM DUAL UNION ALL
SELECT DATE '2022-08-11', 'ROOM 4', 'Cardiology', null FROM DUAL UNION ALL
SELECT DATE '2022-08-07', 'ROOM 1', 'Orthopedics', null FROM DUAL UNION ALL
SELECT DATE '2022-08-07', 'ROOM 2', 'Head and Neck', null FROM DUAL UNION ALL
SELECT DATE '2022-08-09', 'ROOM 3', 'Gynecology', null FROM DUAL UNION ALL
SELECT DATE '2022-08-08', 'ROOM 1', 'Anesthesiology', null FROM DUAL;

输出:
| 手术日期|房间_NM|服务中心_NM|外科医生_NM|
| --|--|--|--|
| 2019 -08-07 00:00:00|房间1|麻醉学| * 空 *|
| 2019 -08-07 00:00:00|房间1|骨科|Json,B B|
| 2019 -08-07 00:00:00|房间1|骨科|Json,B B|
| 2019 -08-07 00:00:00|室2|头颈| * 空 *|
| 2019 -08-07 00:00:00|室2|血管|约瑟夫湖|
| 2019 -08- 18 00:00:00|房间1|麻醉学| * 空 *|
| 2019 -08- 29 00:00:00|室3|妇科|马丁河|
| 2019 -08- 29 00:00:00|室3|妇科|马丁河|
| 2019 -08-11 00:00:00|室2|心脏病学|格伦角|
| 2019 -08-11 00:00:00|室4|心脏病学|格伦角|
| 2022-08-11 00:00:00|室4|心脏病学|格伦角|
fiddle

fdx2calv

fdx2calv2#

你说在那个表中还有其他列,但是它们的值是 distinct 的。如果有一些唯一的行标识符(例如 popularID,这是我添加到示例数据中的),一种选择是使用分析函数。大概是这样的:
样本数据:

SQL> with surgery (id, surgery_date, room_nm, service_nm, surgeon_nm) as
  2    (select 1, date '2022-08-07', 'room 1', 'orthopedics'  , 'johnson' from dual union all
  3     select 2, date '2022-08-11', 'room 4', 'cardiology'   , 'glen'    from dual union all
  4     select 3, date '2022-08-09', 'room 3', 'gynecology'   , 'martin'  from dual union all
  5     select 4, date '2022-08-07', 'room 1', 'anestesiology', null      from dual union all
  6     --
  7     select 5, date '2022-08-07', 'room 1', 'orthopedics'  , null      from dual union all
  8     select 6, date '2022-08-11', 'room 4', 'cardiology'   , null      from dual union all
  9     select 7, date '2022-08-09', 'room 3', 'gynecology'   , null      from dual
 10    )

查询方式:

11  select surgery_date, room_nm, service_nm,
 12    nvl(surgeon_nm,
 13        lag (surgeon_nm) ignore nulls over (partition by surgery_date, room_nm, service_nm
 14                                            order by id)) surgeon_nm
 15  from surgery
 16  order by id;

SURGERY_DA ROOM_N SERVICE_NM    SURGEON
---------- ------ ------------- -------
2022-08-07 room 1 orthopedics   johnson
2022-08-11 room 4 cardiology    glen
2022-08-09 room 3 gynecology    martin
2022-08-07 room 1 anestesiology
2022-08-07 room 1 orthopedics   johnson
2022-08-11 room 4 cardiology    glen
2022-08-09 room 3 gynecology    martin

7 rows selected.

SQL>

相关问题