例如下列分组汇总SQL语句,返回本月每个产品类别的销售总金额:
select 产品类别,sum(单价*数量) as 销售金额 from 销售记录
where Date_Format(销售日期,'%Y%m')=Date_Format(now(),'%Y%m')
group by 产品类别
MYSQL数据库
SELECT 产品,GROUP_CONCAT(零件),SUM(数量),SUM(金额) FROM (
SELECT 'A' AS 产品,'A1' AS 零件,'10' AS 数量,'10' AS 金额
UNION ALL
SELECT 'A' AS 产品,'A2' AS 零件,'20' AS 数量,'20' AS 金额
UNION ALL
SELECT 'A' AS 产品,'A3' AS 零件,'10' AS 数量,'10' AS 金额
UNION ALL
SELECT 'B' AS 产品,'B1' AS 零件,'10' AS 数量,'10' AS 金额
UNION ALL
SELECT 'B' AS 产品,'B2' AS 零件,'20' AS 数量,'20' AS 金额
UNION ALL
SELECT 'B' AS 产品,'B3' AS 零件,'10' AS 数量,'10' AS 金额
) T GROUP BY 产品
select top(5) 商品,类别 \\显示每个分类的前5条信息from product_type,product
where product_type.类别=product.类别 \\两个表的对应关系
group by 类别 \\分类汇总
order by create_time dec\\降序排列
由于不知道你具体表间关系和列名,你自己调整下,应该就行了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)