A raiz de la consulta que hice hace unos dias (mysql pivotar tabla resultado de campos json) me he encontrado con otro problema. Resulta que tengo que obtener unos campos de varias union y dividirlo por el total de elementos que tiene el union.
actualmente tengo:
select
concat("assembled") as field, concat("1") as veces,
count(if(json_extract(filters, '$.assembled')= true,json_extract(filters, '$.assembled'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("windows") as field,concat("1") as veces,
count(if(json_extract(filters, '$.osWindows')= true,json_extract(filters, '$.osWindows'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("linux") as field,concat("1") as veces,
count(if(json_extract(filters, '$.osLinux')= true,json_extract(filters, '$.osLinux'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("mac") as field,concat("1") as veces,
count(if(json_extract(filters, '$.osMac')= true,json_extract(filters, '$.osMac'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("sdcard") as field,concat("1") as veces,
count(if(json_extract(filters, '$.sdCard')= true,json_extract(filters, '$.sdCard'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("usb") as field,concat("1") as veces,
count(if(json_extract(filters, '$.usb')= true,json_extract(filters, '$.usb'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("ethernet") as field,concat("1") as veces,
count(if(json_extract(filters, '$.ethernet')= true,json_extract(filters, '$.ethernet'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("wifi") as field,concat("1") as veces,
count(if(json_extract(filters, '$.wifi')= true,json_extract(filters, '$.wifi'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("bluetooth") as field,concat("1") as veces,
count(if(json_extract(filters, '$.bluetooth')= true,json_extract(filters, '$.bluetooth'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("Display") as field,concat("1") as veces,
count(if(json_extract(filters, '$.integratedDisplay')= true,json_extract(filters, '$.integratedDisplay'),NULL))as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("touchable") as field,concat("1") as veces,
count(if(json_extract(filters, '$.touchableDisplay')= true,json_extract(filters, '$.touchableDisplay'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("Camera") as field,concat("1") as veces,
count(if(json_extract(filters, '$.integratedCamera')= true,json_extract(filters, '$.integratedCamera'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("Scanner") as field,concat("1") as veces,
count(if(json_extract(filters, '$.builtinScanner')= true,json_extract(filters, '$.builtinScanner'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("mobileApp") as field,concat("1") as veces,
count(if(json_extract(filters, '$.mobileApp')= true,json_extract(filters, '$.mobileApp'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("Computer") as field,concat("1") as veces,
count(if(json_extract(filters, '$.integratedComputer')= true,json_extract(filters, '$.integratedComputer'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("closedFrame") as field,concat("1") as veces,
count(if(json_extract(filters, '$.closedFrame')= true,json_extract(filters, '$.closedFrame'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("securityLock") as field,concat("1") as veces,
count(if(json_extract(filters, '$.securityLock')= true,json_extract(filters, '$.securityLock'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("emergencyStop") as field,concat("1") as veces,
count(if(json_extract(filters, '$.emergencyStop')= true,json_extract(filters, '$.emergencyStop'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
union
select
concat("innerLight") as field,concat("1") as veces,
count(if(json_extract(filters, '$.innerLight')= true,json_extract(filters, '$.innerLight'),NULL)) as cuenta
from bigdata_filtered group by concat(ip, filtered_date)
Esto me genera una tabla con los siguientes datos:
+---------------+-------+--------+
| field | veces | cuenta |
+---------------+-------+--------+
| assembled | 1 | 1 |
| assembled | 1 | 9 |
| assembled | 1 | 0 |
| assembled | 1 | 8 |
| assembled | 1 | 4 |
| assembled | 1 | 6 |
| assembled | 1 | 5 |
| assembled | 1 | 7 |
| assembled | 1 | 17 |
| assembled | 1 | 11 |
| assembled | 1 | 3 |
| assembled | 1 | 14 |
| assembled | 1 | 13 |
| assembled | 1 | 15 |
| assembled | 1 | 2 |
| windows | 1 | 0 |
| windows | 1 | 9 |
| windows | 1 | 25 |
| windows | 1 | 2 |
| windows | 1 | 6 |
| windows | 1 | 5 |
| windows | 1 | 11 |
| windows | 1 | 1 |
| windows | 1 | 8 |
| windows | 1 | 4 |
| linux | 1 | 0 |
| linux | 1 | 4 |
| linux | 1 | 11 |
| linux | 1 | 1 |
| linux | 1 | 9 |
| linux | 1 | 8 |
| mac | 1 | 0 |
| mac | 1 | 9 |
| mac | 1 | 11 |
| mac | 1 | 1 |
| mac | 1 | 8 |
| sdcard | 1 | 0 |
| sdcard | 1 | 9 |
| sdcard | 1 | 11 |
| sdcard | 1 | 1 |
| sdcard | 1 | 8 |
| usb | 1 | 0 |
| usb | 1 | 8 |
| usb | 1 | 9 |
| usb | 1 | 20 |
| usb | 1 | 5 |
| usb | 1 | 7 |
| usb | 1 | 4 |
| usb | 1 | 1 |
| usb | 1 | 2 |
| ethernet | 1 | 0 |
| ethernet | 1 | 1 |
| ethernet | 1 | 4 |
| ethernet | 1 | 2 |
| ethernet | 1 | 5 |
| wifi | 1 | 0 |
| wifi | 1 | 5 |
| wifi | 1 | 1 |
| wifi | 1 | 2 |
| bluetooth | 1 | 0 |
| bluetooth | 1 | 1 |
| Display | 1 | 0 |
| Display | 1 | 8 |
| Display | 1 | 4 |
| Display | 1 | 7 |
| Display | 1 | 9 |
| Display | 1 | 3 |
| Display | 1 | 1 |
| Display | 1 | 13 |
| Display | 1 | 15 |
| Display | 1 | 5 |
| touchable | 1 | 0 |
| touchable | 1 | 2 |
| touchable | 1 | 1 |
| Camera | 1 | 0 |
| Scanner | 1 | 0 |
| mobileApp | 1 | 0 |
| Computer | 1 | 0 |
| closedFrame | 1 | 0 |
| closedFrame | 1 | 1 |
| securityLock | 1 | 1 |
| securityLock | 1 | 0 |
| emergencyStop | 1 | 0 |
| emergencyStop | 1 | 1 |
| innerLight | 1 | 0 |
| innerLight | 1 | 1 |
+---------------+-------+--------+
86 rows in set (0,50 sec)
Mi objetivo ahora es conseguir esta misma tabla, pero con el campo "veces" dividido por 86. Es decir, el total de elementos que resultan de la consulta... del union!!!
inicialmente he englobado toda la consulta union en un select tal que asi:
select field, veces, cuenta
from (
... // aqui va todo el select/union
) as T1;
Con esta consulta, vuelvo a tener la misma tabla anterior de resultados.Pero al intentar dividir por el total de elementos, con un
count(*):
select field, veces / count(*), cuenta from ( ... // aqui va todo el select/union
) as T1;
el resultado es:
+---------------------+-------+
| T1.field / count(*) | veces |
+---------------------+-------+
| 0 | 1 |
+---------------------+-------+
1 row in set, 1 warning (0,51 sec)
A alguien se le ocurre alguna forma de obtener el total de filas del union, y dividir un campo por ese total?
Gracias de antemano, y feliz navidad!!!