0

私のセットアップは次のとおりです。3つのテーブルがあります-

  • 学生 (StudentID、FirstName、LastName など)、
  • StudentSemester(StudentID、SemIDなど)、および
  • セメスター(SemID、年)

私の要件は、各学生の詳細を取得することですが、最終学期のみです。論理的には、これは学期番号が最も大きい学期を意味します。クエリを正しく取得できないようです。簡単にするために、「年」は単なる整数です (例: 2000 年、1998 年)。以下は、私がしばらく立ち往生している現在のクエリです。

SELECT dbo.Student.LastName + ' , ' + dbo.Student.FirstName AS Student, dbo.Student.Defence1Date, dbo.Student.Defence2Date, COUNT(StudentSemesters_1.SemID) 
           AS SemesterCount, dbo.Student.EntrySemester + ' - ' +
               (SELECT dbo.StudentSemesters.SemID
                FROM   dbo.StudentSemesters INNER JOIN
                               dbo.ListSemesters ON dbo.StudentSemesters.SemID = dbo.ListSemesters.SemID
                WHERE (dbo.Student.StudentCode = dbo.StudentSemesters.StudentCode)
                GROUP BY dbo.StudentSemesters.SemID, dbo.ListSemesters.Year
                HAVING (dbo.ListSemesters.Year = MAX(dbo.ListSemesters.Year))) AS Expr1
FROM  dbo.Student INNER JOIN
           dbo.StudentSemesters AS StudentSemesters_1 ON dbo.Student.StudentCode = StudentSemesters_1.StudentCode
GROUP BY dbo.Student.LastName, dbo.Student.FirstName, dbo.Student.Defence1Date, dbo.Student.Defence2Date, dbo.Student.EntrySemester, 
           dbo.Student.StudentCode
4

1 に答える 1

0

これを行うと、各学生の最終学期のみの詳細を取得できます。

SELECT 
  s.LastName + ' , ' + s.FirstName   AS Student,  
  s.Defence1Date, 
  s.EntrySemester + ' - ' + s1.SemId AS Expr1
FROM  dbo.Student               AS s
INNER JOIN dbo.StudentSemesters AS S1 ON s.StudentCode = S1.StudentCode
INNER JOIN dbo.ListSemesters    AS lm ON lm.SemID      = s1.SemId
INNER JOIN 
(
    SELECT SemId, MAX(Year) AS MaxYear
    FROM dbo.StudentSemesters
    GROUP BY SemId
) AS s2 ON s2.semId = lm.SemId AND ls.Year = s2.MaxYear;

ただし、 SQL Server 2005 以降を使用している場合は、ウィンドウ関数を使用してこれを行うことができます。

WITH CTE
AS
(
    SELECT 
      s.LastName + ' , ' + s.FirstName   AS Student,  
      s.Defence1Date, 
      s.EntrySemester + ' - ' + s1.SemId AS Expr1,
      ROW_NUMBER() OVER(PARTITION BY s1.SemId
                        ORDER BY Year DESC) AS RowNumber
    FROM  dbo.Student               AS s
    INNER JOIN dbo.StudentSemesters AS S1 ON s.StudentCode = S1.StudentCode
    INNER JOIN dbo.ListSemesters    AS lm ON lm.SemID      = s1.SemId
) 
SELECT 
  Student,
  Defence1Date,
  Expr1,
FROM CTE
WHERE RN = 1;

しかし、これは を取得しませんがSemesterCount、次のようにすることができるかもしれません (これは単なる推測です):

SELECT 
  s.LastName + ' , ' + s.FirstName   AS Student,  
  s.Defence1Date, 
  s1.SemCount,
  s.EntrySemester + ' - ' + s1.SemId AS Expr1
FROM  dbo.Student               AS s
INNER JOIN
(
   SELECT StudentCode, COUNT(SemId) AS SemCount
   FROM dbo.StudentSemesters 
   GROUP BY StudentCode
) AS S1 ON s.StudentCode = S1.StudentCode
INNER JOIN dbo.ListSemesters    AS lm ON lm.SemID      = s1.SemId
INNER JOIN 
(
    SELECT SemId, MAX(Year) AS MaxYear
    FROM dbo.StudentSemesters
    GROUP BY SemId
) AS s2 ON s2.semId = lm.SemId AND ls.Year = s2.MaxYear;
于 2013-04-09T01:18:02.173 に答える