Somma delle colonne di una tabella pivot

Andreawave

Nuovo Utente
27 Gen 2014
11
0
1
Buongiorno,
ho il seguente problema: ho la seguente tabella pivot alla quale devo aggiungere una riga finale con i totali dati dalla somma di ogni colonna.
tabella.jpg

Tale tabella è stata ottenuta tramite il seguente codice:
SQL:
select detailDestinationName as 'Nome Coda',
count(*) as 'Totale Chiamate ricevute',
count(case when status = 'SERVED' then uniqueId else null end) as 'Chiamate Servite',
(count(case when status = 'SERVED' then uniqueId else null end)/count(*)*100) as '% chiamate servite',
ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/(count(*)-count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end))*100),0) as "Totale Abbandoni",
count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end) as 'Chiamate non Servite Superiori a 30 secondi',
ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0) as '% chiamate non servite superiore a 30 secondi',
count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end) as 'Chiamate non Servite Inferiori a 30 secondi',
ifnull((count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0) as '% chiamate non servite inferiore a 30 secondi'
from chiamate_report_new
group by detailDestinationId;

Non ho la minima idea di come si possa fare.
Potreste aiutarmi?
Grazie
 
puoi aggiungere la riga del totale con una union tipo
SQL:
group by detailDestinationId
union
  select 'TOTALE' as 'Nome Coda',
  sum(count(*)) as 'Totale Chiamate ricevute',
...
  from chiamate_report_new;
bada che "union" sia preceduto e seguito da uno spazio
mantieni esattamente uguali i nomi delle colonne come nella query che precede (as ….)
 
puoi aggiungere la riga del totale con una union tipo
SQL:
group by detailDestinationId
union
  select 'TOTALE' as 'Nome Coda',
  sum(count(*)) as 'Totale Chiamate ricevute',
...
  from chiamate_report_new;
bada che "union" sia preceduto e seguito da uno spazio
mantieni esattamente uguali i nomi delle colonne come nella query che precede (as ….)

In pratica nella seconda tabella devo aggiungere solamente delle sum con all'interno i count della tabella precedente, corretto?
Es
SQL:
sum(count(case when status = 'SERVED' then uniqueId else null end) as 'Chiamate Servite',)
Giusto?
 
Ho modificato la query nel seguente modo:

SQL:
select detailDestinationName as 'Nome Coda',
count(*) as 'Totale Chiamate ricevute',
count(case when status = 'SERVED' then uniqueId else null end) as 'Chiamate Servite',
(count(case when status = 'SERVED' then uniqueId else null end)/count(*)*100) as '% chiamate servite',
ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/(count(*)-count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end))*100),0) as "Totale Abbandoni",
count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end) as 'Chiamate non Servite Superiori a 30 secondi',
ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0) as '% chiamate non servite superiore a 30 secondi',
count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end) as 'Chiamate non Servite Inferiori a 30 secondi',
ifnull((count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0) as '% chiamate non servite inferiore a 30 secondi'
from chiamate_report_new
group by detailDestinationId
UNION
 select 'TOTALE' as 'Nome Coda',
  sum(count(*)) as 'Totale Chiamate ricevute',
  sum(count(case when status = 'SERVED' then uniqueId else null end)) as 'Chiamate Servite',
  sum((count(case when status = 'SERVED' then uniqueId else null end)/count(*)*100)) as '% chiamate servite',
  sum(ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/(count(*)-count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end))*100),0)) as "Totale Abbandoni",
  sum(count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)) as 'Chiamate non Servite Superiori a 30 secondi',
  sum(ifnull((count(case when status = 'NOTSERVED' and waitingTime >= 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0)) as '% chiamate non servite superiore a 30 secondi',
  sum(count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end)) as 'Chiamate non Servite Inferiori a 30 secondi',
  sum(ifnull((count(case when status = 'NOTSERVED' and waitingTime < 30 then uniqueId else null end)/count(case when status = 'NOTSERVED' then uniqueId else null end )*100),0)) as '% chiamate non servite inferiore a 30 secondi'
  from chiamate_report_new;

ma ottengo il seguente errore:
Error Code: 1111. Invalid use of group function
Cosa ho sbagliato?
 
esegui la prima select da sola ( senza union e successiva )
deve andare bene perché é quella che produce il risultato che hai visualizzato

esegui la seconda select da sola (senza la prima e senza union )
deve fornire la riga di totale, se dovesse dare errore, correggi i possibili,

quando entrambe le select danno il risultato atteso, uniscile con union, avendo accortezza agli spazi come detto

il mio sistema, non evidenzia errori di sintassi nelle 2 select unite ( con i dovuti spazi )
 
Ho risolto, grazie infinite!
In pratica ciò che generava l'errore era l'aver inserito la funzione "count" all'interno della funzione "sum".

Ho risolto semplicemente sostituendo l'espressione:
SQL:
sum(count(*)) as 'Totale Chiamate ricevute'
..........................
con l'espressione:
SQL:
count(*) as 'Totale Chiamate ricevute',
....etc......
 

Discussioni simili