这是itpub别人问的问题,我学会了,就留个痕迹!
题目:
Create table Table_A
(
UserType varchar2(50),
LoginDate varchar2(50),
Address varchar2(50),
IsCertified varchar2(50)
);
insert into Table_A values('管理员','20050510','城市','是');
insert into Table_A values('一般用户','20050515','郊区','是');
insert into Table_A values('普通管理员','20050610','郊区','是');
insert into Table_A values('一般用户','20050622','城市','是');
insert into Table_A values('普通管理员','20050620','郊区','是');
/*
查询后的结果:
管理员 普通管理员 一般用户
1 2 2
描述一下解题思路:1、首先创建表,插入数据,这个就不多说啦!
2、然后我先利用count聚合函数来查出管理员有多少,一般用户总数和普通管理员的总数。
3、突然想到可以利用decode函数来将几类用户统计总数来,这个时候用的就是sum了。
4、刚开始用0作为补充,结果就出现1,0,0等这样的结果,感觉不对,于是用null试验了一下,还神了。
5、不过出现的却是行和列都有管理员等信息,不对啊,所以将usertype字段,在select语句后删除,直接保留sum字段。
6、成功!
这类的题估计就是要一个输出的格式吧,否则利用count就可以查出来了。
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 8月 11 14:55:15 2008
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn xusy/xusy123@wx
已连接。
SQL> Create table Table_A
2 (
3 UserType varchar2(50),
4 LoginDate varchar2(50),
5 Address varchar2(50),
6 IsCertified varchar2(50)
7 );
表已创建。
SQL> insert into Table_A values('管理员','20050510','城市','是');
已创建 1 行。
SQL> insert into Table_A values('一般用户','20050515','郊区','是');
已创建 1 行。
SQL> insert into Table_A values('普通管理员','20050610','郊区','是');
已创建 1 行。
SQL> insert into Table_A values('一般用户','20050622','城市','是');
已创建 1 行。
SQL> insert into Table_A values('普通管理员','20050620','郊区','是');
已创建 1 行。
SQL> commit
2 ;
提交完成。
SQL> set linesize 300
SQL> select * from table_a;
USERTYPE LOGINDATE
ADDRESS ISCERTI
FIED
-------------------------------------------------- -----------------------------
--------------------- -------------------------------------------------- -------
-------------------------------------------
管理员 20050510
城市 是
一般用户 20050515
郊区 是
普通管理员 20050610
郊区 是
一般用户 20050622
城市 是
普通管理员 20050620
郊区 是
SQL> select usertype,count(usertype) mount from table_a group by usertype;
USERTYPE MOUNT
-------------------------------------------------- ----------
管理员 1
一般用户 2
普通管理员 2
SQL> select usertype,sum(decode(usertype,'管理员',1,0)) 管理员, sum(decode(usert
ype,'一般用户',1,0)) 一般用户, sum(decode(usertype,'普通管理员',1,0)) 普通管理员
from table_a group by usertype;
USERTYPE 管理员 一般用户 普通管理员
-------------------------------------------------- ---------- ---------- ----------
管理员 1 0 0
一般用户 0 2 0
普通管理员 0 0 2
SQL> select usertype,sum(decode(usertype,'管理员',1,null)) 管理员, sum(decode(us
ertype,'一般用户',1,null)) 一般用户, sum(decode(usertype,'普通管理员',1,null))
普通管理员 from table_a group by usertype;
USERTYPE 管理员 一般用户 普通管理员
-------------------------------------------------- ---------- ---------- ----------
管理员 1
一般用户 2
普通管理员 2
SQL> select sum(decode(usertype,'管理员',1,null)) 管理员, sum(decode(usertype,'
一般用户',1,null)) 一般用户, sum(decode(usertype,'普通管理员',1,null)) 普通管理
员 from table_a;
管理员 一般用户 普通管理员
---------- ---------- ----------
1 2 2
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8620228/viewspace-423013/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8620228/viewspace-423013/