我的查询子查询中有错误。我一直试图解决这个问题几个小时,直到我最近放弃。任何人都有oracle DBA的专业知识吗?
如何编写此查询?我需要使用聚合max()和sum()的子查询。
select at.au_id,
((price*sales)*royalty_rate),
max(royalty_share)
from royalties, titles, author_titles at
group by royalty_rate
having max(royalty_share) = (select max((price*sales)*royalty_rate)
from author_titles, titles, royalties
group by rollup (royalty_rate, royalty_share, au_id));
以下是我所拥有的,我需要使用按汇总分组或按立方体分组
以下是问题: [1]列出已经/已经获得最大书籍补偿的作者, 并收到赔偿。如果涉及多于1位作者,请列出总薪酬 和每个作者的补偿。提示:出版商分配预付款和版税 评价一本书。此信息可在Royalties表中找到。特许权使用费率是 适用于该书的收入以确定版税支付。金额 向每位作者支付的预付款和特许权使用费由royalty_share确定 author_titles表中的字段。此解决方案涉及子查询以查找最大值 特许权使用费。
您可以将表格复制并粘贴到您想要使用的内容中。谢谢。 我的表是:
DROP TABLE authors;
CREATE TABLE authors
(
au_id CHAR(3) NOT NULL,
fname VARCHAR(15) NOT NULL,
lname VARCHAR(15) NOT NULL,
phone VARCHAR(12) ,
address VARCHAR(20) ,
city VARCHAR(15) ,
state CHAR(2) ,
zip CHAR(5) ,
CONSTRAINT authors_pk PRIMARY KEY (au_id)
);
INSERT INTO authors VALUES('A01','Sarah','Buchman','718-496-7223','75 West 205 St','Bronx','NY','10468');
INSERT INTO authors VALUES('A02','Wendy','Heydemark','303-986-7020','2922 Baseline Rd','Boulder','CO','80303');
INSERT INTO authors VALUES('A03','Hallie','Hull','415-549-4278','3800 Waldo Ave, #14F','San Francisco','CA','94123');
INSERT INTO authors VALUES('A04','Klee','Hull','415-549-4278','3800 Waldo Ave, #14F','San Francisco','CA','94123');
INSERT INTO authors VALUES('A05','Christian','Kells','212-771-4680','114 Horatio St','New York','NY','10014');
INSERT INTO authors VALUES('A06',' ','Kellsey','650-836-7128','390 Serra Mall','Palo Alto','CA','94305');
INSERT INTO authors VALUES('A07','Paddy','O''Furniture','941-925-0752','1442 Main St','Sarasota','FL','34236');
DROP TABLE titles;
CREATE TABLE titles
(
title_id CHAR(3) NOT NULL,
title VARCHAR(40) NOT NULL,
genre VARCHAR(10) ,
pub_id CHAR(3) NOT NULL,
pages INTEGER ,
price DECIMAL(5,2) ,
sales INTEGER ,
pubdate DATE ,
contract SMALLINT NOT NULL,
CONSTRAINT titles_pk PRIMARY KEY (title_id),
CONSTRAINT Titles_pub_fk FOREIGN KEY (Pub_id)
REFERENCES Publishers (Pub_id)
);
INSERT INTO titles VALUES('T01','1977!','history','P01',107,21.99,566,DATE '2000-08-01',1);
INSERT INTO titles VALUES('T02','200 Years of German Humor','history','P03',14,19.95,9566,DATE '1998-04-01',1);
INSERT INTO titles VALUES('T03','Ask Your System Administrator','computer','P02',1226,39.95,25667,DATE '2000-09-01',1);
INSERT INTO titles VALUES('T04','But I Did It Unconsciously','psychology','P04',510,12.99,13001,DATE '1999-05-31',1);
INSERT INTO titles VALUES('T05','Exchange of Platitudes','psychology','P04',201,6.95,201440,DATE '2001-01-01',1);
INSERT INTO titles VALUES('T06','How About Never?','biography','P01',473,19.95,11320,DATE '2000-07-31',1);
INSERT INTO titles VALUES('T07','I Blame My Mother','biography','P03',333,23.95,1500200,DATE '1999-10-01',1);
INSERT INTO titles VALUES('T08','Just Wait Until After School','children','P04',86,10.00,4095,DATE '2001-06-01',1);
INSERT INTO titles VALUES('T09','Kiss My Boo-Boo','children','P04',22,13.95,5000,DATE '2002-05-31',1);
INSERT INTO titles VALUES('T10','Not Without My Faberge Egg','biography','P01',NULL,NULL,NULL,NULL,0);
INSERT INTO titles VALUES('T11','Perhaps It''s a Glandular Problem','psychology','P04',826,7.99,94123,DATE '2000-11-30',1);
INSERT INTO titles VALUES('T12','Spontaneous, Not Annoying','biography','P01',507,12.99,100001,DATE '2000-08-31',1);
INSERT INTO titles VALUES('T13','What Are The Civilian Applications?','history','P03',802,29.99,10467,DATE '1999-05-31',1);
DROP TABLE author_titles;
CREATE TABLE author_titles
(
au_id CHAR(3) NOT NULL,
title_id CHAR(3) NOT NULL,
au_order SMALLINT NOT NULL,
royalty_share DECIMAL(5,2) NOT NULL,
CONSTRAINT authors_titles_pk PRIMARY KEY (au_id, title_id),
CONSTRAINT author_titles_au_fk FOREIGN KEY (au_id)
REFERENCES Authors (au_id),
CONSTRAINT author_titles_titles_pk FOREIGN KEY (title_id)
REFERENCES Titles (title_id)
);
INSERT INTO author_titles VALUES('A01','T01',1,1.0);
INSERT INTO author_titles VALUES('A01','T02',1,1.0);
INSERT INTO author_titles VALUES('A05','T03',1,1.0);
INSERT INTO author_titles VALUES('A03','T04',1,0.6);
INSERT INTO author_titles VALUES('A04','T04',2,0.4);
INSERT INTO author_titles VALUES('A04','T05',1,1.0);
INSERT INTO author_titles VALUES('A02','T06',1,1.0);
INSERT INTO author_titles VALUES('A02','T07',1,0.5);
INSERT INTO author_titles VALUES('A04','T07',2,0.5);
INSERT INTO author_titles VALUES('A06','T08',1,1.0);
INSERT INTO author_titles VALUES('A06','T09',1,1.0);
INSERT INTO author_titles VALUES('A02','T10',1,1.0);
INSERT INTO author_titles VALUES('A03','T11',2,0.3);
INSERT INTO author_titles VALUES('A04','T11',3,0.3);
INSERT INTO author_titles VALUES('A06','T11',1,0.4);
INSERT INTO author_titles VALUES('A02','T12',1,1.0);
INSERT INTO author_titles VALUES('A01','T13',1,1.0);
DROP TABLE royalties;
CREATE TABLE royalties
(
title_id CHAR(3) NOT NULL,
advance DECIMAL(9,2) ,
royalty_rate DECIMAL(5,2) ,
CONSTRAINT royalties_pk PRIMARY KEY (title_id),
constraint roy_Titles_fk FOREIGN KEY (title_id) REFERENCES Titles (title_id)
);
Alter table royalties disable constraint roy_titles_fk ;
INSERT INTO royalties VALUES('T01',10000,0.05);
INSERT INTO royalties VALUES('T02',1000,0.06);
INSERT INTO royalties VALUES('T03',15000,0.07);
INSERT INTO royalties VALUES('T04',20000,0.08);
INSERT INTO royalties VALUES('T05',100000,0.09);
INSERT INTO royalties VALUES('T06',20000,0.08);
INSERT INTO royalties VALUES('T07',1000000,0.11);
INSERT INTO royalties VALUES('T08',0,0.04);
INSERT INTO royalties VALUES('T09',0,0.05);
INSERT INTO royalties VALUES('T10',NULL,NULL);
INSERT INTO royalties VALUES('T11',100000,0.07);
INSERT INTO royalties VALUES('T12',50000,0.09);
INSERT INTO royalties VALUES('T13',20000,0.06);
Alter table royalties enable constraint roy_titles_fk ;
commit;
DROP TABLE publishers;
CREATE TABLE publishers
(
pub_id CHAR(3) NOT NULL,
pname VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state CHAR(2) ,
country VARCHAR(15) NOT NULL,
CONSTRAINT publishers_pk PRIMARY KEY (pub_id)
);
INSERT INTO publishers VALUES('P01','Abatis Publishers','New York','NY','USA');
INSERT INTO publishers VALUES('P02','Core Dump Books','San Francisco','CA','USA');
INSERT INTO publishers VALUES('P03','Schadenfreude Press','Hamburg',NULL,'Germany');
INSERT INTO publishers VALUES('P04','Tenterhooks Press','Berkeley','CA','USA');
对我来说这很难:(
答案 0 :(得分:4)
让我们分步进行。通过不连续的步骤处理任务通常更容易。根据您的尝试,您似乎很清楚您正在努力解决一些基本概念。我希望这有助于您学习概念
-- 1) First you know you are looking for authors, so select all the authors
SELECT a.au_id
FROM authors a
-- 2) Build up all of your table joins since we know we will need data from all these tables
-- the author_titles maps authors to titles, and titles map to royalties. Use the foreign keys provided
SELECT a.au_id
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
-- 3) Figure out the formula for an author's compensation
-- 3a) Notice that the royalties table has NULLs for advance and royalty_rate. Convert these to 0
-- 3b) Notice that the titles table has NULLs for price and sales. Convert these to 0
SELECT a.au_id
,total_compensation = (at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0))
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
-- 4) Summarize the compensation for each author by adding the group by author id
SELECT a.au_id
,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
GROUP BY a.au_id
-- 5) Find the the highest compensation. It is the MAX of the previous query
SELECT highest_compensation = MAX(s.total_compensation)
FROM
(SELECT a.au_id
,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
GROUP BY a.au_id) s
-- 6) Find the author(s) that match the highest compensation by combining 4) and 5)
SELECT t1.au_id
FROM
(SELECT a.au_id
,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
GROUP BY a.au_id) t1
WHERE t1.total_compensation =
(SELECT highest_compensation = MAX(s.total_compensation)
FROM
(SELECT a.au_id
,total_compensation = SUM((at.royalty_share * NVL(r.advance, 0)) + (at.royalty_share * NVL(r.royalty_rate,0) * NVL((t.price * t.sales),0)))
FROM authors a
INNER JOIN author_titles at ON a.au_id = at.au_id
INNER JOIN titles t ON at.title_id = t.title_id
INNER JOIN royalties r ON t.title_id = r.title_id
GROUP BY a.au_id) s)