我是初学者, 我使用SQL Server 2008 R2 我有3个表(电影库的数据库) 客户和电影类型, 客户和演员, 客户和董事, 我需要实现的查询是
“显示最喜欢的演员,导演,最喜欢的电影类型(流派) 最喜欢的意思是电影出租次数最多“
上面提到的表有客户ID, 客户名称(适用于有演员的表格) 演员身份,演员姓名, 与这位特定演员拍摄的电影是由顾客拍的, 我在其他表中也是如此(最喜欢的电影类型,最喜欢的导演)
问题是我的客户没有最大价值或者价值相同,但是MAX()在这里不起作用。
请有人帮忙吗?
这是最喜欢的导演表的代码:
enter code here CREATE VIEW CustFavorDirector AS
SELECT CustTakenFilms.CustomerID ,CustTakenFilms.[Customer Name],
ActorDirector.ParticipantID, ActorDirector.[Participant Name], COUNT(ActorDirector.ParticipantID) 'Times was taken'
FROM
(
SELECT
Cust.CustomerID,
Cust.FirstName + ' ' + Cust.LastName AS 'Customer Name' ,
Fil.FilmID, Fil.FilmName
FROM
FilmLibrary.dbo.Customer AS Cust
INNER JOIN
FilmLibrary.dbo.Rental AS Rent
ON
Cust.CustomerID = Rent.CustomerID
INNER JOIN
FilmLibrary.dbo.RentalDetails AS RentDet
ON
Rent.RentalID = RentDet.RentalID
INNER JOIN
FilmLibrary.dbo.FilmCopy AS FilCop
ON
RentDet.FilmCopyID = FilCop.FilmCopyID
INNER JOIN
FilmLibrary.dbo.Film AS Fil
ON
FilCop.FilmID = Fil.FilmID
) AS CustTakenFilms
INNER JOIN
(
SELECT Film.FilmID, Film.FilmName,Part.ParticipantID , Part.FirstName + ' ' + Part.LastName AS 'Participant Name', Rol.RoleTypeName
FROM
FilmLibrary.dbo.Film AS Film
INNER JOIN
FilmLibrary.dbo.FilmDetails AS FilDet
ON
Film.FilmID = FilDet.FilmID
INNER JOIN
FilmLibrary.dbo.Participant AS Part
ON
FilDet.ParticipantID = Part.ParticipantID
INNER JOIN
FilmLibrary.dbo.RoleType AS Rol
ON
FilDet.RoleTypeID = Rol.RoleTypeID
WHERE Rol.RoleTypeName IN ('Director')
) AS ActorDirector
ON
CustTakenFilms.FilmID = ActorDirector.FilmID
GROUP BY CustTakenFilms.CustomerID , CustTakenFilms.[Customer Name],
ActorDirector.ParticipantID, ActorDirector.[Participant Name]
这是我在尝试选择该视图时得到的结果:
CustomerID Customer Name ParticipantID Participant Name Times was taken
111123 Abel Smith 1 Clark Foster 2
111123 Abel Smith 15 Philip Morris 2
111456 Aurora Jones 13 Clay Johnson 2
111456 Aurora Jones 14 Brittany Walker 1
111456 Aurora Jones 15 Philip Morris 1
111789 Alejandro Wil 12 Andrey Konchalovsky 2
111789 Alejandro Wil 14 Brittany Walker 2
112123 Ayana Thomas 12 Andrey Konchalovsky 3
112123 Ayana Thomas 15 Philip Morris 1
112123 Ayana Thomas 16 Xavier Lonso 1
113456 Daniel Bell 12 Andrey Konchalovsky 1
113456 Daniel Bell 13 Clay Johnson 2
113456 Daniel Bell 14 Brittany Walker 3
113456 Daniel Bell 15 Philip Morris 1
答案 0 :(得分:0)
如果您采用已经创建的确切视图,将其包装在CTE中,使用第二个CTE为每个客户获取最大值,然后将其加入到您的第一个CTE中,您就可以获得它。如下所示:
CREATE VIEW CustFavorDirector AS
with CustDirectorRentals as (
SELECT CustTakenFilms.CustomerID ,CustTakenFilms.[Customer Name],
ActorDirector.ParticipantID, ActorDirector.[Participant Name], COUNT(ActorDirector.ParticipantID) 'Times was taken'
FROM
(
SELECT
Cust.CustomerID,
Cust.FirstName + ' ' + Cust.LastName AS 'Customer Name' ,
Fil.FilmID, Fil.FilmName
FROM
FilmLibrary.dbo.Customer AS Cust
INNER JOIN
FilmLibrary.dbo.Rental AS Rent
ON
Cust.CustomerID = Rent.CustomerID
INNER JOIN
FilmLibrary.dbo.RentalDetails AS RentDet
ON
Rent.RentalID = RentDet.RentalID
INNER JOIN
FilmLibrary.dbo.FilmCopy AS FilCop
ON
RentDet.FilmCopyID = FilCop.FilmCopyID
INNER JOIN
FilmLibrary.dbo.Film AS Fil
ON
FilCop.FilmID = Fil.FilmID
) AS CustTakenFilms
INNER JOIN
(
SELECT Film.FilmID, Film.FilmName,Part.ParticipantID , Part.FirstName + ' ' + Part.LastName AS 'Participant Name', Rol.RoleTypeName
FROM
FilmLibrary.dbo.Film AS Film
INNER JOIN
FilmLibrary.dbo.FilmDetails AS FilDet
ON
Film.FilmID = FilDet.FilmID
INNER JOIN
FilmLibrary.dbo.Participant AS Part
ON
FilDet.ParticipantID = Part.ParticipantID
INNER JOIN
FilmLibrary.dbo.RoleType AS Rol
ON
FilDet.RoleTypeID = Rol.RoleTypeID
WHERE Rol.RoleTypeName IN ('Director')
) AS ActorDirector
ON
CustTakenFilms.FilmID = ActorDirector.FilmID
GROUP BY CustTakenFilms.CustomerID , CustTakenFilms.[Customer Name],
ActorDirector.ParticipantID, ActorDirector.[Participant Name]
)
, CustMaxRentals AS (
SELECT CustomerID, max([Times was taken]) AS MaxTimesTaken
FROM CustDirectorRentals
)
SELECT cdr.*
FROM CustDirectorRentals cdr
JOIN CustMaxRentals cmr ON cdr.CutomerId = cmr.CustomerId AND cdr.[Times was taken] = cmr.MaxTimesTaken;