Eseguire una query mysql contenente totali, subtotali e totale generale

E’ possibile estrarre in una unica query totali e subtotali utilizzando l’operatore WITH ROLLUP.  I campi di cui viene mostrato il subtotale vengono riportati con valore null.

Di seguito un esempio che recupera le visite del blog nel mese di maggio suddivise per os e browser con totale generale e subtotale per os.

SELECT os, browser, COUNT( * )
FROM `wp_statpress`
WHERE os <> ''
AND browser <> ''
AND DATE >= "20090501"
AND DATE <= "20090531"
GROUP BY os, browser
WITH ROLLUP
os browser count( * )
Debian Linux Epiphany 2
Debian Linux Firefox 3 44
Debian Linux Iceweasel 209
Debian Linux Internet Explorer 7 3
Debian Linux Links 1
Debian Linux NULL 259
iPhone Safari 20
iPhone NULL 20
Linux Chrome 2
Linux Epiphany 2
Linux Firefox 11
Linux Firefox 2 36
Linux Firefox 3 335
Linux Generic Gecko 26
Linux Iceweasel 2
Linux Konqueror 16
Linux Mozilla Minefield 5
Linux Opera 20
Linux Safari 1
Linux Thunderbird 159
Linux NULL 615
Mac OS X Firefox 2 1
Mac OS X Firefox 3 151
Mac OS X Opera 2
Mac OS X Safari 154
Mac OS X NULL 308
Suse Linux Firefox 3 27
Suse Linux SeaMonkey 1
Suse Linux NULL 28
Symbian Safari 13
Symbian NULL 13
Ubuntu Linux Firefox 2 37
Ubuntu Linux Firefox 3 1058
Ubuntu Linux Mozilla Minefield 2
Ubuntu Linux NULL 1097
Windows 2000 Firefox 2 3
Windows 2000 Firefox 3 2
Windows 2000 Internet Explorer 5 4
Windows 2000 Internet Explorer 6 67
Windows 2000 Opera 4
Windows 2000 NULL 80
Windows 95 Internet Explorer 4 341
Windows 95 Internet Explorer 5 4
Windows 95 NULL 345
Windows CE Internet Explorer 4 1
Windows CE Internet Explorer 6 2
Windows CE NULL 3
Windows ME Internet Explorer 6 9
Windows ME NULL 9
Windows NT 4 Chrome 2
Windows NT 4 Firefox 3 68
Windows NT 4 Internet Explorer 1
Windows NT 4 Internet Explorer 5 2
Windows NT 4 Internet Explorer 6 3
Windows NT 4 Internet Explorer 8 22
Windows NT 4 Opera 1
Windows NT 4 NULL 99
Windows Server 2003 Firefox 3 17
Windows Server 2003 Internet Explorer 6 18
Windows Server 2003 Internet Explorer 7 51
Windows Server 2003 SeaMonkey 1
Windows Server 2003 NULL 87
Windows Vista Chrome 48
Windows Vista Firefox 2 11
Windows Vista Firefox 3 403
Windows Vista Internet Explorer 7 87
Windows Vista Internet Explorer 8 65
Windows Vista Safari 2
Windows Vista NULL 616
Windows XP Chrome 78
Windows XP Firefox 4
Windows XP Firefox 2 357
Windows XP Firefox 3 1817
Windows XP Generic Gecko 1
Windows XP Internet Explorer 6 3123
Windows XP Internet Explorer 7 780
Windows XP Internet Explorer 8 140
Windows XP K-Meleon 1
Windows XP Opera 144
Windows XP Thunderbird 28
Windows XP NULL 6473
NULL NULL 10052
os browser count( * )
Debian Linux Epiphany 2
Debian Linux Firefox 3 44
Debian Linux Iceweasel 209
Debian Linux Internet Explorer 7 3
Debian Linux Links 1
Debian Linux NULL 259
iPhone Safari 20
iPhone NULL 20
Linux Chrome 2
Linux Epiphany 2
Linux Firefox 11
Linux Firefox 2 36
Linux Firefox 3 335
Linux Generic Gecko 26
Linux Iceweasel 2
Linux Konqueror 16
Linux Mozilla Minefield 5
Linux Opera 20
Linux Safari 1
Linux Thunderbird 159
Linux NULL 615
Mac OS X Firefox 2 1
Mac OS X Firefox 3 151
Mac OS X Opera 2
Mac OS X Safari 154
Mac OS X NULL 308
Suse Linux Firefox 3 27
Suse Linux SeaMonkey 1
Suse Linux NULL 28
Symbian Safari 13
Symbian NULL 13
Ubuntu Linux Firefox 2 37
Ubuntu Linux Firefox 3 1058
Ubuntu Linux Mozilla Minefield 2
Ubuntu Linux NULL 1097
Windows 2000 Firefox 2 3
Windows 2000 Firefox 3 2
Windows 2000 Internet Explorer 5 4
Windows 2000 Internet Explorer 6 67
Windows 2000 Opera 4
Windows 2000 NULL 80
Windows 95 Internet Explorer 4 341
Windows 95 Internet Explorer 5 4
Windows 95 NULL 345
Windows CE Internet Explorer 4 1
Windows CE Internet Explorer 6 2
Windows CE NULL 3
Windows ME Internet Explorer 6 9
Windows ME NULL 9
Windows NT 4 Chrome 2
Windows NT 4 Firefox 3 68
Windows NT 4 Internet Explorer 1
Windows NT 4 Internet Explorer 5 2
Windows NT 4 Internet Explorer 6 3
Windows NT 4 Internet Explorer 8 22
Windows NT 4 Opera 1
Windows NT 4 NULL 99
Windows Server 2003 Firefox 3 17
Windows Server 2003 Internet Explorer 6 18
Windows Server 2003 Internet Explorer 7 51
Windows Server 2003 SeaMonkey 1
Windows Server 2003 NULL 87
Windows Vista Chrome 48
Windows Vista Firefox 2 11
Windows Vista Firefox 3 403
Windows Vista Internet Explorer 7 87
Windows Vista Internet Explorer 8 65
Windows Vista Safari 2
Windows Vista NULL 616
Windows XP Chrome 78
Windows XP Firefox 4
Windows XP Firefox 2 357
Windows XP Firefox 3 1817
Windows XP Generic Gecko 1
Windows XP Internet Explorer 6 3123
Windows XP Internet Explorer 7 780
Windows XP Internet Explorer 8 140
Windows XP K-Meleon 1
Windows XP Opera 144
Windows XP Thunderbird 28
Windows XP NULL 6473
NULL NULL 10052

Per approfondimenti consultare i seguenti link:

Un pensiero su “Eseguire una query mysql contenente totali, subtotali e totale generale

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.