我有以下表结构和数据(使用 Oracle DB 12c):
CREATE TABLE authors (
aid NUMBER(38) GENERATED ALWAYS AS IDENTITY CONSTRAINT authors_pk PRIMARY KEY NOT NULL,
fname VARCHAR2(100 CHAR) NOT NULL,
sname VARCHAR2(150 CHAR) NOT NULL,
dob NUMBER(4),
gender CHAR(1 CHAR)
);
CREATE UNIQUE INDEX authors_uind ON authors (fname, sname, dob, gender); --- Combination of columns must be unique
INSERT INTO authors(sname, fname, dob, gender) VALUES('Codd', 'Edgar F', 1923, 'M');
INSERT INTO authors(sname, fname, dob, gender) VALUES('Date', 'Chris J', 1941, 'M');
INSERT INTO authors(sname, fname, dob, gender) VALUES('Darwin', 'Hugh', 1943, 'M');
INSERT INTO authors(sname, fname, dob, gender) VALUES('Lions', 'John', 1937, 'M');
CREATE TABLE publications (
pid NUMBER(38) GENERATED ALWAYS AS IDENTITY CONSTRAINT publications_pk PRIMARY KEY NOT NULL,
title VARCHAR2(150 CHAR) NOT NULL,
written NUMBER(4)
);
CREATE INDEX publications_ind ON publications (title, written);
INSERT INTO publications(title, written) VALUES('A Relational Model of Data for Large Shared Data Banks', 1970);
INSERT INTO publications(title, written) VALUES('The Relational Model for Database Management', 1990);
INSERT INTO publications(title, written) VALUES('An Introduction to Database Systems', 2003);
INSERT INTO publications(title, written) VALUES('The Third Manifesto', 2000);
INSERT INTO publications(title, written) VALUES('Temporal Data and the Relational Model', 2002);
INSERT INTO publications(title, written) VALUES('Database in Depth: Relational Theory for Practitioners', 2005);
INSERT INTO publications(title, written) VALUES('Commentary on UNIX', 1976);
CREATE TABLE author_publications (
aid NUMBER(38) REFERENCES authors (aid),
pid NUMBER(38) REFERENCES publications (pid),
CONSTRAINT author_publications_pk PRIMARY KEY (aid, pid)
);
INSERT INTO author_publications(aid, pid) VALUES(1, 1);
INSERT INTO author_publications(aid, pid) VALUES(1, 2);
INSERT INTO author_publications(aid, pid) VALUES(2, 3);
INSERT INTO author_publications(aid, pid) VALUES(2, 4);
INSERT INTO author_publications(aid, pid) VALUES(2, 5);
INSERT INTO author_publications(aid, pid) VALUES(2, 6);
INSERT INTO author_publications(aid, pid) VALUES(3, 4);
INSERT INTO author_publications(aid, pid) VALUES(3, 5);
INSERT INTO author_publications(aid, pid) VALUES(4, 7);
一个作者可能写了多本书,而一本书是由多个作者写的。
查询应该是什么样的,以便我在每本书(所有书籍)中得到一行,并将作者的姓名连接在一起。例如:
| Written | Title | PID | Authors |
|---------|-------|-----|---------|
| 1990 | THE THIRD MANIFESTO | 4 | DATE, DARWIN |
| 2002 | TEMPORAL DATA AND THE RELATIONAL MODEL | 8 | DARWIN, DATE |
注 1:第四列中作者姓名的顺序并不重要,一行中单个作者姓名后的逗号也不重要。
注 2:GROUP BY
如果可以通过不同的方式获得所需的结果,则不需要使用子句。
我的查询如下:
--- Get a list of author names per title
SELECT p.written, upper(p.title), p.pid, concat(upper(a.sname), ', ')
FROM publications p INNER JOIN author_publications apub on p.pid = apub.pid
INNER JOIN authors a on apub.aid = a.aid
GROUP BY p.pid
order by p.written, upper(p.title)
;
我收到的错误消息是“不是 GROUP BY 表达式”。(我不明白该语句的哪个特定部分失败或为什么失败。)有问题的行是 SELECT 语句的开头。
而不是
CONCAT
,使用LISTAGG
:结果: