查询如下,我收到错误:
SELECT a.name
FROM author a, catalog c
WHERE a.authorid = c.authorid
AND c.bookid IN (
SELECT bookid
FROM orderdetails
GROUP BY bookid
HAVING sum(quantity) = (
SELECT max(sum(quantity))
FROM orderdetails
)
);
以下表格由图书经销商维护。
AUTHOR (author-id:int, name:string, city:string, country:string)
PUBLISHER (publisher-id:int, name:string, city:string, country:string)
CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int, price:int)
CATEGORY (category-id:int, description:string)
ORDER-DETAILS (order-no:int, book-id:int, quantity:int)
问题是:
IV。找到具有最大销售额的图书的作者。
答案 0 :(得分:0)
可能是你需要一个中间组
select a.name
from author a
inner join catalog c on a.authorid=c.authorid
and c.bookid in (select bookid
from orderdetails
group by bookid
having sum(quantity) = (
select max( sum_quantity) from (
select sum(quantity) sum_quantity
from orderdetails
group by bookid
)
);
答案 1 :(得分:0)
您收到错误,因为只有您选择的列:
SELECT a.name
FROM author a, catalog c
WHERE a.authorid = c.authorid
AND c.bookid IN (
SELECT bookid, sum(quantity) as total_qty
FROM orderdetails
GROUP BY bookid
HAVING total_qty = (
SELECT max(sum(quantity))
FROM orderdetails
)
);
我认为你可以避免IN更好地尝试获得不那么重的查询:
SELECT a.name
FROM author a
INNER JOIN catalog c ON a.authorid = c.authorid
INNER JOIN (
SELECT bookid, sum(quantity) as total_qty
FROM orderdetails
GROUP BY bookid
HAVING total_qty = (
SELECT max(sum(quantity))
FROM orderdetails
)
) t ON t.bookid = c.bookid