我正在尝试在一个选择语句中使用多个汇总,并且停留在一个值基于另一个汇总的情况下(总收入是将CY
乘以COUNT(TrackID)
得出的)。
不允许我进行CTE,并且尝试使用子查询失败。三行注释掉的行是我失败的尝试(不是失败的子查询尝试,我已经删除了那些)。
Unit Price
我也尝试过这种方法,但是结果被忽略了
SELECT C.Country
,Ar.Name AS 'Artist Name'
,COUNT(T.TrackId) AS 'Track Count'
,COUNT(DISTINCT T.Name) AS 'Unique Track Count'
,COUNT(T.TrackID) - COUNT(DISTINCT T.Name) AS 'Count Difference'
--,POWER(IL.UnitPrice,COUNT(T.TrackId)) AS 'Test'
--,SUM(IL.UnitPrice * COUNT(T.TrackID)) AS 'Total Revenue'
--,IL.UnitPrice * COUNT(T.TrackID) AS 'Total Revenue'
,CASE MediaTypeId
WHEN 3
THEN 'Video'
ELSE 'Audio'
END AS MediaType
FROM Artist Ar
JOIN Album Al
ON Al.ArtistId = Ar.ArtistId
JOIN Track T
ON T.AlbumId = Al.AlbumId
JOIN InvoiceLine Il
ON Il.TrackId = T.TrackId
JOIN Invoice I
ON I.InvoiceId = Il.InvoiceId
JOIN Customer C
ON C.CustomerId = I.CustomerId
GROUP BY Ar.Name
,C.Country
,CASE MediaTypeId
WHEN 3
THEN 'Video'
ELSE 'Audio'
END
ORDER BY C.Country ASC, COUNT(T.TrackID) DESC, Ar.Name ASC
答案 0 :(得分:0)
您不能在同一查询中的计算中使用聚合,也不能在聚合中使用聚合,您需要将查询包装为内部查询,并在外部查询中使用聚合进行计算-
Select [columnsNames], [aggregates]
From
(
Your Inner Query
)a
Group By [columnsNames]
Order By [columnsNames]