Following the query I made a few days ago ( mysql pivoting table result of fields json ) I have found another problem. It turns out that I have to obtain several fields of union and divide it by the total number of elements that the union has.
I currently have:
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)
This generates me a table with the following data:
+---------------+-------+--------+
| 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)
My goal now is to get this same table, but with the field "times" divided by 86. That is, the total of elements that result from the query ... of the union !!!
initially I have included all the union query in a select such that:
select field, veces, cuenta
from (
... // aqui va todo el select/union
) as T1;
With this query, I have the same previous table of results again.But when trying to divide by the total of elements, with a
count (*):
select field, times / count (*), count from ( ... // here goes all the select / union
) as T1;
the result is:
+---------------------+-------+
| T1.field / count(*) | veces |
+---------------------+-------+
| 0 | 1 |
+---------------------+-------+
1 row in set, 1 warning (0,51 sec)
Does anyone come up with any way to get the total union rows, and divide a field by that total?
Thanks in advance, and happy Christmas !!!