设为首页 加入收藏

TOP

常见SQL面试题2
2014-11-24 00:58:28 来源: 作者: 【 】 浏览:7
Tags:常见 SQL 试题

原表:
courseid coursename score
————————————-
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
————————————-
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseid coursename score mark
—————————————————
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass
—————————————————
写出此查询语句


没有装ORACLE,没试过
select courseid, coursename ,score ,decode(sign(score-60),-1,’fail’,'pass’) as mark from course


完全正确


SQL> desc course_v
Name Null Type
—————————————– ——– —————————-
COURSEID NUMBER
COURSENAME VARCHAR2(10)
SCORE NUMBER


SQL> select * from course_v;


COURSEID COURSENAME SCORE
———- ———- ———-
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80


SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,’fail’,'pass’) as mark from course_v;


COURSEID COURSENAME SCORE MARK
———- ———- ———- —-
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass


*******************************************************************************


原表:


id proid proname
1 1 M
1 2 F
2 1 N
2 2 G
3 1 B
3 2 A
查询后的表:


id pro1 pro2
1 M F
2 N G
3 B A
写出查询语句


解决方案


sql求解
表a
列 a1 a2
记录 1 a
1 b
2 x
2 y
2 z
用select能选成以下结果吗?
1 ab
2 xyz
使用pl/sql代码实现,但要求你组合后的长度不能超出oracle varchar2长度的限制。
下面是一个例子
create or replace type strings_table is table of varchar2(20);
/
create or replace function merge (pv in strings_table) return varchar2
is
ls varchar2(4000);
begin
for i in 1..pv.count loop
ls := ls || pv(i);
end loop;
return ls;
end;
/
create table t (id number,name varchar2(10));
insert into t values(1,’Joan’);
insert into t values(1,’Jack’);
insert into t values(1,’Tom’);
insert into t values(2,’Rose’);
insert into t values(2,’Jenny’);


column names format a80;
select t0.id,merge(cast(multiset(select name from t where t.id = t0.id) as strings_table)) names
from (select distinct id from t) t0;


drop type strings_table;
drop function merge;
drop table t;


用sql:


Well if you have a thoretical maximum, which I would assume you would given the legibility of listing hundreds of employees in the way you describe then yes. But the SQL needs to use the LAG function for each employee, hence a hundred emps a hundred LAGs, so kind of bulky.


This example uses a max of 6, and would need more cut n pasting to do more than that.


SQL> select deptno, dname, emps
2 from (
3 select d.deptno, d.dname, rtrim(e.ename ||’, ‘||
4 lead(e.ename,1) over (partition by d.deptno
5 order by e.ename) ||’, ‘||
6 lead(e.ename,2) over (partition by d.deptno
7 order by e.ename) ||’, ‘||
8 lead(e.ename,3) over (partition by d.deptno
9 order by e.ename) ||’, ‘||
10 lead(e.ename,4) over (partition by d.deptno
11 order by e.ename) ||’, ‘||
12 lead(e.ename,5) over (partition by d.deptno
13 order by e.ename),’, ‘) emps,
14 row_number () over (partition by d.deptno
15 order by e.ename) x
16 from emp e, dept d
17 where d.deptno = e.deptno
18 )
19 where x = 1
20 /


DEPTNO DNAME EMPS
——- ———– ——————————————
10 ACCOUNTING CLARK, KING, MILLER
20 RESEARCH ADAMS, FORD, JONES, ROONEY, SCOTT, SMITH
30 SALES ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD


also
先create function get_a2;
create or replace function get_a2( tmp_a1 number)
return varchar2
is
Col_a2 varchar2(4000);
begin
Col_a2:=”;
for cur in (select a2 from unite_a where a1=tmp_a1)
loop
Col_a2=Col_a2||cur.a2;
end loop;
return Col_a2;
end get_a2;


select distinct a1 ,get_a2(a1) from unite_a
1 ABC
2 EFG
3 KMN


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇常见SQL面试题1 下一篇常见SQL面试题3

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: