For AI agents: the documentation index is at /docs/aidbox/llms.txt. A Markdown version of this page is available at /docs/aidbox/notebooks.md or by requesting it with the Accept: text/markdown header.
Aidbox Docs
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 patient
Result
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
countgender
2
62female
62male
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 5
Result
idgivenfamilyagebirthdate
pt-2JaneDoe
pt-1JohnDoe
2b08424d-1733-4ded-b5db-54ff3cda2557Chong355Ziemann981081915-06-23
28694499-75a8-4f18-86da-406ea997460dAmalia471Funk3241081915-06-23
61bdd30b-ee2b-4033-a9de-112ac1271e91Amalia471Botsford9771081915-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 5
Result
idpatient_id
bloodgrouppat-123
rhstatuspat-123
bgpanelpat-123
086bec29-56b7-41ca-b433-8c71bf3571780be40874-5ae1-46e0-b549-1b5e17c58518
02f122d4-8156-4f8b-a94e-dd87137f9d37c75af4ff-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 10
Result
patient agepatient genderobservation codeobservation value
36femaleSexual orientation
12maleAbuse Status [OMAHA]
12maleSexual orientation
36femaleSexual orientation
36femaleHousing status
77maleChloride103.84672530842549 mmol/L
36femaleSexual orientation
12maleHIV status
12maleAbuse Status [OMAHA]
36femaleHIV 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 10
Result
counttable_name
20385observation
4488claim
3464encounter
3460explanationofbenefit
2854procedure
1636immunization
1430diagnosticreport
1028medicationrequest
873condition
356careplan
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
sizerelation
40 MBpublic.observation
34 MBpublic.explanationofbenefit
18 MBpublic.claim
7408 kBpublic.attribute
6608 kBpublic.encounter
6016 kBpublic.diagnosticreport
4568 kBpublic.procedure
2664 kBpg_toast.pg_toast_36264986
2368 kBpublic.observation_pkey
2096 kBpublic.immunization