Follows:
SELECT COUNT(*) AS QTD_CLIENTES_CADASTRADOS, EXTRACT(YEAR FROM DATACADASTRO) AS ANO
FROM TABELA
WHERE EXTRACT(YEAR FROM DATACADASTRO) IN (2016, 2017, 2018)
GROUP BY EXTRACT(YEAR FROM DATACADASTRO);
Explanation:
When using the function EXTRACT (YEAR FROM DATE)
you will extract the day/month/year from a determining field timestamp
of your database, with this function, you can make conditional, use in the order by
, add into SELECT
among other features.
I used the in
to prevent the addition of three and
(which would refer to each year), thus avoiding the SQL
great without need.
Edited
There’s another way to do this SQL
avoiding so many EXTRACTS
:
SELECT COUNT(S.QTD) AS CLIENTES_CADASTRADOS, S.ANO
FROM (SELECT 1 AS QTD, EXTRACT(YEAR FROM DATACADASTRO) AS ANO
FROM TABELA) AS S
WHERE S.ANO IN (2016, 2017, 2018);
GROUP BY S.ANO;
Hello I tried to do the search this way and this returning an error message, could anyone help me? SQL: select datapedido, sum(total) total from pedido Where status>0 group by substr(datapedido,0,7) order by datapedido desc Error: select datapedido, sum(total) total from pedido Where status>0 group by substr(datapedido,0,7) order by datapedido desc LIMIT 0, 25 Mysql messages : Documentation #1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'server.pedido.datapedido' which is not functionally dependent on Columns in GROUP BY clause; this
– Fabio Branco da Silva