@prefix fhir: <http://hl7.org/fhir/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

# - resource -------------------------------------------------------------------

<http://hl7.org/fhir/OperationDefinition/Observation-lastn> a fhir:OperationDefinition ;
  fhir:nodeRole fhir:treeRoot ;
  fhir:id [ fhir:v "Observation-lastn"] ; # 
  fhir:text [
     fhir:status [ fhir:v "generated" ] ;
     fhir:div [ fhir:v "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p class=\"res-header-id\"><b>Generated Narrative: OperationDefinition Observation-lastn</b></p><a name=\"Observation-lastn\"> </a><a name=\"hcObservation-lastn\"> </a><p>URL: [base]/Observation/$lastn</p><h3>Parameters</h3><table class=\"grid\"><tr><td><b>Use</b></td><td><b>Name</b></td><td><b>Scope</b></td><td><b>Cardinality</b></td><td><b>Type</b></td><td><b>Binding</b></td><td><b>Documentation</b></td></tr><tr><td>IN</td><td>max</td><td/><td>0..1</td><td><a href=\"datatypes.html#positiveInt\">positiveInt</a></td><td/><td><div><p><code>max</code> is an optional input parameter to the <em>lastn</em> query operation. It is used to specify the maximum number of Observations to return from each group. For example for the query \"Fetch the last 3 results for all vitals for a patient\" <code>max</code> = 3.</p>\n</div></td></tr><tr><td>OUT</td><td>return</td><td/><td>1..1</td><td><a href=\"bundle.html\">Bundle</a></td><td/><td><div><p>The set of most recent N Observations that match the <em>lastn</em> query search criteria.</p>\n</div></td></tr></table><div><p>The key differences between this query operation and simply searching Observation using the combination of <code>_count</code> and <code>_sort</code> parameters are:</p>\n<ul>\n<li>The <em>lastn</em> query returns <strong>only</strong> the last N resource grouped by code. Using the _count query method doesn't restrict the total matches so you may need to page through several \"A\" Observations before getting to Observation \"B\".</li>\n<li>The server is responsible for grouping the observations by codes. This frees the client from needing to determine which codes she should ask for.</li>\n</ul>\n<p>This operation cannot be performed on observations that the user is not authorized to see. It is assumed that the server has identified and secured the context appropriately, and can either associate the authorization context with a single patient, or determine whether the context has the rights to the nominated patient, if there is one. If there is no nominated patient (e.g. the operation is invoked at the system level) and the context is not associated with a single patient record, then the server should return an error. Specifying the relationship between the context, a user and patient records is outside the scope of this specification.</p>\n</div></div>"^^rdf:XMLLiteral ]
  ] ; # 
  fhir:extension ( [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/structuredefinition-fmm"^^xsd:anyURI ;
       fhir:l <http://hl7.org/fhir/StructureDefinition/structuredefinition-fmm>
     ] ;
     fhir:value [
       a fhir:Integer ;
       fhir:v 3
     ]
  ] [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/structuredefinition-standards-status"^^xsd:anyURI ;
       fhir:l <http://hl7.org/fhir/StructureDefinition/structuredefinition-standards-status>
     ] ;
     fhir:value [
       a fhir:Code ;
       fhir:v "normative"
     ]
  ] [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg"^^xsd:anyURI ;
       fhir:l <http://hl7.org/fhir/StructureDefinition/structuredefinition-wg>
     ] ;
     fhir:value [
       a fhir:Code ;
       fhir:v "oo"
     ]
  ] ) ; # 
  fhir:url [
     fhir:v "http://hl7.org/fhir/OperationDefinition/Observation-lastn"^^xsd:anyURI ;
     fhir:l <http://hl7.org/fhir/OperationDefinition/Observation-lastn>
  ] ; # 
  fhir:version [ fhir:v "6.0.0-ballot4"] ; # 
  fhir:name [ fhir:v "Lastn"] ; # 
  fhir:title [ fhir:v "Last N Observations Query"] ; # 
  fhir:status [ fhir:v "active"] ; # 
  fhir:kind [ fhir:v "operation"] ; # 
  fhir:experimental [ fhir:v false] ; # 
  fhir:date [ fhir:v "2026-06-25T05:29:09+00:00"^^xsd:dateTime] ; # 
  fhir:publisher [ fhir:v "HL7 International / Orders and Observations"] ; # 
  fhir:contact ( [
     fhir:telecom ( [
       fhir:system [ fhir:v "url" ] ;
       fhir:value [ fhir:v "http://hl7.org/fhir" ]
     ] [
       fhir:system [ fhir:v "email" ] ;
       fhir:value [ fhir:v "fhir@lists.hl7.org" ]
     ] )
  ] [
     fhir:telecom ( [
       fhir:system [ fhir:v "url" ] ;
       fhir:value [ fhir:v "http://www.hl7.org/Special/committees/orders" ]
     ] )
  ] ) ; # 
  fhir:description [ fhir:v "The *lastn query* meets the common need for searching for the most recent or last n=number of observations for a subject. For example, retrieving the last 5 temperatures for a patient to view trends or fetching the most recent laboratory results or vitals signs. To ask a server to return the last n=number of observations, the *lastn* query uses the [normal search parameters](observation.html#search) defined for the Observation resource. However, rather than their normal use, they are interpreted as inputs - i.e.. instead of requiring that the resources literally contain the search parameters, they are passed to a server algorithm of some kind that uses them to determine the most appropriate matches.\n\nThe request for a lastn query SHALL include:\n\n* A `$lastn` operation parameter\n* A subject using either the `patient` or `subject` search parameter\n* A `category` parameter and/or a search parameter that contains a code element in its FHIRpath expression. ( e.g., `code` or `code-value-concept`)\n\nThe request for a lastn query MAY include:\n\n* Other Observation search parameters and modifiers\n\nThe response from a lastn query is a set of observations:\n\n* Filtered by additional parameters\n   * If not explicitly filtered by status then will include statuses of 'entered-in-error'\n* Ordered first by “equivalent” `Observation.code` value\n   * Codes SHALL be considered equivalent if the `coding.value` *and* `coding.system` are the same.\n   * Text only codes SHALL be treated and grouped based on the text.\n   * For codes with translations (multiple codings), the code translations are assumed to be equal and the grouping by code SHALL follow the transitive property of equality. For example:\n\n  |Observation.code for observation a|Observation.code for observation b|Observation.code for observation c|number of groups [codes/text in each group]|\n  |---|---|---|---|\n  |a|b|c | 3 [a],[b],[c]|\n  |a|b|a,c | 2 [a.c],[b]|\n  |a|b|a,b | 1 [a,b]|\n  |'textM'|'Text'|'t e x t'|3 ['text'],['Text'],['t e x t']|\n\n* The ordering of the “equivalent” code groups is not specified.\n* Ordered secondly (within each code group) from most recent effective time to the oldest effective time. For further guidance, refer here on [sorting](https://www.hl7.org/fhir/search.html#_sort).\n* Limited to the number of requested responses per group specified by the optional *max* query parameter\n  * In case of a tie—when the effective times for more than one Observation in the same code group—both will be returned. Therefore, more Observations may be returned than is specified in *max*. For example, 4 Observations instead of 3 if the 3rd and 4th most recent observation had the same effective time.\n* If no maximum number is given then only the most recent Observation in each group is returned.\n\nNote that the individual Observation.code values used for grouping might not be explicit in the request, for example if a category of Observations is requested, or if a code is specified with the `:below` modifier.\n\nThe set of returned observations should represent distinct real-world observations and not the same observation with changes in status or versions. If there are no matches, the *lastn* query SHALL return an empty search set with no error, but may include an operation outcome with further advice."] ; # 
  fhir:jurisdiction ( [
     fhir:coding ( [
       fhir:system [
         fhir:v "http://unstats.un.org/unsd/methods/m49/m49.htm"^^xsd:anyURI ;
         fhir:l <http://unstats.un.org/unsd/methods/m49/m49.htm>
       ] ;
       fhir:code [ fhir:v "001" ] ;
       fhir:display [ fhir:v "World" ]
     ] )
  ] ) ; # 
  fhir:affectsState [ fhir:v false] ; # 
  fhir:code [ fhir:v "lastn"] ; # 
  fhir:comment [ fhir:v "The key differences between this query operation and simply searching Observation using the combination of `_count` and `_sort` parameters are:\r\r* The *lastn* query returns **only** the last N resource grouped by code. Using the _count query method doesn't restrict the total matches so you may need to page through several \"A\" Observations before getting to Observation \"B\".\r* The server is responsible for grouping the observations by codes. This frees the client from needing to determine which codes she should ask for.\r\rThis operation cannot be performed on observations that the user is not authorized to see. It is assumed that the server has identified and secured the context appropriately, and can either associate the authorization context with a single patient, or determine whether the context has the rights to the nominated patient, if there is one. If there is no nominated patient (e.g. the operation is invoked at the system level) and the context is not associated with a single patient record, then the server should return an error. Specifying the relationship between the context, a user and patient records is outside the scope of this specification."] ; # 
  fhir:resource ( [
     fhir:v "Observation"^^xsd:anyURI ;
     fhir:l fhir:Observation
  ] ) ; # 
  fhir:system [ fhir:v false] ; # 
  fhir:type [ fhir:v true] ; # 
  fhir:instance [ fhir:v false] ; # 
  fhir:parameter ( [
     fhir:name [ fhir:v "max" ] ;
     fhir:use [ fhir:v "in" ] ;
     fhir:min [ fhir:v "0"^^xsd:nonNegativeInteger ] ;
     fhir:max [ fhir:v "1" ] ;
     fhir:documentation [ fhir:v "`max` is an optional input parameter to the *lastn* query operation. It is used to specify the maximum number of Observations to return from each group. For example for the query \"Fetch the last 3 results for all vitals for a patient\" `max` = 3." ] ;
     fhir:type [
       fhir:v "positiveInt"^^xsd:anyURI ;
       fhir:l fhir:positiveInt
     ]
  ] [
     fhir:name [ fhir:v "return" ] ;
     fhir:use [ fhir:v "out" ] ;
     fhir:min [ fhir:v "1"^^xsd:nonNegativeInteger ] ;
     fhir:max [ fhir:v "1" ] ;
     fhir:documentation [ fhir:v "The set of most recent N Observations that match the *lastn* query search criteria." ] ;
     fhir:type [
       fhir:v "Bundle"^^xsd:anyURI ;
       fhir:l fhir:Bundle
     ]
  ] ) . # 

# -------------------------------------------------------------------------------------

