如何根据日期获得记录总和

时间:2016-12-15 12:45:50

标签: mysql

我有一个SQL查询

SELECT t2.net 
     , t1.date 
  FROM database1.invoices t1 
  join invoicesdetail t2 
    on t1.id = t2.InvID 
 where shopid = 11
   and t1.date between '2016-01-30' AND '2016-01-31'  
   AND t2.StockType = 1

` 通过使用上面的查询,我得到了像

这样的记录
net date
120 2016-01-30
115 2016-01-20
 35 2016-01-31
170 2016-01-31

我只是想计算这个记录的日期,如2016-01-30总计235和2016-01-31总计是205,总计是440,请帮我创建此查询

2 个答案:

答案 0 :(得分:1)

总结一下:

SELECT sum(t2.net), t1.date
FROM database1.invoices t1
JOIN invoicesdetail t2 ON t1.id = t2.InvID
WHERE shopid='11' AND t1.date BETWEEN '2016-01-30' AND '2016-01-31' AND t2.StockType=1
GROUP BY t1.date

答案 1 :(得分:0)

SELECT SUM(t2.net), t1.date 
FROM database1.invoices t1 
JOIN invoicesdetail t2 ON t1.id = t2.InvID 
WHERE shopid='11' 
AND t1.date between '2016-01-30' AND '2016-01-31'  
AND t2.StockType=1
GROUP BY t1.date
UNION
SELECT SUM(t2.net), 'TOTAL' 
FROM database1.invoices t1 
JOIN invoicesdetail t2 ON t1.id = t2.InvID 
WHERE shopid='11' 
AND t1.date between '2016-01-30' AND '2016-01-31'  
AND t2.StockType=1