From c832b3a3e242e895929961f97d1251e9ddd5b431 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 2 Jul 2026 19:09:36 +0530 Subject: [PATCH] Add remote SQL script for investigation category backfill --- ...-07-02_backfill_investigation_category.sql | 25 +++++++++++++++++ .../README_investigation_category_backfill.md | 28 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 scripts/sql/2026-07-02_backfill_investigation_category.sql create mode 100644 scripts/sql/README_investigation_category_backfill.md diff --git a/scripts/sql/2026-07-02_backfill_investigation_category.sql b/scripts/sql/2026-07-02_backfill_investigation_category.sql new file mode 100644 index 0000000..fbfa02e --- /dev/null +++ b/scripts/sql/2026-07-02_backfill_investigation_category.sql @@ -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; diff --git a/scripts/sql/README_investigation_category_backfill.md b/scripts/sql/README_investigation_category_backfill.md new file mode 100644 index 0000000..df9ed94 --- /dev/null +++ b/scripts/sql/README_investigation_category_backfill.md @@ -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 -p < 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.