MYSQL SUM&累计

时间:2017-06-06 22:57:20

标签: mysql

我正在尝试为以下mysql查询获取GRAND TOTAL。但我没有运气,我还不太擅长mysql。

SELECT SUM(ep_price)
FROM orders
WHERE date_shipped BETWEEN '2017-04-01' and '2017-04-30'
GROUP BY order_number 

查询:

Result: 

'152989', '93670.39999999998'
'157669', '159.5'
'165955', '45195'
'166054', '5030'
'166410', '29700'
'167023', '4530'
'167110', '9647.2'
'167152', '56620'
'167477', '46087.14'
'167483', '1690'
'167545', '3295'
'167563', '9144.550000000001'
'167566', '35036.67999999999'
'167736', '2015'
'167853', '1277.5'
'167971', '3695'
'167982', '5690.4'
'168063', '4316.4'
'168101', '3400'
'168330', '4686.9'
'168435', '725'
'168454', '4002.5'
'168455', '6372'
'168477', '1941'
'168590', '3625.5'
'168593', '18560.4'
'168704', '238'
'168706', '119'
'168870', '3340'
'168962', '2800'
'169019', '2050'
'169203', '1940'
'169207', '7500.51'
'169209', '3888.4'
'169212', '10006.5'
'169214', '5580'
'169230', '2742.5'
'169235', '33490.2'
'169371', '12400'
'169413', '671.6'
'169529', '2345'
'169642', '17480'
'169689', '1500'
'169731', '1602.9'
'169835', '567'
'169893', '535.2'
'169895', '431'
'170015', '143.2'
'170031', '431.8'
'170050', '233.7'
'170093', '3465'
'170157', '3070'
'170159', '6040'
'170176', '746.5'
'170260', '3195'
'170318', '4081.06'
'170323', '5400'
'170324', '2865'
'170418', '1815'
'170419', '3984'
'170451', '29775.2'
'170483', '184.7'
'170484', '996.4'
'170486', '1363.8'
'170549', '9975.75'
'170566', '5350'
'170599', '5978.5'
'170600', '5480.42'
'170603', '1309.95'
'170612', '9979.7'
'170619', '2194'
'170620', '32490'
'170661', '4075'
'170721', '1910'
'170735', '4950'
'170756', '19450'
'170758', '9250'
'170763', '285.8'
'170821', '1875'
'170849', '4950'
'170859', '3212.84'
'170865', '132.6'
'170991', '583.1'
'170992', '155.2'
'171108', '181.6'
'171111', '180.9'
'171113', '1481.1'
'171120', '332.8'
'171135', '3869.3199999999997'
'171200', '1175'
'171204', '288.1'
'171205', '261.5'
'171206', '256.6'
'171208', '221.6'
'171209', '815'
'171217', '14903.6'
'171262', '3910'
'171269', '545'
'171272', '273.1'
'171303', '976.25'
'171305', '3600'
'171309', '195.2'
'171310', '16959.13'
'171354', '1215'
'171382', '4500'
'171386', '86.9'
'171407', '511.5'
'171410', '2744.4'
'171412', '5634'
'171416', '5287.5'
'171417', '2063.3999999999996'
'171423', '150.2'
'171513', '4370.4'
'171566', '2747'
'171567', '134.7'
'171670', '2067'
'171676', '348.8'
'171677', '437.5'
'171717', '198'
'171728', '165.5'
'171767', '17430'
'171771', '865.8000000000001'
'171774', '3627.5'
'171831', '13987.5'
'171834', '142.4'
'171836', '1108.2'
'171837', '5199'
'171956', '254.9'
'171958', '1286.8'
'171962', '5586.4'
'171963', '4128.9'
'172067', '1690'
'172068', '5705'
'172069', '893.75'
'172074', '3795'
'172174', '1242'
'172214', '1411.2'
'172215', '568.8'
'172217', '1848.2'
'172219', '1985'
'172220', '22436'
'172244', '166.1'
'172245', '325.9'
'172246', '2395'
'172329', '1885.5'
'172333', '197.8'
'172338', '297.2'
'172341', '1126.5'
'172342', '138.3'
'172352', '388.3'
'172355', '160'
'172382', '244.2'
'172442', '3628.71'
'172448', '1050'
'172452', '213.2'
'172466', '292.6'
'172467', '180'
'172469', '407.1'
'172515', '965'
'172539', '3812.5'
'172602', '1649'
'172631', '514'
'172632', '552.8'
'172834', '250.7'
'172835', '360'
'172836', '0'
'172840', '0'
'172844', '815'
'172846', '2970'
'172902', '4635'
'172997', '143.9'
'172998', '174.7'
'173132', '210'
'173255', '290'
'173260', '565.9'
'173277', '115'
'173545', '672.1'
'173546', '225'
'173562', '215'
'173606', '445.6'
'173611', '228.4'
'173646', '238.61'
'173690', '755'
'173694', '15400'
'173706', '0'
'174028', '855'

那么如何在总计中将所有这些order_numbers一起添加?

我将有多个具有相同order_number和ep_price的条目,但区别是job_number。这就是我使用GROUP BY order_number的原因。

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

您在寻找ROLLUP吗?

SELECT SUM(ep_price)
FROM orders
WHERE date_shipped BETWEEN '2017-04-01' and '2017-04-30'
GROUP BY order_number WITH ROLLUP;

这将在最后一行返回所有组的总和。