SQL 按月分类语句

fengling2001 2011-10-12 05:46:22
表结构如下,


ID Code Category Month Rate
1 0001 A 1 92
2 0001 A 2 93
3 0001 B 3 94
4 0001 B 1 91
5 0001 A 5 97
6 0002 B 4 92


如何得到如下结果:

Month A_Num A_Rate B_Num B_Rate
1 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
2 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-2 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
3 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-3 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
4 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-4 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
...
12 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-12 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
4季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
Total 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)


Month固定是12个月,输出格式如上也是固定的,如果表里面没有对应月份数据,记作0,上例子中总共有2个不同Code
具体统计项好办,关键是如何生成类似Month这一列。(1-2 表示1月份到2月份对应的数据,1+2 - 1∩2,AVG 为AVG(Rate1)+AVG(Rate2)/2)
...全文
191 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengling2001 2011-10-13
  • 打赏
  • 举报
回复
结果如上,如果直接union,不太好实现,我现在的想法就是直接得出1-12月的数据,然后MOnth这列,进行插入,有别的方法吗?
fengling2001 2011-10-13
  • 打赏
  • 举报
回复

Month A_Num A_Rate B_Num B_Rate
1 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
2 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-2 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
3 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-3 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
4 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-4 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
5 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-5 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
6 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-6 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
2季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
7
1-7 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
8 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-8 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
9 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-9 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
3季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
10 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-10 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
11 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-11 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
12 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
1-12 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
4季度 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
Total 不同Code个数 AVG(Rate) 不同Code个数 AVG(Rate)
geniuswjt 2011-10-13
  • 打赏
  • 举报
回复
不知道啥叫“不同Code个数”,更不知道啥叫avg(rate)
建议LZ就按你提供的测试数据摆出结果
你写中文没人看得懂
小宏 2011-10-13
  • 打赏
  • 举报
回复
额~~~没看明白
haitao 2011-10-12
  • 打赏
  • 举报
回复
【关键是如何生成类似Month这一列】
一个查询按每月1行输出
union
一个查询 按1月到每月的累计作为1行输出
iymmgd 2011-10-12
  • 打赏
  • 举报
回复
顶顶。。。。
--小F-- 2011-10-12
  • 打赏
  • 举报
回复
没看懂你的结果.
--小F-- 2011-10-12
  • 打赏
  • 举报
回复
需要得到什么结果?
中国风 2011-10-12
  • 打赏
  • 举报
回复
结果直接貼數據出來看看
-晴天 2011-10-12
  • 打赏
  • 举报
回复
create table tb([month] int)
insert into tb select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7
union all select 8 union all select 9 union all select 10
union all select 11 union all select 12
go
select ltrim([month]) from tb
union all
select '1-'+ltrim([month]) from tb where [month]>1
/*
--------------
1
2
3
4
5
6
7
8
9
10
11
12
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-9
1-10
1-11
1-12

(23 行受影响)

*/
go
drop table tb

34,594

社区成员

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

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