Itgensql056 DATEPART e GROUP BY

Abbiamo un problema con la seguente istruzione:

select DATEPART("week", dateWorked) as kw
,      sum(hoursworked) as abrechenbar
from   Autotask.TimeEntries.TimeEntries 
where  (dateWorked between '01-01-2023' and '12-31-2023') 
and    isnonbillable = 0
group 
by     DATEPART("week", dateWorked);

Messaggio di errore:

itgensql056:
Errore di sintassi tra i due '’ sulla riga 3, colonna 17:
select DATEPART(“week”…e = 0group by DATEPART
(***“week”, dateWorked)order by DATE…
Errore: input non corrispondente ‘(’ ci si aspettava {, ‘;’}.

Versione: 23.0.114-INTERNAL+4399

L’errore itgensql056 si verifica perché Invantive UniversalSQL (purtroppo) non consente l’uso di funzioni SQL con operatori di gruppo in un unico passaggio.

La soluzione tipica è utilizzare una vista inline, che porta a:

select kw
,      sum(abrechenbar) as abrechenbar
from   ( select DATEPART("week", dateWorked) kw
         ,      hoursworked abrechenbar
         from   Autotask.TimeEntries.TimeEntries 
         where  (dateWorked between to_date('20230101', 'YYYYMMDD') and to_date('20231231', 'YYYYMMDD')) 
         and    isnonbillable = 0
       )
group 
by     kw

In questa query anche il formato della data è stato reso fisso per evitare di affidarsi a conversioni di tipo di dati implicite.

Grazie mille, funziona perfettamente.