I have the following table in SQL:
ID Descripcion Respuesta
1 Nombre1 True
2 Nombre2 True
2 Nombre2 False
2 Nombre2 False
1 Nombre1 True
And I want to get in a single table the following:
Descripcion TotalRegistros RegistrosSI RegistrosNo
Nombre1 2 2 0
Nombre2 3 1 2
Through this query I can obtain the total of Records
SELECT Descripcion, COUNT(ID) FROM Tabla GROUP BY Descripcion, ID //Por ejemplo
But I don't know how to add the other columns with data, I know that if I get the total and the total of YES in the same table I can get the RecordsNO, but I don't know how to generate it. Is there a way to have a subquery on count for example?
You could solve it this way:
What we do is use a
SUM()
conditional depending on the answer, if it is we"True"
will add 1 inRegistroSi
and 0 inRegistroNo
. If itRespuesta
is actually of typeBIT
, we will simply compareRespuesta = 1
.COUNT(1)
it just adds up all the cases.