1.写出你常用得15个Oracle函数,并举例说明其用法
2.现在有个表:A(Id,name,regdate),B(id,groupiud),C(id,name2)
写出下列Sql语句
A. 统计A表中每个月注册用户数
B. 统计A表中有姓名相同的用户数
C. 如果表A中有姓名相同得用户,把姓名相同得查出,写入C表
D. A中ID有多个相同的数据,A中姓名相同得ID只保留注册时间最大的数据
题解
第一题、考察的都是最基本得知识
(1)、nvl(test1,exp1,exp2)当test1不为null时,取exp1的值,反之取exp2的值。
select nvl(deptno,1,0) from dept;
(2)、to_char(exp1,exp2)把日期或数字转换为字符串
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
(3)、to_date(exp1,exp2)把字符串转换为日期
insert into 表 values(to_date('2011-02-03','yyyy-mm-dd'));
(4)、LTRIM(exp1,'字符')删除exp1左边的指定字符
select ltrim(' haha ') from dual;
(5)、RTRIM(exp1,'字符')删除exp1右边的指定字符
select ltrim(' haha ') from dual;
(6)、TRIM(exp1,'字符')删除exp1左右两边的指定字符
select ltrim(' haha ') from dual;
(7)、length(exp1)返回指定exp1字符长度
select length('aaaaa') from dual;
(8)、lengthb(exp1)返回指定exp1字节长度(如有中文时使用)
select lengthb('aaaaa哈哈') from dual;
(9)、substr('exp',begin,len)字符串截取函数
select substr('123456',2,2) from dual;
(10)、avg(exp)求指定列exp的平均值
select avg(exp) from 表;
(11)、sum(exp)求指定列exp的求和
select sum(exp) from 表;
(12)、max(exp)求指定列exp的最大值
select avg(exp) from 表;
(13)、min(exp)求指定列exp的最小值
select avg(exp) from 表;
(14)、abs(exp)返回绝对值
select abs(-1) from dual;
(15)、trunc 截掉小数点后值(整除)
select trunc(10/3) from dual;
第二题、
A.统计每个月得注册数,只需要把时间进行按月分组即可,进行count计数
select count(*) as renshu,to_char(regdate,'yyyymm') from A group by to_char(regdate,'yyyymm');
select count(*) as renshu,to_char(regdate,'mm') from A group by to_char(regdate,'mm');
B. 查询姓名相同,即对姓名进行分组,对姓名进行计数,个数大于1即为重复姓名
select count(*),name from A group by name having count(*)>1;
C. 可以根据第二步得思路进行查询,即A表中得姓名在重复得姓名表中
insert into C SELECT ID,NAME FROM A WHERE NAME IN (select name from A group by name having count(*)>1)
或者是对A表起别名为a,b两个表,A表得id<>b表得id,相等即重复
insert into C select a.id,a.name from A a where a.name in
(select b.name from A b where b.id <>a.id)
D. 查询出相同姓名得最大日期,将不再最大日期得数据删掉即可
delete from A a where regdate not in (select max(regdate) from A b where b.id=a.id and b.name=a.name)
欢迎关注公众号Excel螺丝钉