0

好的,我正在尝试使用此计数函数来计算 case 语句的不同部分。这是现在的工作代码。它会告诉我有人从哪一年开始上学。学年的细分方式是 200703 是 07 年夏季,200801 是 07 年秋季,200802 是 08 年春季。所以案例陈述给了我一个学生开始的学年。我只需要一种简单的方法来计算每个学年有多少一年级学生。我尝试在 case 语句周围使用 Count 并尝试设置不同的方法来计算它,但总是遇到聚合函数错误。任何帮助都会很棒。我在 SQL Server 2008 R2 中写这个

SELECT DISTINCT dbo.student_crs_hist.id_num,   
     sum(dbo.student_crs_hist.credit_hrs) as 'tot_credit_hrs',   
     min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)  as 'first_year',
     'Year' = CASE
     WHEN min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)in ('200703','200801','200802') Then '20007-2008'
     WHEN min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)in ('200803','200901','200902') Then '20008-2009'
     WHEN min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)in ('200903','201001','201002') Then '20009-2010'
     WHEN min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)in ('201003','201101','201102') Then '2010-2011'
     Else '2011-2012'
     END
FROM dbo.student_crs_hist,   
     dbo.degree_history 
WHERE dbo.student_crs_hist.id_num = dbo.degree_history.id_num  and  
        ((dbo.STUDENT_CRS_HIST.YR_CDE in ('2007') and dbo.STUDENT_CRS_HIST.TRM_CDE in ('03')) OR 
       (dbo.student_crs_hist.yr_cde in ( '2008','2009','2010','2011')  AND  dbo.student_crs_hist.trm_cde in ( '01','02','03' ))OR
         (dbo.STUDENT_CRS_HIST.YR_CDE in ('2012') and dbo.STUDENT_CRS_HIST.TRM_CDE in ('01','02'))) and 
      dbo.student_crs_hist.transaction_sts in ( 'C','H' )  AND  
      dbo.student_crs_hist.grade_cde in ( 'A','B','C' )  AND  
      dbo.degree_history.major_1 = 'ASBD' and
      dbo.student_crs_hist.adv_req_cde not in ('COL081','EGL082','EGL092','EGL093','MAT091','MAT092','MAT093','COM091','EGL100','REA100')  
Group By
     dbo.STUDENT_CRS_HIST.ID_NUM 
Having
     sum(dbo.student_crs_hist.credit_hrs) >= '30' and

     (min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE) = '200703' or
     min(dbo.student_crs_hist.yr_cde) in ('2008','2009','2010','2011') or
     MIN(dbo.student_crs_hist.yr_cde+dbo.student_crs_hist.trm_cde) in ('201201','201202'))
    Order By
    min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)
4

1 回答 1

0

试试这个方法:

...
SUM(
   CASE WHEN min(dbo.student_crs_hist.yr_cde+dbo.STUDENT_CRS_HIST.TRM_CDE)in ('200703','200801','200802') Then 1 ELSE 0 END
   ) as FirstYearStudents_07-08
...
于 2013-04-03T20:27:57.820 回答