Documentazione Invantive
Forum di supporto InvantiveAccedi / Registrati

Funzione di aggregazione Last

Sintassi

%%{init: {
  'theme': 'base',
  'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
  'themeVariables': {
    'fontSize': '11px',
    'fontFamily': 'Arial'
  }
}}%%
flowchart LR
    lastAggregateFunction_start((START))
    lastAggregateFunction_start --> lastAggregateFunction_0_0[LAST]:::quoted
    lastAggregateFunction_0_0 --> lastAggregateFunction_0_1["("]:::quoted
    lastAggregateFunction_0_1 --> lastAggregateFunction_0_2[<a href="Invantive UniversalSQL/Grammar/Expression" class="internal-link">expression</a>]
    lastAggregateFunction_0_2 --> lastAggregateFunction_0_3[")"]:::quoted
    lastAggregateFunction_0_3 --> lastAggregateFunction_end((END))

Scopo

Funzione di gruppo che restituisce l'ultimo valore diverso da null in un insieme di risultati ordinato. L'ordine delle righe è determinato dalla sorgente; combinarlo con un order by in una query annidata per ottenere un risultato deterministico.

Esempi

Il seguente esempio recupera l'ultima persona di contatto da un elenco di contatti, nell'ordine in cui i contatti sono memorizzati:

select last(cnt.contact_name)
from   csvtable
       ( passing 'Acme Industries#Alice Johnson#CFO|Acme Industries#Tom Peters#Accounts Payable|Bergmann GmbH#Erika Weber#Controller|Carlson Ltd#James Smith#CFO|Dubois SA#Marie Dupont#Accounts Payable'
         row delimiter '|'
         column delimiter '#'
         columns customer_name varchar2 position next
         ,       contact_name  varchar2 position next
         ,       role          varchar2 position next
       ) cnt

-------------------
Marie Dupont

Il seguente esempio recupera per cliente la persona di contatto alfabeticamente ultima:

select srt.customer_name
,      last(srt.contact_name)
from   ( select cnt.customer_name
         ,      cnt.contact_name
         from   csvtable
                ( passing 'Acme Industries#Alice Johnson#CFO|Acme Industries#Tom Peters#Accounts Payable|Bergmann GmbH#Erika Weber#Controller|Carlson Ltd#James Smith#CFO|Dubois SA#Marie Dupont#Accounts Payable'
                  row delimiter '|'
                  column delimiter '#'
                  columns customer_name varchar2 position next
                  ,       contact_name  varchar2 position next
                  ,       role          varchar2 position next
                ) cnt
         order
         by     cnt.customer_name
         ,      cnt.contact_name
       ) srt
group
by     srt.customer_name
order
by     srt.customer_name

-------------------
Acme Industries    Tom Peters
Bergmann GmbH      Erika Weber
Carlson Ltd        James Smith
Dubois SA          Marie Dupont