我有一台设备的校准日志。有 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
假设:
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 | 通过 |