MySQL Forums
Forum List  »  Spanish

sumar campos null y no null por id
Posted by: antonio sanchez
Date: February 22, 2018 11:27AM

hola tengo una situación estoy tratando de sumar los campos null y no null por id de campo

ejemplo

campo 1 = 1 accion 1 hallazgo
campo 2 = 0 accion 0 hallazgo
campo 3 = 2 accion 2 hallazgo

solo que me manda esto

campo 1 = 3 accion 3 hallazgo
campo 2 = 3 accion 3 hallazgo
campo 3 = 3 accion 3 hallazgo

solo me suma los campos pero no me separa por id

este es mi código donde lo hago con un if

SELECT a.texto as titulo,b.id_pregunta,
SUM(CASE WHEN b.valor = 1 THEN 1 ELSE 0 END) AS "si",
SUM(CASE WHEN b.valor = 0 THEN 1 ELSE 0 END) AS "NO",
SUM( IF(b.id_pregunta = a.id and b.hallazgo IS NOT NULL, 1, 0)) as hallazgos,
SUM( IF(b.id_pregunta = a.id and b.Accion IS NOT NULL, 1, 0)) as acciones,
SUM( IF(b.id_pregunta = a.id and b.hallazgo IS NULL, 1, 0)) as no hallazgos,
SUM( IF(b.id_pregunta = a.id and b.Accion IS NULL, 1, 0)) as no acciones
FROM respuestas a INNER JOIN opciones b ON a.id = b.id_pregunta WHERE a.idenc =
GROUP BY a.id,a.texto,b.id_pregunta

este es otro codigo donde lo hago con un case

SELECT a.texto as titulo,b.id_pregunta,
SUM(CASE WHEN b.valor = 1 THEN 1 ELSE 0 END) AS "si",
SUM(CASE WHEN b.valor = 0 THEN 1 ELSE 0 END) AS "NO",
SUM(CASE WHEN b.hallazgo IS NOT NULL THEN 1 ELSE 0 END) AS "NO",
SUM(CASE WHEN b.Accion IS NOT NULL THEN 1 ELSE 0 END) AS "NO",
SUM(CASE WHEN b.hallazgo IS NULL THEN 1 ELSE 0 END) AS "NO",
SUM(CASE WHEN b.Accion IS NULL THEN 1 ELSE 0 END) AS "NO"
FROM respuestas a INNER JOIN opciones b ON a.id = b.id_pregunta WHERE a.idenc =
GROUP BY a.id,a.texto,b.id_pregunta

Options: Reply• Quote


Subject
Views
Written By
Posted
sumar campos null y no null por id
2009
February 22, 2018 11:27AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.