求条SQL语句?

paulchenbo 2008-01-18 11:17:54
年度周期统计,统计范围是上月的26号到本月的25号,如何使用group by 进行汇总计算?
...全文
100 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bzscs 2008-01-18
  • 打赏
  • 举报
回复
写个自定义函数,不知道行不行,例如输入日期为1月26-2月25,函数结果为1;2月26-3月25,函数结果为2;,然后group by 此函数(日期)
-狙击手- 2008-01-18
  • 打赏
  • 举报
回复
select datepart(wk,datecol),sum(..........)
from t
where datecol between .... and ....
grtoup by datepart(wk,datecol)
dawugui 2008-01-18
  • 打赏
  • 举报
回复
自己建立一个表如:
1月 12-26 1-25
2月 1-26 2-25
....

12月11-26,12-25

然后使用group by
wzy_love_sly 2008-01-18
  • 打赏
  • 举报
回复
没有表写不出来.....
kk19840210 2008-01-18
  • 打赏
  • 举报
回复

select convert(varchar(7),max(dt),120),count(1) from # group by case when day(dt)<26 then convert(varchar(7),dt,120) else convert(varchar(7),dateadd(month,1,dt),120) end


------- -----------
2007-01 3
2007-02 3

(2 行受影响)
kk19840210 2008-01-18
  • 打赏
  • 举报
回复
create table # (id int identity(1,1),dt datetime)
insert into # values('2006-12-26')
insert into # values('2007-01-01')
insert into # values('2007-01-22')
insert into # values('2007-01-28')
insert into # values('2007-02-01')
insert into # values('2007-02-11')

select count(1) from # group by case when day(dt)<26 then convert(varchar(7),dt,120) else convert(varchar(7),dateadd(month,1,dt),120) end



-----------
3
3
kk19840210 2008-01-18
  • 打赏
  • 举报
回复
group by case when day(时间)<26 then convert(varchar(7),时间,120) else convert(varchar(7),dateadd(month,1,时间),120) end
kk19840210 2008-01-18
  • 打赏
  • 举报
回复

group by case when day(出生年月)<26 then convert(varchar(7),时间,120) else convert(varchar(7),dateadd(month,1,时间),120) end
paulchenbo 2008-01-18
  • 打赏
  • 举报
回复
ID 日期
1 2006-12-26
2 2007-01-01
3 2007-01-22
4 2007-01-28
5 2007-02-01
6 2007-02-11

查询出
ID 年月 数量
1 2007-01 3
2 2007-02 3
JL99000 2008-01-18
  • 打赏
  • 举报
回复
把原始数据结构和想要的结构贴出来
这样没法写

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧