合并多个值上的行,删除 SQL Oracle 中的 null

问题描述 投票:0回答:1

我有一台设备的校准日志。有 3 种校准类型需要每天记录,失败的结果需要重新测试。

一天内的数据如下所示:

设备# 校准 很多 结果数 结果评估
1 123 2 通过
1 123 1 通过
1 室温 74 通过
2 124 2 通过
2 124 2 失败
2 124 1 通过
2 室温 74 通过

我需要我的专栏看起来像:

装备# 很多 校准高 口径低 室温 结果评估
1 123 2 1 74 通过
2 124 2 2 74 失败
2 124 2 1 74 通过

我尝试过使用 MAX,但它忽略了重考。

select
equipment_#,
lot,
MAX(DECODE(calibration, 'ROOM TEMP', num_result)) Room_temp_result,
MAX(DECODE(calibration, 'ROOM TEMP', result_assessment)) Room_asessment,
MAX(DECODE(calibration, 'LOW', num_result)) low_result,
MAX(DECODE(calibration, 'LOW', result_assessment)) low_assessment,
MAX(DECODE(calibration, 'HIGH', num_result)) high_result,
MAX(DECODE(calibration, 'HIGH', result_assessment)) high_assessment
from table
group by 
equipment_#,
lot
sql oracle max
1个回答
0
投票

假设:

  • 如果
    LOT
    NULL
    那么它应该是该
    LOT
    的最大值
    EQUIPMENT#
  • 测试在
    PASS
    结果后停止,因此,对于每个
    EQUIPMENT#
    LOT
    ,应先按
    FAIL
    排序,最后按
    PASS
    排序,并且任何没有进行高/低/温度测试的行都应有之前通过的测试可以延续。

如果这些假设成立,那么您可以使用:

SELECT equipment#,
       lot,
       LAST_VALUE(high_result) IGNORE NULLS OVER (
         PARTITION BY equipment#, lot
         ORDER BY rn
       ) AS calib_high,
       LAST_VALUE(low_result) IGNORE NULLS OVER (
         PARTITION BY equipment#, lot
         ORDER BY rn
       ) AS calib_low,
       LAST_VALUE(temp_result) IGNORE NULLS OVER (
         PARTITION BY equipment#, lot
         ORDER BY rn
       ) AS room_temp,
       LEAST(
         LAST_VALUE(high_assessment) IGNORE NULLS OVER (
           PARTITION BY equipment#, lot
           ORDER BY rn
         ),
         LAST_VALUE(low_assessment) IGNORE NULLS OVER (
           PARTITION BY equipment#, lot
           ORDER BY rn
         ),
         LAST_VALUE(temp_assessment) IGNORE NULLS OVER (
           PARTITION BY equipment#, lot
           ORDER BY rn
         )
       ) AS result_assess
FROM   (
  SELECT EQUIPMENT#,
         CALIBRATION,
         COALESCE(LOT, MAX(lot) OVER (PARTITION BY equipment#)) AS lot,
         NUM_RESULT,
         RESULT_ASSESSMENT,
         ROW_NUMBER() OVER (
           PARTITION BY equipment#, calibration
           ORDER BY result_assessment
         ) AS rn
  FROM   table_name
)
PIVOT (
  MAX(num_result) AS result,
  MAX(result_assessment) AS assessment
  FOR calibration IN (
    'HIGH'      AS high,
    'LOW'       AS low,
    'ROOM TEMP' AS temp
  )
);

对于样本数据:

CREATE TABLE table_name (EQUIPMENT#, CALIBRATION, LOT, NUM_RESULT, RESULT_ASSESSMENT) AS
SELECT 1, 'HIGH',      123,   2, 'PASS' FROM DUAL UNION ALL
SELECT 1, 'LOW',       123,   1, 'PASS' FROM DUAL UNION ALL
SELECT 1, 'ROOM TEMP', NULL, 74, 'PASS' FROM DUAL UNION ALL
SELECT 2, 'HIGH',      124,   2, 'PASS' FROM DUAL UNION ALL
SELECT 2, 'LOW',       124,   2, 'FAIL' FROM DUAL UNION ALL
SELECT 2, 'LOW',       124,   1, 'PASS' FROM DUAL UNION ALL
SELECT 2, 'ROOM TEMP', NULL, 74, 'PASS' FROM DUAL;

输出:

设备# 很多 CALIB_HIGH CALIB_LOW 房间温度 结果_评估
1 123 2 1 74 通过
2 124 2 2 74 失败
2 124 2 1 74 通过

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.