111 2021-07 50 //tr>111 2021-08 10 0111 2021-09 <1>100 222 2021-07 8 0//tr>2021-08年,2222021-09年,222
目前我有一个订单表,其格式为每月一行:
订单计数您需要条件聚合:
SELECT id,
MAX(CASE WHEN order_month = '2021-07' THEN order_count ELSE 0 END) `2021-07-orderCount`,
MAX(CASE WHEN order_month = '2021-07' THEN order_sum ELSE 0 END) `2021-07-orderSum`,
MAX(CASE WHEN order_month = '2021-08' THEN order_count ELSE 0 END) `2021-08-orderCount`,
MAX(CASE WHEN order_month = '2021-08' THEN order_sum ELSE 0 END) `2021-08-orderSum`,
MAX(CASE WHEN order_month = '2021-09' THEN order_count ELSE 0 END) `2021-09-orderCount`,
MAX(CASE WHEN order_month = '2021-09' THEN order_sum ELSE 0 END) `2021-09-orderSum`
FROM orders
GROUP BY id
ORDER BY id;
请参阅演示