SQL on FHIR
2.1.0-pre - ci-build International flag

SQL on FHIR, published by HL7 International / FHIR Infrastructure. This guide is not an authorized publication; it is the continuous build for version 2.1.0-pre built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/HL7/sql-on-fhir/ and changes regularly. See the Directory of published versions

Library: Unique Patient Addresses

Official URL: http://hl7.org/fhir/uv/sql-on-fhir/Library/UniquePatientAddressesQuery Version: 2.1.0-pre
Standards status: Informative Computable Name: UniquePatientAddressesQuery
Other Identifiers: OID:2.16.840.1.113883.4.642.40.77.28.5

Retrieves each patient's most recent address in a given city. Includes two dialects for the same logical query.

Standard SQL:

WITH ranked_addresses AS (
  SELECT
    patient_view.id AS patient_id,
    patient_view.name,
    patient_address_view.address_line1,
    patient_address_view.city,
    patient_address_view.state,
    patient_address_view.postal_code,
    patient_address_view.updated_at,
    ROW_NUMBER() OVER (
      PARTITION BY patient_view.id
      ORDER BY patient_address_view.updated_at DESC, patient_address_view.address_id DESC
    ) AS address_rank
  FROM patient_view
  JOIN patient_address_view
    ON patient_view.id = patient_address_view.patient_id
  WHERE patient_view.active = true
    AND patient_address_view.city = :city
)
SELECT
  patient_id,
  name,
  address_line1,
  city,
  state,
  postal_code
FROM ranked_addresses
WHERE address_rank = 1

PostgreSQL dialect:

SELECT DISTINCT ON (patient_view.id)
  patient_view.id AS patient_id,
  patient_view.name,
  patient_address_view.address_line1,
  patient_address_view.city,
  patient_address_view.state,
  patient_address_view.postal_code
FROM patient_view
JOIN patient_address_view
  ON patient_view.id = patient_address_view.patient_id
WHERE patient_view.active = true
  AND patient_address_view.city = :city
ORDER BY patient_view.id, patient_address_view.updated_at DESC, patient_address_view.address_id DESC

Title: Unique Patient Addresses
Id: UniquePatientAddressesQuery
Version: 2.1.0-pre
Url: Unique Patient Addresses

urn:oid:2.16.840.1.113883.4.642.40.77.28.5

Type:

system: http://hl7.org/fhir/uv/sql-on-fhir/CodeSystem/LibraryTypesCodes

code: sql-query

Date: 2026-07-28 04:38:44+0000
Publisher: HL7 International / FHIR Infrastructure
Description:

Retrieves each patient's most recent address in a given city. Includes two dialects for the same logical query.

Standard SQL:

WITH ranked_addresses AS (
  SELECT
    patient_view.id AS patient_id,
    patient_view.name,
    patient_address_view.address_line1,
    patient_address_view.city,
    patient_address_view.state,
    patient_address_view.postal_code,
    patient_address_view.updated_at,
    ROW_NUMBER() OVER (
      PARTITION BY patient_view.id
      ORDER BY patient_address_view.updated_at DESC, patient_address_view.address_id DESC
    ) AS address_rank
  FROM patient_view
  JOIN patient_address_view
    ON patient_view.id = patient_address_view.patient_id
  WHERE patient_view.active = true
    AND patient_address_view.city = :city
)
SELECT
  patient_id,
  name,
  address_line1,
  city,
  state,
  postal_code
FROM ranked_addresses
WHERE address_rank = 1

PostgreSQL dialect:

SELECT DISTINCT ON (patient_view.id)
  patient_view.id AS patient_id,
  patient_view.name,
  patient_address_view.address_line1,
  patient_address_view.city,
  patient_address_view.state,
  patient_address_view.postal_code
FROM patient_view
JOIN patient_address_view
  ON patient_view.id = patient_address_view.patient_id
WHERE patient_view.active = true
  AND patient_address_view.city = :city
ORDER BY patient_view.id, patient_address_view.updated_at DESC, patient_address_view.address_id DESC
Jurisdiction: 001
Related Artifacts:

Dependencies

  • https://example.org/ViewDefinition/patient_view
  • https://example.org/ViewDefinition/patient_address_view
Parameters:
NameTypeMinMaxIn/Out
citystringIn
Content: application/sql
Encoded data (1000 characters)
Content: application/sql;dialect=postgresql
Encoded data (660 characters)