我有一个查询,显示日期的便宜和昂贵的订单数量:
SELECT od1.date,
(SELECT COUNT( od2.id) FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price BETWEEN 500 AND 2000
) cheap_orders,
(SELECT SUM(myproducts.price)
FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price BETWEEN 500 AND 2000
) sum_ch_o,
(SELECT COUNT( od2.id) FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price > 2000
) expensive_orders,
(SELECT SUM(myproducts.price)
FROM myorders od2
INNER JOIN myproducts ON od2.product_id = myproducts.id
WHERE od2.date = od1.date
AND myproducts.price > 2000
) sum_exp_o
FROM myorders od1
GROUP BY od1.date
它给我一张桌子:
date cheap_orders sum_ch_o expensive_orders sum_exp_o
2018-06-06 0 0 1 4000
2018-06-04 2 3100 0 0
2018-06-07 1 780 3 28000
2018-06-05 1 560 4 15500
如何修改查询以获取此表的另一个视图:
date order_cat quantity sum
2018-06-06 cheap 0 0
2018-06-06 expensive 1 4000
2018-06-04 cheap 2 3100
2018-06-04 expensive 0 0
2018-06-07 cheap 1 780
2018-06-07 expensive 3 28000