I'm working on a report in MySQL where I need to show a percentage of various events that occurred with a group of entities.
The structure of my table is as follows:
| id | eventoA | eventoB | eventoC | eventoD |
|----|---------|---------|---------|---------|
| 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 0 | 0 | 0 |
| 3 | 1 | 0 | 0 | 0 |
| 4 | 0 | 0 | 1 | 0 |
| 5 | 0 | 1 | 0 | 0 |
| 6 | 1 | 1 | 0 | 0 |
| 7 | 1 | 1 | 0 | 0 |
| 8 | 1 | 0 | 1 | 0 |
| 9 | 0 | 0 | 1 | 0 |
| 10 | 0 | 0 | 0 | 0 |
The columns EventoA
, EventoB
, and so on are of type BIT
and are updated by the application when a certain event is triggered for that entity. Today I can generate this report with the following query :
SELECT COUNT('id') AS 'Total',
SUM('eventoA') AS 'eventoDisparado',
COUNT('id') - SUM('eventoA') AS 'eventoNaoDisparado'
FROM tabela;
Query Result:
Total: 10, eventDisplayed: 5, eventNotDisplayed: 5
But this way I can not combine multiple events because the number of events ends up not beating:
SELECT COUNT('id') AS 'Total',
SUM('eventoA') + SUM('eventoB') AS 'eventoDisparado',
COUNT('id') - SUM('eventoA') + SUM('eventoB') AS 'eventoNaoDisparado'
FROM tabela;
Expected result:
Total: 10, eventDisplayed: 6, eventNotDisplayed: 4
Result obtained
Total: 10, eventDisplayed: 8, eventNotDisplayed: 2
I would like to combine the columns eventoA
and eventoB
like this:
| id | eventoA | eventoB | eventoA + eventoB |
|----|---------|---------|-------------------|
| 1 | 0 | 0 | 0 |
| 2 | 1 | 0 | 1 |
| 3 | 1 | 0 | 1 |
| 4 | 0 | 0 | 0 |
| 5 | 0 | 1 | 1 |
| 6 | 1 | 1 | 1 |
| 7 | 1 | 1 | 1 |
| 8 | 1 | 0 | 1 |
| 9 | 0 | 0 | 0 |
| 10 | 0 | 0 | 0 |
You may get the expected result using a OR
bitwise .
Is it possible to use this type of operators directly in a query ? What other alternatives do I have to get the expected result?