如何防止varchar追加重复信息
How to prevent Varchar Appending Duplicate Information
我正在学习 PL/SQL Oracle SQL Developer 中的存储函数,但我在尝试创建 returns 行不包含重复信息的函数时遇到了问题。
架构信息:
- 有 ANUMBER(申请人编号)的 APPLICANT
- SPOSSESSED(拥有的技能)有一个 SNAME(技能名称)和一个数字(给 link 申请技能的人)
- 有 PNUMBER 的 POSITION
- SNEEDED 具有 SNAME 和 PNUMBER link 职位所需的技能
任务本质上是有一个函数,该函数接受一个数字和 returns 一个字符串,其中包含申请人具有技能的可用职位。
我当前的代码:
CREATE OR REPLACE FUNCTION applicant_position_titles(anum NUMBER) RETURN VARCHAR IS
course_list VARCHAR(300);
first_position POSITION.TITLE%TYPE;
current_position POSITION.TITLE%TYPE;
BEGIN
course_list := '';
FOR spossessed_cursor IN (SELECT sname FROM SPOSSESSED WHERE anumber = anum)
LOOP
FOR sneeded_cursor IN (SELECT PNUMBER FROM SNEEDED WHERE spossessed_cursor.sname = sname)
LOOP
FOR position_cursor IN (SELECT TITLE FROM POSITION WHERE sneeded_cursor.PNUMBER = PNUMBER)
LOOP
course_list := course_list || ' ' || position_cursor.title;
END LOOP;
END LOOP;
END LOOP;
RETURN course_list;
END applicant_position_titles;
/
我的Select声明:
SELECT anumber, applicant_position_titles(anumber)
FROM APPLICANT
WHERE applicant_position_titles(anumber) IS NOT NULL;
结果:
ANUMBER
APPLICANT_POSITION_TITLES(ANUMBER)
1
lecturer lecturer lecturer senior lecturer lecturer professor professor professor
我想知道如何优化此代码功能以防止重复位置。
例如,对于第一行,我希望第 2 列具有:
ANUMBER
APPLICANT_POSITION_TITLES(ANUMBER)
1
lecture senior lecturer professor
我知道它正在发生,因为每个技能都可以应用于多个位置,但我不知道解决此问题的最佳方法是什么。我尝试了一些方法,例如存储和比较 VARCHARS,但似乎没有任何效果。
我还在学习SQL,请放轻松我这恶心的代码。谢谢 :)
@OldProgrammer 是对的,您可以在一个 select 语句中完成。
这是我的样本 table 和数据:
create table SPOSSESSED (sname varchar2(30), anumber number);
create table sneeded (sname varchar2(30), pnumber number);
create table "position" (title varchar2(30), pnumber number);
-------------------------------------
insert into SPOSSESSED values('name',1);
insert into SPOSSESSED values('name2',1);
insert into SPOSSESSED values('name3',1);
--------------------------------------
insert into sneeded values ('name',111);
insert into sneeded values ('name2',222);
insert into sneeded values ('name3',222);
--------------------------------------------
insert into "position" values ('lecturer',111);
insert into "position" values ('professor',222);
这是一个 select 语句:
select sp.anumber, LISTAGG(p.title,' ') WITHIN GROUP (ORDER BY sp.anumber) AS title
from spossessed sp,sneeded sn,"position" p
where
sp.sname=sn.sname and
p.pnumber=sn.pnumber
group by sp.anumber
结果:
ANUMBER | TITLE
1 lecturer professor
删除相同职位标题的编辑:
select anumber, LISTAGG(title,' ')
WITHIN GROUP (ORDER BY anumber) AS TITLE
from (
select distinct sp.anumber, p.title
from spossessed sp,sneeded sn,"position" p
where sp.sname=sn.sname and p.pnumber=sn.pnumber
)
group by anumber;
我正在学习 PL/SQL Oracle SQL Developer 中的存储函数,但我在尝试创建 returns 行不包含重复信息的函数时遇到了问题。
架构信息:
- 有 ANUMBER(申请人编号)的 APPLICANT
- SPOSSESSED(拥有的技能)有一个 SNAME(技能名称)和一个数字(给 link 申请技能的人)
- 有 PNUMBER 的 POSITION
- SNEEDED 具有 SNAME 和 PNUMBER link 职位所需的技能
任务本质上是有一个函数,该函数接受一个数字和 returns 一个字符串,其中包含申请人具有技能的可用职位。
我当前的代码:
CREATE OR REPLACE FUNCTION applicant_position_titles(anum NUMBER) RETURN VARCHAR IS
course_list VARCHAR(300);
first_position POSITION.TITLE%TYPE;
current_position POSITION.TITLE%TYPE;
BEGIN
course_list := '';
FOR spossessed_cursor IN (SELECT sname FROM SPOSSESSED WHERE anumber = anum)
LOOP
FOR sneeded_cursor IN (SELECT PNUMBER FROM SNEEDED WHERE spossessed_cursor.sname = sname)
LOOP
FOR position_cursor IN (SELECT TITLE FROM POSITION WHERE sneeded_cursor.PNUMBER = PNUMBER)
LOOP
course_list := course_list || ' ' || position_cursor.title;
END LOOP;
END LOOP;
END LOOP;
RETURN course_list;
END applicant_position_titles;
/
我的Select声明:
SELECT anumber, applicant_position_titles(anumber)
FROM APPLICANT
WHERE applicant_position_titles(anumber) IS NOT NULL;
结果:
ANUMBER | APPLICANT_POSITION_TITLES(ANUMBER) |
---|---|
1 | lecturer lecturer lecturer senior lecturer lecturer professor professor professor |
我想知道如何优化此代码功能以防止重复位置。
例如,对于第一行,我希望第 2 列具有:
ANUMBER | APPLICANT_POSITION_TITLES(ANUMBER) |
---|---|
1 | lecture senior lecturer professor |
我知道它正在发生,因为每个技能都可以应用于多个位置,但我不知道解决此问题的最佳方法是什么。我尝试了一些方法,例如存储和比较 VARCHARS,但似乎没有任何效果。
我还在学习SQL,请放轻松我这恶心的代码。谢谢 :)
@OldProgrammer 是对的,您可以在一个 select 语句中完成。 这是我的样本 table 和数据:
create table SPOSSESSED (sname varchar2(30), anumber number);
create table sneeded (sname varchar2(30), pnumber number);
create table "position" (title varchar2(30), pnumber number);
-------------------------------------
insert into SPOSSESSED values('name',1);
insert into SPOSSESSED values('name2',1);
insert into SPOSSESSED values('name3',1);
--------------------------------------
insert into sneeded values ('name',111);
insert into sneeded values ('name2',222);
insert into sneeded values ('name3',222);
--------------------------------------------
insert into "position" values ('lecturer',111);
insert into "position" values ('professor',222);
这是一个 select 语句:
select sp.anumber, LISTAGG(p.title,' ') WITHIN GROUP (ORDER BY sp.anumber) AS title
from spossessed sp,sneeded sn,"position" p
where
sp.sname=sn.sname and
p.pnumber=sn.pnumber
group by sp.anumber
结果:
ANUMBER | TITLE
1 lecturer professor
删除相同职位标题的编辑:
select anumber, LISTAGG(title,' ')
WITHIN GROUP (ORDER BY anumber) AS TITLE
from (
select distinct sp.anumber, p.title
from spossessed sp,sneeded sn,"position" p
where sp.sname=sn.sname and p.pnumber=sn.pnumber
)
group by anumber;