80 lines
3.1 KiB
SQL
80 lines
3.1 KiB
SQL
-- Date: 2026-04-23
|
|
-- Commit message stem: fix-t-orderheader-group-result-per-test-data
|
|
-- Target: devone / one_lab
|
|
-- Problem:
|
|
-- Historical rows in t_orderheader_group_result for per-test group results
|
|
-- were inserted with T_OrderHeaderGroupResultGroup_ResultID = T_TestID.
|
|
-- The correct structure is:
|
|
-- - T_OrderHeaderGroupResultGroup_ResultID = Group_ResultID
|
|
-- - T_OrderHeaderGroupResultT_TestID = T_TestID
|
|
-- - T_OrderHeaderGroupResultGroup_ResultName = T_TestName
|
|
--
|
|
-- Safety:
|
|
-- This repair only updates active rows where:
|
|
-- 1. T_OrderHeaderGroupResultT_TestID > 0
|
|
-- 2. current Group_ResultID does not exist in master group_result
|
|
-- 3. the T_TestID maps to exactly one active Group_ResultID in group_resultdetail
|
|
|
|
START TRANSACTION;
|
|
|
|
-- Preview rows that will be updated
|
|
SELECT gr.T_OrderHeaderGroupResultID,
|
|
gr.T_OrderHeaderGroupResultT_OrderHeaderID,
|
|
gr.T_OrderHeaderGroupResultGroup_ResultID AS wrong_group_result_id,
|
|
map.correct_group_result_id,
|
|
gr.T_OrderHeaderGroupResultT_TestID,
|
|
t.T_TestName,
|
|
gr.T_OrderHeaderGroupResultGroup_ResultName AS current_name
|
|
FROM t_orderheader_group_result gr
|
|
JOIN (
|
|
SELECT Group_ResultDetailT_TestID AS test_id,
|
|
MIN(Group_ResultDetailGroup_ResultID) AS correct_group_result_id
|
|
FROM group_resultdetail
|
|
WHERE Group_ResultDetailIsActive = 'Y'
|
|
GROUP BY Group_ResultDetailT_TestID
|
|
HAVING COUNT(DISTINCT Group_ResultDetailGroup_ResultID) = 1
|
|
) map
|
|
ON gr.T_OrderHeaderGroupResultT_TestID = map.test_id
|
|
JOIN t_test t
|
|
ON gr.T_OrderHeaderGroupResultT_TestID = t.T_TestID
|
|
LEFT JOIN group_result g
|
|
ON gr.T_OrderHeaderGroupResultGroup_ResultID = g.Group_ResultID
|
|
WHERE gr.T_OrderHeaderGroupResultIsActive = 'Y'
|
|
AND gr.T_OrderHeaderGroupResultT_TestID > 0
|
|
AND g.Group_ResultID IS NULL
|
|
ORDER BY gr.T_OrderHeaderGroupResultID;
|
|
|
|
-- Apply repair
|
|
UPDATE t_orderheader_group_result gr
|
|
JOIN (
|
|
SELECT Group_ResultDetailT_TestID AS test_id,
|
|
MIN(Group_ResultDetailGroup_ResultID) AS correct_group_result_id
|
|
FROM group_resultdetail
|
|
WHERE Group_ResultDetailIsActive = 'Y'
|
|
GROUP BY Group_ResultDetailT_TestID
|
|
HAVING COUNT(DISTINCT Group_ResultDetailGroup_ResultID) = 1
|
|
) map
|
|
ON gr.T_OrderHeaderGroupResultT_TestID = map.test_id
|
|
JOIN t_test t
|
|
ON gr.T_OrderHeaderGroupResultT_TestID = t.T_TestID
|
|
LEFT JOIN group_result g
|
|
ON gr.T_OrderHeaderGroupResultGroup_ResultID = g.Group_ResultID
|
|
SET gr.T_OrderHeaderGroupResultGroup_ResultID = map.correct_group_result_id,
|
|
gr.T_OrderHeaderGroupResultGroup_ResultName = t.T_TestName
|
|
WHERE gr.T_OrderHeaderGroupResultIsActive = 'Y'
|
|
AND gr.T_OrderHeaderGroupResultT_TestID > 0
|
|
AND g.Group_ResultID IS NULL;
|
|
|
|
SELECT ROW_COUNT() AS updated_rows;
|
|
|
|
-- Verify no wrong active rows remain
|
|
SELECT COUNT(*) AS remaining_wrong_rows
|
|
FROM t_orderheader_group_result gr
|
|
LEFT JOIN group_result g
|
|
ON gr.T_OrderHeaderGroupResultGroup_ResultID = g.Group_ResultID
|
|
WHERE gr.T_OrderHeaderGroupResultIsActive = 'Y'
|
|
AND gr.T_OrderHeaderGroupResultT_TestID > 0
|
|
AND g.Group_ResultID IS NULL;
|
|
|
|
COMMIT;
|