select p.product_id, product_name, sum(quantity) as total_sales, max(quantity) as max_monthly_sales, min(quantity) as min_monthly_sales, cast(avg(quantity) as signed) as avg_monthly_sales from products_underline p join sales_underline s on p.product_id = s.product_id where sale_month between '2024-01' and '2024-06' group by p.product_id order by p.product_id asc;
很简单,合并两个表,取出sale_month between '2024-01' and '2024-06'的记录,然后group by 商品id,获得合计,最大,最小和平均。