You can do several SELECT
for the intended result:
SELECT
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 4) AS Numero_registos_Abril,
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 4 AND tipo = 1) AS Numero_registos_Abril_tipo1,
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 4 AND tipo = 2) AS Numero_registos_Abril_tipo2,
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 3) AS Numero_registos_Marco,
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 3 AND tipo = 1) AS Numero_registos_Marco_tipo1,
(SELECT COUNT (*) FROM tbl_x WHERE MONTH(dataCriacao) = 3 AND tipo = 2) AS Numero_registos_Marco_tipo2,
FROM tbl_x
EDIT:
Using the CASE
, and similarly to the various select
of the above solution, you will make a COUNT
for each of your conditions:
SELECT
COUNT(CASE WHEN MONTH(dataCriacao) = 4 THEN 1 END) AS Numero_registos_Abril,
COUNT(CASE WHEN MONTH(dataCriacao) = 4 AND tipo = 1 THEN 1 END) AS Numero_registos_Abril_tipo1,
COUNT(CASE WHEN MONTH(dataCriacao) = 4 AND tipo = 2 THEN 1 END) AS Numero_registos_Abril_tipo2,
COUNT(CASE WHEN MONTH(dataCriacao) = 3 THEN 1 END) AS Numero_registos_Marco,
COUNT(CASE WHEN MONTH(dataCriacao) = 3 AND tipo = 1 THEN 1 END) AS Numero_registos_Marco_tipo1,
COUNT(CASE WHEN MONTH(dataCriacao) = 3 AND tipo = 2 THEN 1 END) AS Numero_registos_Marco_tipo2,
FROM tbl_x
In this example, the value you put on THEN
is irrelevant.
Uses the
CASE
, would be a solution similar to that suggested by @Cesarmiguel , but without using subselect... To learn how to use CASE , search IBM Knowledge Center. (on this IBM page, change the language to English, before researching, it does not work right)– ceinmart
@Filipemoraes the answer solved your question?
– CesarMiguel
@Cesarmiguel yes, but I used CASE in place of several selects, as ceinmart reported in the comment above. Can change the response using CASE to mark it as right?
– Filipe Moraes
@Filipemoraes, I can, but now I have little time. Then I warn you when to edit
– CesarMiguel
@Filipemoraes, I already added. See what you think
– CesarMiguel