欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sql分组查询统计

程序员文章站 2022-06-05 09:30:09
...
SELECT
	COUNT(1) count,
	CASE
			WHEN age < 25 THEN
				0
			WHEN age >= 25
			AND age < 35 THEN
				1
			WHEN age >= 35
			AND age < 50 THEN
				2
			WHEN age > 50 THEN
				3
			END zu
FROM
	(
		SELECT
			YEAR (NOW()) - SUBSTR(c.card_number, 7, 4) age
		FROM
			(
				SELECT DISTINCT
					s.customer_id id
				FROM
					t_sales s
				LEFT JOIN t_product p ON s.product_id = p.id
				WHERE
					p.`status` IN (60, 70)
				AND s.`status` IN (20, 30, 40)
			) t
		LEFT JOIN t_customer c ON t.id = c.id
	) ages
GROUP BY
	zu
ORDER BY
	zu;

描述:

有时间再写吧。执行后是这样子,count是各年龄段认数之和

sql分组查询统计

相关标签: sql