求助:如何用sql实现以下显示效果

[复制链接]
查看11 | 回复8 | 2013-1-30 22:13:19 | 显示全部楼层 |阅读模式
本帖最后由 yean2013 于 2013-2-21 08:36 编辑
表 EMP 有两个字段,EMP_ID, DEPT_ID,数据如下:
EMP_ID| DEPT_ID
E1 | D1
E2 | D1
E3 | D4
E4 | D2
E5 | D3

要求找出在相同部门工作的两个人,显示效果如下
FIRST-EMPID# SECOND-EMPID#
E1
E2
-------------------
如何用sql 实现如上显示效果?



回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
光SQL是实现不了的,得用游标才能实现
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
select a.DEPT_ID dept,a.EMP_ID as FIRST_EMPID,b.EMP_ID as SECOND_EMPID
from EMP a,EMP b
where a.dept_id=b.dept_id and a.emp_idtanjiaping 发表于 2013-2-21 08:46
光SQL是实现不了的,得用游标才能实现

不要想当然,这种简单的需求何须上游标。
group一下,一个min,一个max
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
只找出有2个人的,,大于2个人的呢?
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
sheep_yang678 发表于 2013-2-21 08:52
不要想当然,这种简单的需求何须上游标。
group一下,一个min,一个max

如果这个部门只有一个人,岂不是会出来结果相同的两个人
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
bedba 发表于 2013-2-21 09:00
如果这个部门只有一个人,岂不是会出来结果相同的两个人

你不能加个where条件么
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
为什么要显示成一行,员工要是很多,看起来也没列方便
回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
本帖最后由 yean2013 于 2013-2-21 09:35 编辑
select a.DEPT_ID dept,a.EMP_ID as FIRST_EMPID,b.EMP_ID as SECOND_EMPID
from EMP a,EMP b
where a.dept_id=b.dept_id and a.emp_id
谢谢bedba,解决。

要求中是要用self-join

回复

使用道具 举报

千问 | 2013-1-30 22:13:19 | 显示全部楼层
用PL/SQL实现了多个员工的显示,如下:declare
---查询部门的人员
v_deptnumber;
v_full_name varchar2(4000);
v_namevarchar2(20);
cursor cur_dept is
select distinct dept from emp where dept is not null order by 1;
cursor cur_name is
select emp_name from emp where dept = v_dept order by 1;
cursor cur_name_n is
select emp_name from emp where dept is null order by 1;
begin
open cur_dept;
loop
fetch cur_dept
into v_dept;
exit when cur_dept%notfound;
open cur_name;
loop
fetch cur_name
into v_name;
exit when cur_name%notfound;
v_full_name := trim(rtrim(v_full_name) || chr(9) || trim(v_name));
end loop;
close cur_name;
dbms_output.put_line(v_dept || ': ' || v_full_name);
v_full_name := '';
end loop;
close cur_dept;
open cur_name_n;
v_full_name := '';
loop
fetch cur_name_n
into v_name;
exit when cur_name_n%notfound;
v_full_name := trim(rtrim(v_full_name) || chr(9) || trim(v_name));
end loop;
dbms_output.put_line('NULL: ' || v_full_name);
close cur_name_n;
end;
/复制代码如下是执行结果:
SQL> create table emp (dept number,emp_name varchar2(20));
insert into emp values (10,'aaa');
Table created.
SQL>
1 row created.
SQL> insert into emp values (10,'bbb');
1 row created.
SQL> insert into emp values (10,'ccc');
1 row created.
SQL> insert into emp values (20,'dddd');
1 row created.
SQL> insert into emp values (20,'eeee');
1 row created.
SQL> insert into emp values (20,'ffff');
1 row created.
SQL> insert into emp values (30,'ggggg');
1 row created.
SQL> insert into emp values (40,'hhhhh');
1 row created.
SQL> insert into emp (emp_name) values ('ii');
1 row created.
SQL> insert into emp (emp_name) values ('jj');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from emp;
DEPT EMP_NAME
---------- ----------------------------------------
10 aaa
10 bbb
10 ccc
20 dddd
20 eeee
20 ffff
30 ggggg
40 hhhhh
ii
jj
10 rows selected.
SQL> set serveroutput on
SQL> declare
2---查询同一个部门的人员
3v_deptnumber;
4v_full_name varchar2(4000);
5v_namevarchar2(20);
6cursor cur_dept is
7select distinct dept from emp where dept is not null order by 1;
8cursor cur_name is
9select emp_name from emp where dept = v_dept order by 1;
10cursor cur_name_n is
11select emp_name from emp where dept is null order by 1;
12begin
13open cur_dept;
14loop
15fetch cur_dept
16into v_dept;
17exit when cur_dept%notfound;
18open cur_name;
19loop
20fetch cur_name
21
into v_name;
22exit when cur_name%notfound;
23v_full_name := trim(rtrim(v_full_name) || chr(9) || trim(v_name));
24end loop;
25close cur_name;
26dbms_output.put_line(v_dept || ': ' || v_full_name);
27v_full_name := '';
28end loop;
29close cur_dept;
30open cur_name_n;
31v_full_name := '';
32loop
33fetch cur_name_n
34into v_name;
35exit when cur_name_n%notfound;
36v_full_name := trim(rtrim(v_full_name) || chr(9) || trim(v_name));
37end loop;
38dbms_output.put_line('NULL: ' || v_full_name);
39close cur_name_n;
40end;
41/
10: aaa bbb ccc
20: ddddeeeeffff
30: ggggg
40: hhhhh
NULL: iijj
PL/SQL procedure successfully completed.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行