我有 4 张桌子。一种叫做艺术家。这是表结构:
artistID lastname firstname nationality dateofbirth datedcease
另一张表称为工作
workId title copy medium description artist ID
转表
TransactionID Date Acquired Acquistionprice datesold
askingprice salesprice customerID workID
客户表
customerID lastname Firstname street city state
zippostalcode country areacode phonenumber email
第一个问题是哪位艺术家的作品售出最多,以及该艺术家的作品已售出多少。
我的 SQL 查询如下
SELECT *
FROM dtoohey.artist A1
INNER JOIN
(SELECT
COUNT(W1.ArtistID) AS COUNTER, artistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.work W1 ON W1.workid = T1.Workid
GROUP BY W1.artistID) TEMP1 ON TEMP1.artistID = A1.artistID
WHERE
A1.artistID = TEMP1.artistId
ORDER BY
COUNTER desc;
我要得到整张桌子,但我只想显示最高计数的第一行 - 我该怎么做?
qns 2 是哪位艺术家的作品获得最高平均利润的销售额(即艺术家每次销售作品所获得的平均利润),以及该金额是多少。
我的 SQL 查询如下
SELECT
A1.artistid, A1.firstname
FROM
(SELECT
(salesPrice - AcquisitionPrice) as profit,
w1.artistid as ArtistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.WORK W1 ON W1.workid = T1.workid) TEMP1
INNER JOIN
dtoohey.artist A1 ON A1.artistID = TEMP1.artistID
GROUP BY
A1.artistid
HAVING
MAX(PROFIT) = AVG(PROFIT);
我无法执行它