两个日期计算字段之间的值的SQL SUM [英] SQL SUM of values between two dates calculated fields

查看:281
本文介绍了两个日期计算字段之间的值的SQL SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此查询,该查询返回null:

I have this query, which returns null:

SELECT 
   SUM(sales) as [MAT]  
from 
   TABLE2 
where 
   Date1 between CONVERT(VARCHAR(23), DATEADD(MONTH, -11, Date1), 111) and 
                 CONVERT(VARCHAR(23), DATEADD(MONTH, 0, Date1), 111);

在列中,date1的格式为YYYY-DD-MM,这就是为什么要转换它的原因.

In the columns, date1 is in the format YYYY-DD-MM, which is why I convert it.

我希望两个日期之间的乘积(另一个字段)中每个日期的金额总和.

I want the sum of the amounts from each date by product (another field) between those two dates.

-更新-

我已经将格式更改为DATETIME.现在查询如下:

Hi, i already changed the format to DATETIME . Now the query looks like this:

SELECT 
   SUM(sales)as [MAT] 
from 
   TABLE2 
where 
   Date1 between DATEADD(MONTH,-11,Date1) and DATEADD(MONTH,0,Date1);

它向我显示了所有产品的总数,我想要每种产品的总数...有什么想法吗? @DanBracuk

It shows me the total amount of all the products, and i want the total for each...any ideas? @DanBracuk

推荐答案

假设您在表中有一个名为"partid"的列,则可能是这样的:

assuming you've got a column in the table called 'partid', it would be something like this:

select partid,sum(sales)
from 
 TABLE2 
where 
 Date1 between DATEADD(MONTH,-11,Date1) and DATEADD(MONTH,0,Date1);
group by partid

这篇关于两个日期计算字段之间的值的SQL SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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