需要在SQL Server中替换名称上的id_parent

时间:2011-04-22 10:43:53

标签: sql sql-server sql-server-2005

我有一个问题:

SELECT Strtbmovie.id_parent,
       Strtbuser.Name  as 'Name',
       Strtbmovie.Name as Movie,
       sum(Strtbmovieatt.CountBeginWatch),
       sum(Strtbmovieatt.CountEndWatch),
       sum(Strtbmovieatt.CountBeginTest),
       sum(Strtbmovieatt.CountEndTest),
       max(Strtbmovieatt.CountTest)
FROM   Strtbmovie
       INNER JOIN Strtbmovieatt
         ON Strtbmovie.Idmovie = Strtbmovieatt.Idmovie
       INNER JOIN Strtbuser
         ON Strtbmovieatt.Iduser = Strtbuser.Iduser
GROUP  BY Strtbmovie.Name,
          Strtbuser.Name,
          Strtbmovie.id_parent  

如何在知名id_parent?

的名称中替换此查询列id_parent

1 个答案:

答案 0 :(得分:3)

您必须使用Strtbmovie表INNER JOIN添加额外的ON Strtbmovie.id_parent = StrtbmovieParent.Idmovie,并按实际名称添加组,如下所示:

SELECT StrtbmovieParent.Name as ParentName,
       Strtbuser.Name  as 'Name',
       Strtbmovie.Name as Movie,
       sum(Strtbmovieatt.CountBeginWatch),
       sum(Strtbmovieatt.CountEndWatch),
       sum(Strtbmovieatt.CountBeginTest),
       sum(Strtbmovieatt.CountEndTest),
       max(Strtbmovieatt.CountTest)
FROM   Strtbmovie
       INNER JOIN Strtbmovieatt
         ON Strtbmovie.Idmovie = Strtbmovieatt.Idmovie
       INNER JOIN Strtbuser
         ON Strtbmovieatt.Iduser = Strtbuser.Iduser
       INNER JOIN Strtbmovie as StrtbmovieParent
         ON Strtbmovie.id_parent = StrtbmovieParent.Idmovie
GROUP  BY Strtbmovie.Name,
          Strtbuser.Name,
          StrtbmovieParent.Name