0

我是初学者,我使用 SQL Server 2008 R2 我有 3 个表(电影库的数据库)客户和电影类型、客户和演员、客户和导演,我需要实现的查询是

“显示最喜欢的演员、导演、最喜欢的电影类型(类型),其中最喜欢的意思是电影被出租的次数最多”

上面提到的表格有客户 ID、客户姓名(对于有演员的表格)演员 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
4

1 回答 1

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;
于 2013-05-15T16:14:04.847 回答