This is may table. In one column there are daily dates and in the other is a classification.
I use this query to count how many registers are in a month and which ones are C
or D
, but the subquery can only return one value . How can i fix it?
select FORMAT(fecha, 'yyyy-MM') as FECHAS, COUNT(*) as TOTAL,
(select count(tipo) from prueba where tipo = 'C' group by FORMAT(fecha, 'yyyy-MM')) as CREDITO,
(select count(tipo) from prueba where tipo = 'D' group by FORMAT(fecha, 'yyyy-MM')) as DEBITO
from prueba
where fecha >= '01-08-2014'
group by FORMAT(fecha, 'yyyy-MM')
Best Answer
You didn't specify which database you were using, but in most databases, you can use a
CASE
expression inside theCOUNT
aggregate function to accomplish your goal: