表结构
id :标识列
insutype:类型 [int]
value :值 小数类型
createtime:创建时间 DateTime问题如下:查询 按月和类型分组统计总和
现在的要求:不是按自然月查询,例如 :2007-07-27到2007-08-26 (包括边界)算一个月 算8月份的值
例如
id insutype value createtime
1 2 1 2007-07-262 1 8 2007-07-27
3 2 2 2007-08-11
4 1 8 2007-08-225 2 9 2007-08-27
6 1 9 2007-09-1
7 2 10 2007-10-1查询出来的结果应该是这样
类型 值(sum) 时间
1 1 2007-07
2 2 2007-08
1 16 2007-082 9 2007-09
1 9 2007-092 10 2007-10
----------------
请各位帮忙了,万分感谢
id :标识列
insutype:类型 [int]
value :值 小数类型
createtime:创建时间 DateTime问题如下:查询 按月和类型分组统计总和
现在的要求:不是按自然月查询,例如 :2007-07-27到2007-08-26 (包括边界)算一个月 算8月份的值
例如
id insutype value createtime
1 2 1 2007-07-262 1 8 2007-07-27
3 2 2 2007-08-11
4 1 8 2007-08-225 2 9 2007-08-27
6 1 9 2007-09-1
7 2 10 2007-10-1查询出来的结果应该是这样
类型 值(sum) 时间
1 1 2007-07
2 2 2007-08
1 16 2007-082 9 2007-09
1 9 2007-092 10 2007-10
----------------
请各位帮忙了,万分感谢
解决方案 »
- 请老师讲讲left joint, right joint, inner joint 的区别吧
- 菜鸟咨询-----网络版进销存
- sql进行报表统计语句运行速度慢
- 菜鸟送分了 SQL一个关于重复数据的问题
- 邹老大,请回答!!!
- 求sql 汇总语句
- “未指定的错误”
- mysql 的sql语句
- 我的数据库日志文件一周时间会增加1G,怎样才能让数据库按时自动压缩日志,紧急求援!!!
- 请大家评一下这个号称最快的通用分页存储过程好不好?
- 求一条SQL语句!!(保证迅速结贴)
- 执行dbcc checkdb('databasename')报错:在 sysobjects、sysindexes、syscolumns 或 systypes 中存在一致性错误,妨碍了进一步的 CHECKDB
declare @t table(id int,insutype int,value int,createtime DateTime)
insert @t
select 1, 2, 1, '2007-07-26' union all
select 2, 1, 8, '2007-07-27' union all
select 3, 2, 2, '2007-08-11' union all
select 4, 1, 8, '2007-08-22' union all
select 5, 2, 9, '2007-08-27' union all
select 6, 1, 9, '2007-09-1' union all
select 7, 2, 10, '2007-10-1'----查询
SELECT insutype as 类型,
值 = sum(value),
时间 =
case
when day(createtime) >= 27
then convert(varchar(7),dateadd(month,1,createtime),120)
else convert(varchar(7),createtime,120)
end
FROM @t GROUP BY insutype,
case
when day(createtime) >= 27
then convert(varchar(7),dateadd(month,1,createtime),120)
else convert(varchar(7),createtime,120)
end
ORDER BY 3,1 DESC/*结果
类型 值(sum) 时间
类型 值 时间
----------- ----------- -------
2 1 2007-07
2 2 2007-08
1 16 2007-08
2 9 2007-09
1 9 2007-09
2 10 2007-10
*/
declare @t table(id int,insutype int,value int,createtime DateTime)
insert @t
select 1, 2, 1, '2007-07-26' union all
select 2, 1, 8, '2007-07-27' union all
select 3, 2, 2, '2007-08-11' union all
select 4, 1, 8, '2007-08-22' union all
select 5, 2, 9, '2007-08-27' union all
select 6, 1, 9, '2007-09-1' union all
select 7, 2, 10, '2007-10-1'
select id,insutype,[value] ,createtime into #t1
from @t update #t1 set createtime=dateadd(day,5,createtime) where day(createtime)>26 select insutype,sum([value]),year(createtime),month(createtime) from #t1
group by insutype,year(createtime),month(createtime)
order by year(createtime),month(createtime)
drop table #t1