All notebooks
Getting Started: SQL Basics
Work with Aidbox database
16 cells · updated May 16, 2024
This is a read-only view. Responses shown were saved when the notebook was published —
open it in your own Aidbox to run the cells against live data.
Prepare data
Please make sure you understand the next step will delete all Patient and Observation resources in your DB. Please omit it if you want to keep the records.
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
-- truncate patient;
-- truncate observation;No results were returned by the query.
Let's add Patient resources
REST
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
POST /fhir/Patient/$load
Accept: text/yaml
Content-Type: text/yaml
source: 'https://storage.googleapis.com/aidbox-public/synthea/100/Patient.ndjson.gz'
Response:
Body
Status:
200
{total: 124}
Let's add Observation resources
REST
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
POST /fhir/Observation/$load
Accept: text/yaml
Content-Type: text/yaml
source: 'https://storage.googleapis.com/aidbox-public/synthea/100/Observation.ndjson.gz'
Response:
Body
Status:
200
{total: 20382}
Discover resources
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select count(*) from patientResult
| count |
|---|
| 126 |
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select resource#>>'{gender}' as gender, count(*)
from patient
group by 1
Result
| count | gender |
|---|---|
| 2 | |
| 62 | female |
| 62 | male |
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select id
, resource#>>'{name,0,family}' as family
, resource#>>'{name,0,given,0}' as given
, resource#>>'{birthDate}' as birthDate
, extract(year from age(now(), (resource#>>'{birthDate}')::date)) as age
from patient
order by 5 desc
limit 5Result
| id | given | family | age | birthdate |
|---|---|---|---|---|
| pt-2 | Jane | Doe | ||
| pt-1 | John | Doe | ||
| 2b08424d-1733-4ded-b5db-54ff3cda2557 | Chong355 | Ziemann98 | 108 | 1915-06-23 |
| 28694499-75a8-4f18-86da-406ea997460d | Amalia471 | Funk324 | 108 | 1915-06-23 |
| 61bdd30b-ee2b-4033-a9de-112ac1271e91 | Amalia471 | Botsford977 | 108 | 1915-06-23 |
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select id, resource#>>'{subject, id}' as patient_id
from observation
limit 5Result
| id | patient_id |
|---|---|
| bloodgroup | pat-123 |
| rhstatus | pat-123 |
| bgpanel | pat-123 |
| 086bec29-56b7-41ca-b433-8c71bf357178 | 0be40874-5ae1-46e0-b549-1b5e17c58518 |
| 02f122d4-8156-4f8b-a94e-dd87137f9d37 | c75af4ff-d5e9-4bf4-a391-3b7a5f27f8ee |
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select extract(year from age(now(), (p.resource#>>'{birthDate}')::date)) as "Patient age"
, p.resource#>>'{gender}' as "Patient gender"
, o.resource#>>'{code,text}' as "Observation code"
, concat(o.resource#>>'{value,Quantity,value}', ' ', o.resource#>>'{value,Quantity,unit}') as "Observation value"
from observation as o
join patient as p on p.id = o.resource#>>'{subject, id}'
limit 10Result
| patient age | patient gender | observation code | observation value |
|---|---|---|---|
| 36 | female | Sexual orientation | |
| 12 | male | Abuse Status [OMAHA] | |
| 12 | male | Sexual orientation | |
| 36 | female | Sexual orientation | |
| 36 | female | Housing status | |
| 77 | male | Chloride | 103.84672530842549 mmol/L |
| 36 | female | Sexual orientation | |
| 12 | male | HIV status | |
| 12 | male | Abuse Status [OMAHA] | |
| 36 | female | HIV status |
Discover database
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select relname as table_name
, n_live_tup as count
from pg_stat_all_tables
where schemaname = 'public'
order by count desc
limit 10Result
| count | table_name |
|---|---|
| 20385 | observation |
| 4488 | claim |
| 3464 | encounter |
| 3460 | explanationofbenefit |
| 2854 | procedure |
| 1636 | immunization |
| 1430 | diagnosticreport |
| 1028 | medicationrequest |
| 873 | condition |
| 356 | careplan |
SQL
Send
Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
SELECT nspname || '.' || relname AS relation, pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 10;Result
| size | relation |
|---|---|
| 40 MB | public.observation |
| 34 MB | public.explanationofbenefit |
| 18 MB | public.claim |
| 7408 kB | public.attribute |
| 6608 kB | public.encounter |
| 6016 kB | public.diagnosticreport |
| 4568 kB | public.procedure |
| 2664 kB | pg_toast.pg_toast_36264986 |
| 2368 kB | public.observation_pkey |
| 2096 kB | public.immunization |