我有 4 个具有以下结构的表:
表artist
:
artistID lastname firstname nationality dateofbirth datedcease
表work
:
workId title copy medium description artist ID
表Trans
:
TransactionID Date Acquired Acquistionprice datesold askingprice salesprice customerID workID
表Customer
:
customerID lastname Firstname street city state zippostalcode country areacode phonenumber email
第一个问题是哪位艺术家的作品最多,artsold
以及有多少艺术家的作品被售出。
我的 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;
我要得到整张桌子,但我只想显示最高计数的第一行我该怎么做?
我已尝试插入WHERE ROWNUM <=1
,但它显示艺术家 ID 为 1
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);
我无法执行它
我已经尝试了下面的查询,但仍然无法得到它不断收到错误丢失右括号
SELECT A1.artistid, A1.firstname, TEMP1.avgProfit
FROM
(
SELECT
AVG(salesPrice - AcquisitionPrice) as avgProfit,
W1.artistid as artistid
FROM dtoohey.trans T1
INNER JOIN dtoohey.WORK W1
ON W1.workid = T1.workid
GROUP BY artistid
ORDER BY avgProfit DESC
LIMIT 1
) TEMP1
INNER JOIN dtoohey.artist A1
ON A1.artisid = TEMP1.artistid