Group by year in date field in MySQL

早过忘川 提交于 2019-12-02 04:19:58

Extract the year from the date and group by it

select year(date) as year, 
       count(customer_id) as customers
from your_table
group by year
order by year asc

Use the EXTRACT function to get the relevant part of the date :

select extract(YEAR from join_d)"Year",count(DISTINCT customer_id) "customer_count" 
from  customer 
group by year
order by year;

Use the YEAR function to get the year part of the date, and use that in the grouping and ordering.

SELECT YEAR(date) AS year, COUNT(*) AS customer_count
FROM customer
GROUP BY year
ORDER BY year
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!