Query data from flat views

Using flat views

Once you've saved your View Definition, the corresponding flat view will be created in a database in sof schema. For example, to select all the rows from a view named patient_view you'll need a query like this:

select * from sof.patient_view

Note that in the current version of Aidbox, View Definitions can be materialized as SQL views only.

From here, you can use your flat views however you like. Popular use cases include building complex queries for data analysis and using BI-tools to build dashboards.

Real-world examples

Get all patients diagnosed with COVID after a specified date

To find all patients who was born after or in 1970 and who were diagnosed with COVID after or in 2021, you'll need to define 2 views.

{
  "name": "patient_view",
  "resource": "Patient",
  "status": "active",
  "description": "Patient flat view",
  "select": [
    {
      "column": [
        {
          "name": "id",
          "path": "id"
        },
        {
          "name": "name",
          "path": "name[0].given.join(' ')"
        },
        {
          "name": "birthDate",
          "path": "birthDate"
        }
      ]
    }
  ]
}

With these views defined, you can query the information you need with the following query.

select pt.name,
       pt.birthDate,
       cond.date
from sof.patient_view pt
join sof.condition_view cond on cond.pid = pt.id
where pt.birthDate > '1970-01-01'
  and cond.code = '840539006'
  and cond.system = 'http://snomed.info/sct'
  and cond.date > '2021-01-01'
limit 100;

All patients who had an encounter in a specified period in a given location

To find all times patients had an encounter in 2020 or later in a location managed by a given organization you'll need to define 3 views.

{
  "name": "patient_view",
  "resource": "Patient",
  "description": "Patient flat view",
  "status": "active",
  "select": [
    {
      "column": [
        {
          "name": "id",
          "path": "id"
        },
        {
          "name": "name",
          "path": "name[0].given.join(' ')"
        }
      ]
    }
  ]
}

Then, your query will look like this.

select pt.name,
       loc.name location,
       enc.start_time
from sof.patient_view pt
join sof.encounter_view enc on enc.subject_id = pt.id
join sof.location_view loc on enc.location_id = loc.id
where enc.start_time > '2020-01-01'
  and loc.org_id = '74ab949d-17ac-3309-83a0-13b4405c66aa'
limit 100;

Last updated