功能统计!

zhuxianjun1 2009-01-09 09:26:58
表a:(用户使用功能模块记录表)
字段:user_id(用户ID) ,user_name(用户姓名), fun_id(功能模块代号),now_time(当前时间)
数据如:
00001,小明,1,2008-08-01
00001,小明,2,2008-08-02
00001,小明,3,2008-08-03
00002,小明,4,2008-08-04
00002,小明,1,2008-08-01
00003,小明,12,2008-08-02
00003,小明,6,2008-08-03
00003,小明,9,2008-08-04

表b:(功能模块说明表)
字段:fun_id(功能模块代号),fun_name(功能模块名称)
1,用户套餐功能1说明
2,用户套餐功能2说明
3,用户套餐功能3说明
4,用户套餐功能4说明
5,用户套餐功能5说明
6,用户套餐功能6说明
7,用户套餐功能7说明
8,用户套餐功能8说明
9,用户套餐功能9说明
10,用户套餐功能10说明
11,用户套餐功能11说明
12,用户套餐功能12说明

我想统计200808这个月各功能有多少人在用,SQL怎么写呢?
查询结果格式如:user_id ,user_name ,count (功能1),count (功能2),count (功能3),count (功能4),count (功能5)...count (功能12)
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
宇尘埃 2009-01-09
  • 打赏
  • 举报
回复
考虑到效率的话,结合楼上各位的观点,
可以通过存储过程实现:
定义游标,通过循环,
采用case 或者decode 拼接sql,
然后执行输出.
当然,如果很多的话,还是7楼的办法,通过开发工具,用程序实现更灵活些.
cjq1018 2009-01-09
  • 打赏
  • 举报
回复
butchroller 不是已经说了吗
eviler 2009-01-09
  • 打赏
  • 举报
回复
1.如果表 b 就这12个功能模块 ,一直不会变化的话,你可以使用楼上的方法 ,使用 case 或者decode 来穷举
2.如果表 b 的功能模块会变化 ,那么一个sql应该是不能解决的吧 ,如果你是使用别的开发工具来做报表的话,pb中有crosstab 可以很方便的行转列 ;另外一种方式就是 使用存储过程 ,返回 结果集,或者动态建表,把结果集插进去吧
icss_zhen 2009-01-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhuxianjun1 的回复:]
我想要的格式如:
user_id, user_name,功能1,功能2.。。。。。。功能12
0001 ,小立 , 10 ,20 。。。。。。12
0002 ,小三 , 0 ,20 。。。。。。5
。。。。。。。。。。。。。。。。。。。。。。。。
[/Quote]

select user_id, user_name,
count(decode(fun_id,1,1,0) 功能1,
count(decode(fun_id,2,1,0) 功能2,
count(decode(fun_id,3,1,0) 功能3,
count(decode(fun_id,4,1,0) 功能4,
count(decode(fun_id,5,1,0) 功能5,
count(decode(fun_id,6,1,0) 功能6,
..............
count(decode(fun_id,12,1,0) 功能12
from a
group by user_id, user_name

ypz32 2009-01-09
  • 打赏
  • 举报
回复
如果是水晶报表的话可以用“交叉表专家”。
sql语句实现的话,先在开发环境中获得所有用户功能套餐,
再按照
select user_id, user_name,
count(case when fun_id=1 then 1 end) f1,
count(case when fun_id=2 then 1 end) f2,
count(case when fun_id=3 then 1 end) f3,
...
count(case when fun_id=12 then 1 end) f12
from a
group by user_id, user_name
的格式在开发环境中把sql拼接好,最后执行。

zhuxianjun1 2009-01-09
  • 打赏
  • 举报
回复
我想要的格式如:
user_id, user_name,功能1,功能2.。。。。。。功能12
0001 ,小立 , 10 ,20 。。。。。。12
0002 ,小三 , 0 ,20 。。。。。。5
。。。。。。。。。。。。。。。。。。。。。。。。
butchroller 2009-01-09
  • 打赏
  • 举报
回复

select user_id, user_name,
count(case when fun_id=1 then 1 end) f1,
count(case when fun_id=2 then 1 end) f2,
count(case when fun_id=3 then 1 end) f3,
...
count(case when fun_id=12 then 1 end) f12
from a
group by user_id, user_name
hongqi162 2009-01-09
  • 打赏
  • 举报
回复

你的问题和这个类似


--测试数据
create table test1(id int,cdate varchar2(2),shop varchar2(100), y_com varchar2(100), total int);

insert into test1
select 1,'01','北京','运输公司1',4 from dual union all
select 2,'01','北京','运输公司2',5 from dual union all
select 3,'01','北京','运输公司3',6 from dual union all
select 4,'01','北京','运输公司1',7 from dual union all
select 5,'01','北京','运输公司1',8 from dual union all
select 6,'01','北京','运输公司1',9 from dual union all
select 7,'01','北京','运输公司1',10 from dual;


--行列转换
create or replace procedure getRstData( rst out sys_refcursor)
is
begin
declare
cursor cur is select y_com,sum(total) s
from test1
group by cdate,shop,y_com;

t1 varchar2(100);
t2 varchar2(100);
str varchar2(4000);
begin
str:='select cdate,shop';
open cur;
loop
fetch cur into t1,t2;
exit when cur%notfound;
str:=str||','''||t1||''',''(台数'||t2||')''';
end loop;
str:=str||' from test1 group by cdate,shop';
--dbms_output.put_line(str);
close cur;
open rst for str;
end;
end;


--输出结果
1 01 北京 运输公司1 (台数38) 运输公司2 (台数5) 运输公司3 (台数6)
zhuxianjun1 2009-01-09
  • 打赏
  • 举报
回复
有会写的帮忙一下,谢谢了

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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