select
a.消费日期,
a.消费地点,
a.消费类别,
a.消费金额,
(select sum(消费金额) from 表 where datediff(mm,消费日期,a.消费日期)=0) as 截至今天本月消费金额,
(select sum(消费金额) from 表 where datediff(yy,消费日期,a.消费日期)=0)as 截至今天本年消费金额
from
表 a
a.消费日期,
a.消费地点,
a.消费类别,
a.消费金额,
(select sum(消费金额) from 表 where datediff(mm,消费日期,a.消费日期)=0) as 截至今天本月消费金额,
(select sum(消费金额) from 表 where datediff(yy,消费日期,a.消费日期)=0)as 截至今天本年消费金额
from
表 a
解决方案 »
- 一个简单的视图引起的疑虑
- ERP系统中BOM分层展开,有些难度,高手来试一试!(再帖子一帖)
- SQL 2K:某个表8W条数据的表查询到7W多条后面的数据出不来了
- 如何实现快速高效的向SQL数据库的表里面插入10000条数据
- 100分求教 菜鸟求问怎么做跨表的汇总?
- ORA-00933 错误,求大神解决
- 视图,临时表,存储过程?
- 求一函数,不是很难
- 关于存储过程的,请高手给个完整的解答
- 大家好,我想请问一下如何选定一个SQL Server数据库中的几个表,将这几个表输出到一个Excel文件?要VB代码实现或用SQL Server的编程来实现?谢谢!
- 利用DTS备份恢复数据库
- 把表查询作为条件参数传递的实现方法?谢谢!可能问题不好理解,请进来看看!
(select sum(消费金额) from t t_m
where t_m.消费地点=t.消费地点 and t_m.消费类别=t.消费类别
and t_m.消费日期<=t.消费日期
and datediff(month, t_m.消费日期, t.消费日期)=0) 消费金额_m,
(select sum(消费金额) from t t_y
where t_y.消费地点=t.消费地点 and t_y.消费类别=t.消费类别
and t_y.消费日期<=t.消费日期
and datediff(month, t_y.消费日期, t.消费日期)=0) 消费金额_y
from t
a.消费日期,
a.消费地点,
a.消费类别,
a.消费金额,
(select sum(消费金额) from 表 where datediff(mm,消费日期,@rq)=0) as 截至今天本月消费金额,
(select sum(消费金额) from 表 where datediff(yy,消费日期,@rq)=0)as 截至今天本年消费金额
from
表 a
sum(cast when month(消费日期)=month(getdate()) then 消费金额 else 0) as 截至今天本月消费金额,
sum(cast when year(消费日期)=year(getdate()) then 消费金额 else 0) as 截至今天本年消费金额
from 表