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
| Page standards status: Informative |
{
"resourceType" : "Library",
"id" : "UniquePatientAddressesQuery",
"meta" : {
"profile" : [
🔗 "http://hl7.org/fhir/uv/sql-on-fhir/StructureDefinition/SQLQuery"
]
},
"text" : {
"status" : "extensions",
"div" : "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n<div>\n <table class=\"grid dict\">\n \n \n <tr>\n <th scope=\"row\"><b>Title: </b></th>\n <td style=\"padding-left: 4px;\">Unique Patient Addresses</td>\n </tr>\n \n\n \n \n <tr>\n <th scope=\"row\"><b>Id: </b></th>\n <td style=\"padding-left: 4px;\">UniquePatientAddressesQuery</td>\n </tr>\n \n\n \n \n <tr>\n <th scope=\"row\"><b>Version: </b></th>\n <td style=\"padding-left: 4px;\">2.1.0-pre</td>\n </tr>\n \n\n \n <tr>\n <th scope=\"row\"><b>Url: </b></th>\n <td style=\"padding-left: 4px;\"><a href=\"Library-UniquePatientAddressesQuery.html\">Unique Patient Addresses</a></td>\n </tr>\n \n\n \n <tr>\n <th scope=\"row\">\n <b>\n \n \n \n </b>\n </th>\n <td style=\"padding-left: 4px;\">\n \n <p style=\"margin-bottom: 5px;\">\n <span>urn:oid:2.16.840.1.113883.4.642.40.77.28.5</span>\n </p>\n \n </td>\n </tr>\n \n\n \n\n \n\n \n\n \n <tr>\n <th scope=\"row\"><b>Type: </b></th>\n <td style=\"padding-left: 4px;\">\n \n \n \n <p style=\"margin-bottom: 5px;\">\n <b>system: </b> <span><a href=\"CodeSystem-LibraryTypesCodes.html\">http://hl7.org/fhir/uv/sql-on-fhir/CodeSystem/LibraryTypesCodes</a></span>\n </p>\n \n \n <p style=\"margin-bottom: 5px;\">\n <b>code: </b> <span>sql-query</span>\n </p>\n \n \n \n \n \n </td>\n </tr>\n \n\n \n\n \n <tr>\n <th scope=\"row\"><b>Date: </b></th>\n <td style=\"padding-left: 4px;\">2026-07-28 04:38:44+0000</td>\n </tr>\n \n\n \n <tr>\n <th scope=\"row\"><b>Publisher: </b></th>\n <td style=\"padding-left: 4px;\">HL7 International / FHIR Infrastructure</td>\n </tr>\n \n\n \n <tr>\n <th scope=\"row\"><b>Description: </b></th>\n <td style=\"padding-left: 4px;\"><div><p>Retrieves each patient's most recent address in a given city. Includes two\ndialects for the same logical query.</p>\n<p><strong>Standard SQL:</strong></p>\n<pre><code class=\"language-sql\">WITH ranked_addresses AS (\n SELECT\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code,\n patient_address_view.updated_at,\n ROW_NUMBER() OVER (\n PARTITION BY patient_view.id\n ORDER BY patient_address_view.updated_at DESC, patient_address_view.address_id DESC\n ) AS address_rank\n FROM patient_view\n JOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\n WHERE patient_view.active = true\n AND patient_address_view.city = :city\n)\nSELECT\n patient_id,\n name,\n address_line1,\n city,\n state,\n postal_code\nFROM ranked_addresses\nWHERE address_rank = 1\n</code></pre>\n<p><strong>PostgreSQL dialect:</strong></p>\n<pre><code class=\"language-sql\">SELECT DISTINCT ON (patient_view.id)\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code\nFROM patient_view\nJOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\nWHERE patient_view.active = true\n AND patient_address_view.city = :city\nORDER BY patient_view.id, patient_address_view.updated_at DESC, patient_address_view.address_id DESC\n</code></pre>\n</div></td>\n </tr>\n \n\n \n\n \n\n \n\n \n <tr>\n <th scope=\"row\"><b>Jurisdiction: </b></th>\n <td style=\"padding-left: 4px;\">001</td>\n </tr>\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n <tr>\n <th scope=\"row\"><b>Related Artifacts: </b></th>\n <td style=\"padding-left: 4px;\">\n \n \n \n <p><b>Dependencies</b></p>\n <ul>\n \n <li><code>https://example.org/ViewDefinition/patient_view</code></li>\n \n <li><code>https://example.org/ViewDefinition/patient_address_view</code></li>\n \n </ul>\n \n \n \n \n \n </td>\n </tr>\n \n\n \n <tr>\n <th scope=\"row\"><b>Parameters: </b></th>\n <td style=\"padding-left: 4px;\">\n <table class=\"grid-dict\">\n <tr><th><b>Name</b></th><th><b>Type</b></th><th><b>Min</b></th><th><b>Max</b></th><th><b>In/Out</b></th></tr>\n \n <tr><th>city</th><th>string</th><th/><th/><th>In</th></tr>\n \n </table>\n </td>\n </tr>\n \n\n \n\n \n \n \n <tr>\n <td colspan=\"2\">\n <table>\n <tr><th><b>Content: </b> application/sql</th></tr>\n <tr><td><pre><code>Encoded data (1000 characters)</code></pre></td></tr>\n </table>\n </td>\n </tr>\n \n \n \n \n <tr>\n <td colspan=\"2\">\n <table>\n <tr><th><b>Content: </b> application/sql;dialect=postgresql</th></tr>\n <tr><td><pre><code>Encoded data (660 characters)</code></pre></td></tr>\n </table>\n </td>\n </tr>\n \n \n </table>\n</div>\n</div>"
},
"url" : "http://hl7.org/fhir/uv/sql-on-fhir/Library/UniquePatientAddressesQuery",
"identifier" : [
{
"system" : "urn:ietf:rfc:3986",
"value" : "urn:oid:2.16.840.1.113883.4.642.40.77.28.5"
}
],
"version" : "2.1.0-pre",
"name" : "UniquePatientAddressesQuery",
"title" : "Unique Patient Addresses",
"status" : "active",
"type" : {
"coding" : [
{
"system" : "http://hl7.org/fhir/uv/sql-on-fhir/CodeSystem/LibraryTypesCodes",
"code" : "sql-query"
}
]
},
"date" : "2026-07-28T04:38:44+00:00",
"publisher" : "HL7 International / FHIR Infrastructure",
"contact" : [
{
"name" : "HL7 International / FHIR Infrastructure",
"telecom" : [
{
"system" : "url",
"value" : "http://www.hl7.org/Special/committees/fiwg"
}
]
}
],
"description" : "Retrieves each patient's most recent address in a given city. Includes two\ndialects for the same logical query.\n\n**Standard SQL:**\n```sql\nWITH ranked_addresses AS (\n SELECT\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code,\n patient_address_view.updated_at,\n ROW_NUMBER() OVER (\n PARTITION BY patient_view.id\n ORDER BY patient_address_view.updated_at DESC, patient_address_view.address_id DESC\n ) AS address_rank\n FROM patient_view\n JOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\n WHERE patient_view.active = true\n AND patient_address_view.city = :city\n)\nSELECT\n patient_id,\n name,\n address_line1,\n city,\n state,\n postal_code\nFROM ranked_addresses\nWHERE address_rank = 1\n```\n\n**PostgreSQL dialect:**\n```sql\nSELECT DISTINCT ON (patient_view.id)\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code\nFROM patient_view\nJOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\nWHERE patient_view.active = true\n AND patient_address_view.city = :city\nORDER BY patient_view.id, patient_address_view.updated_at DESC, patient_address_view.address_id DESC\n```",
"jurisdiction" : [
{
"coding" : [
{
"system" : "http://unstats.un.org/unsd/methods/m49/m49.htm",
"code" : "001",
"display" : "World"
}
]
}
],
"relatedArtifact" : [
{
"type" : "depends-on",
"label" : "patient_view",
"resource" : "https://example.org/ViewDefinition/patient_view"
},
{
"type" : "depends-on",
"label" : "patient_address_view",
"resource" : "https://example.org/ViewDefinition/patient_address_view"
}
],
"parameter" : [
{
"name" : "city",
"use" : "in",
"documentation" : "City to filter addresses",
"type" : "string"
}
],
"content" : [
{
"extension" : [
{
"url" : "http://hl7.org/fhir/uv/sql-on-fhir/StructureDefinition/sql-text",
"valueString" : "WITH ranked_addresses AS (\n SELECT\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code,\n patient_address_view.updated_at,\n ROW_NUMBER() OVER (\n PARTITION BY patient_view.id\n ORDER BY patient_address_view.updated_at DESC, patient_address_view.address_id DESC\n ) AS address_rank\n FROM patient_view\n JOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\n WHERE patient_view.active = true\n AND patient_address_view.city = :city\n)\nSELECT\n patient_id,\n name,\n address_line1,\n city,\n state,\n postal_code\nFROM ranked_addresses\nWHERE address_rank = 1"
}
],
"contentType" : "application/sql",
"data" : "V0lUSCByYW5rZWRfYWRkcmVzc2VzIEFTICgKICBTRUxFQ1QKICAgIHBhdGllbnRfdmlldy5pZCBBUyBwYXRpZW50X2lkLAogICAgcGF0aWVudF92aWV3Lm5hbWUsCiAgICBwYXRpZW50X2FkZHJlc3Nfdmlldy5hZGRyZXNzX2xpbmUxLAogICAgcGF0aWVudF9hZGRyZXNzX3ZpZXcuY2l0eSwKICAgIHBhdGllbnRfYWRkcmVzc192aWV3LnN0YXRlLAogICAgcGF0aWVudF9hZGRyZXNzX3ZpZXcucG9zdGFsX2NvZGUsCiAgICBwYXRpZW50X2FkZHJlc3Nfdmlldy51cGRhdGVkX2F0LAogICAgUk9XX05VTUJFUigpIE9WRVIgKAogICAgICBQQVJUSVRJT04gQlkgcGF0aWVudF92aWV3LmlkCiAgICAgIE9SREVSIEJZIHBhdGllbnRfYWRkcmVzc192aWV3LnVwZGF0ZWRfYXQgREVTQywgcGF0aWVudF9hZGRyZXNzX3ZpZXcuYWRkcmVzc19pZCBERVNDCiAgICApIEFTIGFkZHJlc3NfcmFuawogIEZST00gcGF0aWVudF92aWV3CiAgSk9JTiBwYXRpZW50X2FkZHJlc3NfdmlldwogICAgT04gcGF0aWVudF92aWV3LmlkID0gcGF0aWVudF9hZGRyZXNzX3ZpZXcucGF0aWVudF9pZAogIFdIRVJFIHBhdGllbnRfdmlldy5hY3RpdmUgPSB0cnVlCiAgICBBTkQgcGF0aWVudF9hZGRyZXNzX3ZpZXcuY2l0eSA9IDpjaXR5CikKU0VMRUNUCiAgcGF0aWVudF9pZCwKICBuYW1lLAogIGFkZHJlc3NfbGluZTEsCiAgY2l0eSwKICBzdGF0ZSwKICBwb3N0YWxfY29kZQpGUk9NIHJhbmtlZF9hZGRyZXNzZXMKV0hFUkUgYWRkcmVzc19yYW5rID0gMQ=="
},
{
"extension" : [
{
"url" : "http://hl7.org/fhir/uv/sql-on-fhir/StructureDefinition/sql-text",
"valueString" : "SELECT DISTINCT ON (patient_view.id)\n patient_view.id AS patient_id,\n patient_view.name,\n patient_address_view.address_line1,\n patient_address_view.city,\n patient_address_view.state,\n patient_address_view.postal_code\nFROM patient_view\nJOIN patient_address_view\n ON patient_view.id = patient_address_view.patient_id\nWHERE patient_view.active = true\n AND patient_address_view.city = :city\nORDER BY patient_view.id, patient_address_view.updated_at DESC, patient_address_view.address_id DESC"
}
],
"contentType" : "application/sql;dialect=postgresql",
"data" : "U0VMRUNUIERJU1RJTkNUIE9OIChwYXRpZW50X3ZpZXcuaWQpCiAgcGF0aWVudF92aWV3LmlkIEFTIHBhdGllbnRfaWQsCiAgcGF0aWVudF92aWV3Lm5hbWUsCiAgcGF0aWVudF9hZGRyZXNzX3ZpZXcuYWRkcmVzc19saW5lMSwKICBwYXRpZW50X2FkZHJlc3Nfdmlldy5jaXR5LAogIHBhdGllbnRfYWRkcmVzc192aWV3LnN0YXRlLAogIHBhdGllbnRfYWRkcmVzc192aWV3LnBvc3RhbF9jb2RlCkZST00gcGF0aWVudF92aWV3CkpPSU4gcGF0aWVudF9hZGRyZXNzX3ZpZXcKICBPTiBwYXRpZW50X3ZpZXcuaWQgPSBwYXRpZW50X2FkZHJlc3Nfdmlldy5wYXRpZW50X2lkCldIRVJFIHBhdGllbnRfdmlldy5hY3RpdmUgPSB0cnVlCiAgQU5EIHBhdGllbnRfYWRkcmVzc192aWV3LmNpdHkgPSA6Y2l0eQpPUkRFUiBCWSBwYXRpZW50X3ZpZXcuaWQsIHBhdGllbnRfYWRkcmVzc192aWV3LnVwZGF0ZWRfYXQgREVTQywgcGF0aWVudF9hZGRyZXNzX3ZpZXcuYWRkcmVzc19pZCBERVND"
}
]
}