PL SQL:如何在同一个单元格中获得多个值? listagg,好的。但是

时间:2019-01-03 13:50:44

标签: reporting-services plsql birt

我是stackoverflow的新手,所以如果我问的不正确,请原谅。

我刚收到一张票,要求修改报告。

实际情况是,当一名雇员参加的培训中有一位以上的讲师时,雇员的记录就会被重复,其重复次数是讲师人数的两倍。

例如如下


Stud_id | ins_fname | ins_lname


001 |雨果红色


001 |马可|绿色

应该是


Stud_id | ins_fname | ins_lname


001 | ugo,marco |红色,绿色

我知道有LISTAGG可以解决此问题,但是不能正常工作。

我猜是因为stud_id值是从嵌套表中提取的,所以我正在处理的报告看起来像这样

选择 col_1, col_2, ins_fname, ins_lname, col_n + 1 ... n, 从 (选择区域   stud.stud_id,   col_n + 1 ... n,   从表 )tableName, tableName2, tableName3,

1 = 1 = 1

这是否有可能影响LISTAGG的功能?

我得到的错误如下

选择列表中的列“ I.MI”(紧随我要列出的列之后)无效,因为GROUP BY子句或聚合函数不包含它:第26行第7行(在pos 1010)     在de.simplicit.vjdbc.util.SQLExceptionHelper.wrapSQLException(SQLExceptionHelper.java:44)     在de.simplicit.vjdbc.util.SQLExceptionHelper.wrap(SQLExceptionHelper.java:25)     在de.simplicit.vjdbc.server.command.CommandProcessor.process(CommandProcessor.java:165)     在de.simplicit.vjdbc.server.servlet.ServletCommandSink.handleRequest(ServletCommandSink.java:176)     在de.simplicit.vjdbc.server.servlet.ServletCommandSink.doPost(ServletCommandSink.java:150)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:648)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain。

我知道这不是最明确的问题,但我仍然非常感谢任何支持。

我使用的编译器是Plateau Report Designer,它是在链接到ORACLE db的BIRT之上构建的

谢谢大家,新年快乐

1 个答案:

答案 0 :(得分:1)

我认为我了解您的问题,并且与分组内容有关!

我花了一些时间来重新创建您的请求,这就是我使用Oracle的LiveSQL可以做到的

https://livesql.oracle.com/apex/livesql/s/hr0w91uigngd7wg5895ov7vc7

如果您无权使用Oracle,则可以创建一个免费帐户,但是以下是我编写的代码。

DROP TABLE training_instructor;
DROP TABLE training_enroll;
DROP TABLE employee;
DROP TABLE instructor;
DROP TABLE training;

CREATE TABLE employee(
  employee_id    NUMBER PRIMARY KEY
 ,employee_name  VARCHAR2(100)
);

CREATE TABLE instructor(
  instructor_id    NUMBER PRIMARY KEY
 ,instructor_name  VARCHAR2(100)
);

CREATE TABLE training(
  training_id    NUMBER   PRIMARY KEY
 ,training_name   VARCHAR2(100)
);

CREATE TABLE training_instructor(
  training_id    NUMBER
 ,instructor_id  NUMBER
 ,PRIMARY KEY(training_id, instructor_id)
);

CREATE TABLE training_enroll(
  training_id    NUMBER
 ,employee_id    NUMBER
 ,PRIMARY KEY(training_id, employee_id)
);

INSERT INTO employee VALUES(1,  'Employee 1');
INSERT INTO employee VALUES(2,  'Employee 2');
INSERT INTO employee VALUES(3,  'Employee 3');
INSERT INTO employee VALUES(4,  'Employee 4');
INSERT INTO employee VALUES(5,  'Employee 5');
INSERT INTO employee VALUES(6,  'Employee 6');
INSERT INTO employee VALUES(7,  'Employee 7');
INSERT INTO employee VALUES(8,  'Employee 8');
INSERT INTO employee VALUES(9,  'Employee 9');
INSERT INTO employee VALUES(10, 'Employee 10');
COMMIT;

INSERT INTO instructor VALUES(1, 'Instructor 1');
INSERT INTO instructor VALUES(2, 'Instructor 2');
INSERT INTO instructor VALUES(3, 'Instructor 3');
INSERT INTO instructor VALUES(4, 'Instructor 4');
INSERT INTO instructor VALUES(5, 'Instructor 5');
COMMIT;


INSERT INTO training VALUES(1, 'Training 1');
INSERT INTO training VALUES(2, 'Training 2');
INSERT INTO training VALUES(3, 'Training 3');
INSERT INTO training VALUES(4, 'Training 4');
INSERT INTO training VALUES(5, 'Training 5');
COMMIT;


INSERT INTO training_instructor VALUES(1, 1);
INSERT INTO training_instructor VALUES(2, 2);
INSERT INTO training_instructor VALUES(2, 3);
INSERT INTO training_instructor VALUES(3, 1);
INSERT INTO training_instructor VALUES(3, 3);
INSERT INTO training_instructor VALUES(3, 5);
INSERT INTO training_instructor VALUES(4, 2);
INSERT INTO training_instructor VALUES(4, 4);
INSERT INTO training_instructor VALUES(5, 4);
INSERT INTO training_instructor VALUES(5, 5);
COMMIT;


INSERT INTO training_enroll VALUES(1, 1);
INSERT INTO training_enroll VALUES(1, 2);
INSERT INTO training_enroll VALUES(1, 3);
INSERT INTO training_enroll VALUES(2, 6);
INSERT INTO training_enroll VALUES(2, 8);
INSERT INTO training_enroll VALUES(2, 10);
INSERT INTO training_enroll VALUES(2, 5);
INSERT INTO training_enroll VALUES(2, 4);
INSERT INTO training_enroll VALUES(3, 3);
INSERT INTO training_enroll VALUES(3, 5);
INSERT INTO training_enroll VALUES(3, 1);
INSERT INTO training_enroll VALUES(5, 10);
INSERT INTO training_enroll VALUES(5, 6);
COMMIT;
/
-- The final query
SELECT e.employee_id 
      ,e.employee_name 
      ,t.training_name 
      ,LISTAGG(i.instructor_name, ' | ')  WITHIN GROUP (ORDER BY i.instructor_name) instructor_list 
  FROM employee e 
       JOIN training_enroll te 
         ON e.employee_id = te.employee_id 
       JOIN training t 
         ON te.training_id = t.training_id 
       JOIN training_instructor ti 
         ON te.training_id = ti.training_id 
       JOIN instructor i 
         ON ti.instructor_id = i.instructor_id 
GROUP BY e.employee_id, e.employee_name, t.training_name 
ORDER BY e.employee_id, t.training_name