我想检索按月份订购的产品的总和(数量)。例如,输出如下:
产品1 ........ 20 ......七月-2008
产品2 ........ 10 ......七月-2008
下面是我的默认查询,它似乎给出了正确的结果:
SELECT ITEMNAME, SUM(QTY), DATE_FORMAT(BILLDATE,'%M-%Y') FROM BILLITEMS
GROUP BY BILLDATE, ITEMNAME
ORDER BY YEAR(BILLDATE), MONTH(BILLDATE), ITEMNAME;
我关心的是查询速度。我尝试了以下优化,但它给出了不同的结果:
SELECT ITEMNAME, SUM(QTY), DATE_FORMAT(BILLDATE,'%M-%Y') FROM BILLITEMS
GROUP BY BILLDATE, ITEMNAME
ORDER BY DATE_FORMAT(BILLDATE,'%Y-%M'), ITEMNAME;
另一个:
SELECT ITEMNAME, SUM(QTY), DATE_FORMAT(BILLDATE,'%M-%Y') FROM BILLITEMS
GROUP BY DATE_FORMAT(BILLDATE,'%M-%Y'), ITEMNAME
ORDER BY DATE_FORMAT(BILLDATE,'%Y-%M'), ITEMNAME;