Example of What I'm trying to do使用TSQL:
我有两个桌子,一个学生桌子和一个比赛桌子。学生表中的每个学生都有1条记录。种族表中的每个学生都有多个记录,每个种族1个。我正在尝试将表格合并成一个视图,其中每个学生都有1条记录,并且他们的每个种族都在单独的列中列出。任何帮助或建议,我们将不胜感激!
我尝试使用简单的case语句,但最终每个学生有多个输出记录。我认为我可能需要使用相关的子查询,但是我并没有太多运气使它起作用。
SELECT DISTINCT
st.[STUDENTS First_Name], st.[STUDENTS Last_Name], st.[STUDENTS
State_StudentNumber], CASE WHEN sr.[STUDENTRACE RaceCd] = 'W' THEN 'W' END White, CASE WHEN sr.[STUDENTRACE RaceCd] = 'B' THEN 'B' END Black, CASE WHEN sr.[STUDENTRACE RaceCd] = 'I' THEN 'I' END Indian,
CASE WHEN sr.[STUDENTRACE RaceCd] = 'A' THEN 'A' END Asian, CASE WHEN sr.[STUDENTRACE RaceCd] = 'P' THEN 'P' END [Pacific Islander], CASE WHEN sr.[STUDENTRACE RaceCd] = 'H' THEN 'H' END Hispanic, CASE WHEN sr.[STUDENTRACE RaceCd] = 'M' THEN 'M' END [Multiple Races]
FROM ps_students_export AS st
JOIN ps_StudentRace_export AS sr on sr.[STUDENTRACE StudentID] = st.[STUDENTS ID]
WHERE st.[STUDENTS Enroll_Status] = 0
GROUP BY st.[STUDENTS First_Name], st.[STUDENTS Last_Name], st.[STUDENTS State_StudentNumber], sr.[STUDENTRACE RaceCd]
ORDER BY st.[STUDENTS State_StudentNumber];
请查看链接。我认为这有助于解释我的表的外观以及最终结果。