SQL查询以在各种DATE范围内检索SUM [英] SQL query to retrieve SUM in various DATE ranges

查看:112
本文介绍了SQL查询以在各种DATE范围内检索SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要得到的结果应该是为4行,其中前三位为1月,2月和3月。最后一行是在这3个月内没有出售的产品。



这是表格。 http://imageshack.us/a/img823/8731/fmlxv.jpg



表列是:

  id 
sale_id
product_id
数量
customer_id
payment_method_id
total_price
日期
时间

所以在结果中,第3行将只是:




  • January ,1月份的SUM

  • 二月,SUM二月份

  • 三月,MArch
    的SUM和下一行应为4月, 4月份没有任何项目,所以我真的不知道这一切。



编者按:根据链接的图像,上面的列将是2013年。

解决方案

我会去以下

  SELECT SUM(totalprice),year(date),month(date)
FROM sales
GROUP BY年(日), th(date)


I have a table with information about sold products, the customer, the date of the purchase and summary of sold units.

The result I am trying to get should be 4 rows where the 1st three are for January, February and March. The last row is for the products that weren't sold in these 3 months.

Here is the table. http://imageshack.us/a/img823/8731/fmlxv.jpg

The table columns are:

id
sale_id
product_id
quantity
customer_id
payment_method_id
total_price
date
time

So in the result the 1st 3 row would be just:

  • January, SUM for January
  • February, SUM for February
  • March, SUM for MArch and the next row should be for April, but there are no items in April yet, so I don't really know how to go about all this.

Editor's note: based on the linked image, the columns above would be for the year 2013.

解决方案

I would go with the following

SELECT SUM(totalprice), year(date), month(date) 
FROM sales
GROUP BY year(date), month(date)

这篇关于SQL查询以在各种DATE范围内检索SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆