1

我有 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);

我无法执行它

4

2 回答 2

0

用于limit 1你的第一个问题

于 2012-10-06T10:31:23.733 回答
0

您的第二个问题可以与第一个问题完全相同地解决。只需更换

COUNT(W1.ArtistID)

AVG(salesPrice - AcquisitionPrice) as AvgProfit

然后使用:

ORDER BY AvgProfit DESC
LIMIT 1

完整的查询应该是:

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
于 2012-10-06T10:37:54.277 回答