将Access Transform / Pivot("交叉表")查询转换为SQL Server 2012

时间:2014-06-10 03:47:32

标签: sql sql-server ms-access ms-access-2010

如何将以下Access交叉表查询转换为SQL Server 2012? 根据我的理解,TRANSFORM和PIVOT不能在Sql Server中使用,但我不确定如何分解下面的代码:

TRANSFORM 
Count(Stats.SessionNumber) AS CountOfSessionNumber
SELECT 
Semesters.Semester, TermOptions.StudentDesc, 
Count(Stats.SessionNumber) AS TotalSessions
FROM Semesters, 
(StudentList_tbl 
INNER JOIN 
((StudentSemestersAndTerms 
INNER JOIN 
Stats ON (StudentSemestersAndTerms.StudentID = Stats.StudentID) 
AND (StudentSemestersAndTerms.Semester = Stats.Semester)) 
INNER JOIN 
TermOptions ON StudentSemestersAndTerms.Q3 = TermOptions.TermID) 
ON StudentList_tbl.StudentID = StudentSemestersAndTerms.StudentID) 
INNER JOIN 
TrainersList ON StudentList_tbl.RTP = TrainersList.RTPID
WHERE 
(((Stats.Semester)<[Semesters].[Semester]))
GROUP BY 
Semesters.Semester, TermOptions.StudentDesc
PIVOT 
TrainersList.ShortName;

示例数据:

-------------------------------------------------------------------------
| Semester | StudentDesc | TotalSessions | FTL  |  GTY  |  MAU  |  CYU  |
-------------------------------------------------------------------------
|        3 | Term 1      |          1420 |  250 |  100  |  50   |  1020 |
-------------------------------------------------------------------------
|        3 | Term 2      |           700 |  200 |   25  |  75   |  300  |
-------------------------------------------------------------------------
|        3 | Term 3      |          1400 |      |   50  | 50    |  1300 |
-------------------------------------------------------------------------

The tables are:
Semesters  
StudentList_tbl
StudentSemestersAndTerms 
Stats
TermOptions
TrainersList

Row Heading
Semesters.Semester
TermOptions.StudentDesc
TrainersList.Shortname

Count
Stats.SessionNumber as TotalSessions

0 个答案:

没有答案