Add remote SQL script for investigation category backfill
This commit is contained in:
parent
681bb947b3
commit
c832b3a3e2
25
scripts/sql/2026-07-02_backfill_investigation_category.sql
Normal file
25
scripts/sql/2026-07-02_backfill_investigation_category.sql
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-- Backfill blank investigation.category_name values using name-based rules.
|
||||
-- Safe to run multiple times: updates only rows where category_name is NULL/blank.
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
UPDATE investigation
|
||||
SET category_name = CASE
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(x[[:space:]]*[-]?[[:space:]]*ray|xray|radiograph|radiology)([^a-z]|$)' THEN 'X-Ray'
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(ultra[[:space:]]*sound|ultrasound|u\\.?s\\.?g\\.?|usg|sonography|doppler)([^a-z]|$)' THEN 'Ultra Sound'
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(ct[[:space:]]*[-]?[[:space:]]*scan|cect|hrct|ncct|computed tomography)([^a-z]|$)' THEN 'CT-Scan'
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(mri|mr[[:space:]]*i|magnetic resonance)([^a-z]|$)' THEN 'MRI'
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(echo|echocardiography|2d[[:space:]]*echo|doppler[[:space:]]*echo)([^a-z]|$)' THEN 'ECHO'
|
||||
WHEN LOWER(Name) REGEXP '(^|[^a-z])(ecg|electrocardiogram|tmt|holter)([^a-z]|$)' THEN 'Cardiology'
|
||||
ELSE 'Pathology'
|
||||
END
|
||||
WHERE category_name IS NULL OR TRIM(category_name) = '';
|
||||
|
||||
SELECT ROW_COUNT() AS updated_rows;
|
||||
|
||||
SELECT category_name, COUNT(*) AS total_rows
|
||||
FROM investigation
|
||||
GROUP BY category_name
|
||||
ORDER BY total_rows DESC, category_name ASC;
|
||||
|
||||
COMMIT;
|
||||
28
scripts/sql/README_investigation_category_backfill.md
Normal file
28
scripts/sql/README_investigation_category_backfill.md
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# Investigation Category Backfill (Remote)
|
||||
|
||||
Run this SQL on remote DB:
|
||||
|
||||
scripts/sql/2026-07-02_backfill_investigation_category.sql
|
||||
|
||||
## Example (MySQL CLI)
|
||||
|
||||
```bash
|
||||
mysql -u <db_user> -p <db_name> < scripts/sql/2026-07-02_backfill_investigation_category.sql
|
||||
```
|
||||
|
||||
## What it does
|
||||
|
||||
- Updates only blank category values in investigation.category_name
|
||||
- Uses name-based rules to map into:
|
||||
- Pathology
|
||||
- X-Ray
|
||||
- Ultra Sound
|
||||
- CT-Scan
|
||||
- MRI
|
||||
- ECHO
|
||||
- Cardiology
|
||||
- Prints updated row count and final category distribution
|
||||
|
||||
## Idempotency
|
||||
|
||||
Safe to re-run. It only touches rows where category_name is blank/null.
|
||||
Loading…
Reference in New Issue
Block a user