TECHNO subbu:
mi mesa
mysql> desc attendance;
+--------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+----------------+
| aid | bigint(20) | NO | PRI | NULL | auto_increment |
| sid | int(10) | YES | MUL | NULL | |
| cid | bigint(20) | YES | MUL | NULL | |
| ttid | bigint(20) | YES | MUL | NULL | |
| did | int(3) | YES | MUL | NULL | |
| date | date | YES | | NULL | |
| hour | varchar(3) | YES | | NULL | |
| stuid | bigint(20) | YES | MUL | NULL | |
| status | varchar(8) | YES | | NULL | |
+--------+------------+------+-----+---------+----------------+
9 rows in set (0.47 sec)
Y es valores son
mysql> select * from attendance;
+-----+------+------+------+------+------------+------+-------+---------+
| aid | sid | cid | ttid | did | date | hour | stuid | status |
+-----+------+------+------+------+------------+------+-------+---------+
| 1 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 16 | present |
| 2 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 17 | absent |
| 3 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 18 | present |
| 4 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 19 | absent |
| 5 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 20 | present |
| 6 | 2 | 13 | 4 | 3 | 2020-03-25 | p1 | 15 | absent |
| 7 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 16 | present |
| 8 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 17 | absent |
| 9 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 18 | present |
| 10 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 19 | absent |
| 11 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 20 | present |
| 12 | 2 | 13 | 2 | 2 | 2020-03-17 | p6 | 15 | absent |
| 13 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 16 | present |
| 14 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 17 | present |
| 15 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 18 | present |
| 16 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 19 | present |
| 17 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 20 | present |
| 18 | 2 | 13 | 4 | 3 | 2020-03-25 | p2 | 15 | present |
| 19 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 16 | absent |
| 20 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 17 | absent |
| 21 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 18 | present |
| 22 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 19 | present |
| 23 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 20 | present |
| 24 | 2 | 13 | 4 | 3 | 2020-03-25 | p8 | 15 | absent |
+-----+------+------+------+------+------------+------+-------+---------+
24 rows in set (0.00 sec)
Ahora, voy a agrupar valor de la tabla, sino que vuelve en 0 Mientras que el grupo de la tabla que devuelve todos y cada uno regresa valor de columna en 0. Por lo que se pasó. Me necesidad presente o ausente en lugar de 0. ¿Alguien puede resolver este error lógico?
GMB:
sum(case when hour = 'P1' then status else 'null' end) p1
El problema es que están los números y Suming cadena Litteral 'null'
.
Querías null
(sin comillas) - o mejor aún,0
sum(case when hour = 'p1' then status else 0 end) p1
Tenga en cuenta que en MySQL evalúa las condiciones de verdadero / falso como 1/0 en contexto numérico, lo que permite acortar la expresión como:
sum(hour = 'p1') p1