我需要在where子句的表达式中使用列X,也在创建列的表达式中使用列,但我不想按它分组

时间:2012-08-03 19:55:10

标签: group-by case

我想使用以下SQL语句

SELECT TRANSCRIPTDETAIL.PEOPLE_ID, 
MIN(TRANSCRIPTDETAIL.TranscriptDetailId) AS min_transcr_detail_id, 
MIN(TRANSCRIPTDETAIL.START_DATE) AS min_class_start, 
MIN(TRANSCRIPTDETAIL.ACADEMIC_YEAR) AS min_aca_year, 

MIN(CASE TRANSCRIPTDETAIL.ACADEMIC_TERM 
    WHEN 'SPRING' THEN TRANSCRIPTDETAIL.ACADEMIC_YEAR + '1-SPRING' 
        + COALESCE (ACADEMIC.ACADEMIC_RATING, N'') 
    WHEN 'SUMI' THEN TRANSCRIPTDETAIL.ACADEMIC_YEAR + '2-SUMI' 
        + COALESCE (ACADEMIC.ACADEMIC_RATING, N'') 
    WHEN 'FALL' THEN TRANSCRIPTDETAIL.ACADEMIC_YEAR + '2-FALL' 
    + COALESCE (ACADEMIC.ACADEMIC_RATING, N'') 
    ELSE TRANSCRIPTDETAIL.ACADEMIC_YEAR + '*-' 
            + transcriptdetail.academic_term + '*' 
            + COALESCE (ACADEMIC.ACADEMIC_RATING, N'') 
    END) AS student_min_term_track, 

MIN(COALESCE (dbo.ACADEMIC.ACADEMIC_RATING, N'')) AS min_track 

FROM dbo.TRANSCRIPTDETAIL AS TRANSCRIPTDETAIL 
INNER JOIN dbo.ACADEMIC 
ON TRANSCRIPTDETAIL.PEOPLE_CODE_ID = dbo.ACADEMIC.PEOPLE_CODE_ID 
    AND TRANSCRIPTDETAIL.ACADEMIC_YEAR = dbo.ACADEMIC.ACADEMIC_YEAR 
    AND TRANSCRIPTDETAIL.ACADEMIC_TERM = dbo.ACADEMIC.ACADEMIC_TERM 
    AND TRANSCRIPTDETAIL.TRANSCRIPT_SEQ = dbo.ACADEMIC.TRANSCRIPT_SEQ 
    AND TRANSCRIPTDETAIL.ACADEMIC_SESSION = dbo.ACADEMIC.ACADEMIC_SESSION 

WHERE (TRANSCRIPTDETAIL.ACADEMIC_YEAR >= '2011') 

GROUP BY TRANSCRIPTDETAIL.PEOPLE_ID, dbo.ACADEMIC.PEOPLE_CODE_ID, 
dbo.ACADEMIC.PROGRAM, dbo.ACADEMIC.COLLEGE 

ORDER BY TRANSCRIPTDETAIL.PEOPLE_ID, min_class_start, dbo.ACADEMIC.PROGRAM DESC

当我把它放在Microsoft SQL服务器管理器2008中时,它通过每年给我一行而不是每人一行,程序和大学,将该学年添加到该组中。

我正在尝试做什么合法的服务器管理器只是不特别关心它,或者我不能这样做?

1 个答案:

答案 0 :(得分:0)

试试这个:

;WITH CTE AS 
(
SELECT t.PEOPLE_ID, 
a.PEOPLE_CODE_ID, 
a.PROGRAM, 
a.COLLEGE , 
t.TranscriptDetailId, 
t.START_DATE, 
t.ACADEMIC_YEAR, 

-- student term track
CASE t.ACADEMIC_TERM 
    WHEN 'SPRING' THEN t.ACADEMIC_YEAR + '1-SPRING' 
        + COALESCE (A.ACADEMIC_RATING, N'') 
    WHEN 'SUMI' THEN t.ACADEMIC_YEAR + '2-SUMI' 
        + COALESCE (A.ACADEMIC_RATING, N'') 
    WHEN 'FALL' THEN t.ACADEMIC_YEAR + '2-FALL' 
    + COALESCE (A.ACADEMIC_RATING, N'') 
    ELSE t.ACADEMIC_YEAR + '*-' 
            + t.academic_term + '*' 
            + COALESCE (A.ACADEMIC_RATING, N'') 
END AS student_term_track, 

-- track
COALESCE (a.ACADEMIC_RATING, N'') AS track 

FROM dbo.TRANSCRIPTDETAIL t 
INNER JOIN dbo.ACADEMIC a ON t.PEOPLE_CODE_ID = a.PEOPLE_CODE_ID 
    AND t.ACADEMIC_YEAR = a.ACADEMIC_YEAR 
    AND t.ACADEMIC_TERM = a.ACADEMIC_TERM 
    AND t.TRANSCRIPT_SEQ = a.TRANSCRIPT_SEQ 
    AND t.ACADEMIC_SESSION = a.ACADEMIC_SESSION 

WHERE (t.ACADEMIC_YEAR >= '2011') 
)

SELECT PEOPLE_ID, 
MIN(TranscriptDetailId) AS min_transcr_detail_id, 
MIN(START_DATE) AS min_class_start, 
MIN(ACADEMIC_YEAR) AS min_aca_year, 
MIN(student_term_track) AS student_min_term_track
MIN(track) ) AS min_track 
FROm CTE
GROUP BY PEOPLE_ID, PEOPLE_CODE_ID, PROGRAM, COLLEGE 
ORDER BY PEOPLE_ID, MIN(START_DATE), PROGRAM DESC