我正在尝试将我的子查询的MAX值表示为NUM_of_CUST列,并将其与tge主查询中的艺术家名称一起显示,以便显示如下:
ARTIST_NAME | NUM_of_CUST
Select A.Lastname ||''|| A.FirstName as Artist_Name from dhey.ARTIST A
LEFT JOIN
(SELECT * FROM(
SELECT COUNT(*) As Num_of_Customer From Dhey.CUSTOMER_ARTIST_INT CAI
GROUP BY ARTISTID
ORDER BY COUNT(*) desc) WHERE ROWNUM = 1) B on A.ArtistID = B.ArtistID;
然而,SQL正在向我抛出B“。”ARTISTID“:无效的标识符异常。
有人可以就此提出建议吗?
谢谢!
答案 0 :(得分:2)
我真的不明白你想做什么,但我认为要么你想要每个艺术家的顾客数量
Select A.Lastname ||''|| A.FirstName as Artist_Name
COUNT(*) as nb_cust
from dhey.ARTIST A
LEFT outer JOIN Dhey.CUSTOMER_ARTIST_INT B on A.ArtistID = B.ArtistID
group by A.ArtistID, A.Lastname ||''|| A.FirstName
order by nb_cust desc
或者您想要最大的客户总数并将其链接到您的艺术家......这是一行的交叉产品。
Select A.Lastname ||''|| A.FirstName as Artist_Name
nb_cust_max
from dhey.ARTIST A
cross join ( select max(nb_cust) as nb_cust_max
from ( select count(*) as nb_cust
From Dhey.CUSTOMER_ARTIST_INT
group by ARTISTID
)
)
我关门了吗?还是完全错了?
否则,正如 MatBailie 所示,您只需添加缺少的字段
Select A.Lastname ||''|| A.FirstName as Artist_Name from dhey.ARTIST A
LEFT JOIN
(SELECT * FROM(
SELECT ARTISTID, COUNT(*) As Num_of_Customer From Dhey.CUSTOMER_ARTIST_INT CAI
GROUP BY ARTISTID
ORDER BY COUNT(*) desc) WHERE ROWNUM = 1) B on A.ArtistID = B.ArtistID;
答案 1 :(得分:0)
我用这个解决了我的答案
SELECT A.lastname ||''||A.firstname as Artist_Name, COUNT(CAI.ARTISTID) AS NumberOfCustomers
FROM dhey.CUSTOMER_ARTIST_INT CAI
LEFT JOIN dhey.CUSTOMER C
ON CAI.CUSTOMERID = C.CUSTOMERID
LEFT JOIN dhey.ARTIST A
ON CAI.ARTISTID = A.ARTISTID
group by A.lastname, A.firstname
having count(*) = (select max(count(ArtistID))
from dhey.CUSTOMER_ARTIST_INT
group by ArtistID);
谢谢大家的帮助!