按时间间隔分组问题

sysu_bin 2007-12-17 01:13:40
现在有一张表,有BirthYear(出生年)和Degree(学位),(dep)系别三个字段,需要统计在一定出生年间隔内的符合某个学位的人数,比如:
1900-1902出生人员统计结果
  博士 硕士 学士 总计
政治科学系 0 0 0 0
行政管理系 0 0 0 0

1903-1905出生人员统计结果
  博士 硕士 学士 总计
政治科学系 0 0 0 0
行政管理系 0 0 0 0

1906-1908出生人员统计结果
  博士 硕士 学士 总计
政治科学系 0 0 0 0
行政管理系 0 0 0 0

请问怎样按时间间隔分组,sql语句该怎样写?谢谢



...全文
197 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sysu_bin 2007-12-17
  • 打赏
  • 举报
回复
谢谢楼上两位,我先试一下
dawugui 2007-12-17
  • 打赏
  • 举报
回复
select dep , 时间 = '1900-1902',
sum(case when year(BirthYear) 1900 and 1902 and 学位 = '博士' then 1 end) '博士',
sum(case when year(BirthYear) 1900 and 1902 and 学位 = '硕士' then 1 end) '硕士',
sum(case when year(BirthYear) 1900 and 1902 and 学位 = '学士' then 1 end) '学士',
sum(case when year(BirthYear) 1900 and 1902 then 1 end) '总计'
from tb group by dep
union all
select dep , 时间 = '1903-1905',
sum(case when year(BirthYear) 1903 and 1905 and 学位 = '博士' then 1 end) '博士',
sum(case when year(BirthYear) 1903 and 1905 and 学位 = '硕士' then 1 end) '硕士',
sum(case when year(BirthYear) 1903 and 1905 and 学位 = '学士' then 1 end) '学士',
sum(case when year(BirthYear) 1903 and 1905 then 1 end) '总计'
from tb group by dep
union all
select dep , 时间 = '1906-1908',
sum(case when year(BirthYear) 1906 and 1908 and 学位 = '博士' then 1 end) '博士',
sum(case when year(BirthYear) 1906 and 1908 and 学位 = '硕士' then 1 end) '硕士',
sum(case when year(BirthYear) 1906 and 1908 and 学位 = '学士' then 1 end) '学士',
sum(case when year(BirthYear) 1906 and 1908 then 1 end) '总计'
from tb group by dep
fcuandy 2007-12-17
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20071214/10/87743826-2644-479a-b144-731c4b29b07e.html

将记录中的日期值按这个贴子里的方式进行分组聚合.
再将聚合结果与原表连表即可得出.

34,837

社区成员

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

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