Your question is SQL: Complex Expression Result. Start with the requirements and the three tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
HCA Healthcare's authorization audit needs a derived score for each completed request code. For a code such as 367, calculate 3^1 + 6^2 + 7^3 = 382, where each digit is raised to its one-based position.
Write a PostgreSQL query that joins HCA request, patient, and facility data, calculates the score, and ranks requests within each active facility.
status = 'Completed', an active facility, and a three-digit numeric request_code.| request_id | request_code | status | facility_id |
|---|---|---|---|
| 101 | 367 | Completed | 1 |
| 102 | 153 | Completed | 1 |
| 103 | 407 | Completed | 2 |
| 105 | 222 | Pending | 3 |
| 107 | 999 | Completed | 4 |
| 112 | 12 | Completed | 1 |
| Column | Type | Description |
|---|---|---|
| request_idPK | INT | Unique authorization request identifier |
| patient_id | INT | Referenced patient |
| facility_id | INT | Referenced HCA Healthcare facility |
| request_code | VARCHAR(20) | Authorization code to score |
| status | VARCHAR(20) | Current request status |
| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| patient_name | VARCHAR(100) | Patient display name |
| Column | Type | Description |
|---|---|---|
| facility_idPK | INT | Unique facility identifier |
| facility_name | VARCHAR(120) | HCA Healthcare facility name |
| is_active | BOOLEAN | Whether the facility is active for reporting |