我有以下列的表
id, money, date
我想要一个SQL查询,它返回在两个自定义日期之间计算的sum(amount)
,例如
Date between '2014-1-10' and '2016-3-12'
Date between '2015-8-10' and '2017-6-12'
Date between '2014-2-10' and '2016-6-12'
结果将与
类似customWhere amount
1 20000000$
2 23495000$
3 12940593$
答案 0 :(得分:0)
在select case
子句中使用groupby
Select (select case
when MyDate between Convert(date,'Some Date')
and Convert(date,'Other Date')
then 1 else 2 end) 'Case',
SUM(Ammount) 'Sum'
FROM YourTable
GROUP BY (select case
when MyDate between Convert(date,'Some Date')
and Convert(date,'Other Date')
then 1 else 2 end)
分组依据 - https://msdn.microsoft.com/en-us/library/ms177673.aspx
答案 1 :(得分:-1)
select 1 as cn, sum(amount) as sss
from table
where Date between '2014-1-10' and '2016-3-12'
union
select 2 as cn, sum(amount) as sss
from table
where Date between '2015-8-10' and '2017-6-12'
...