0
好吧我試圖用這個計數函數來計算case語句的不同部分。這是現在的工作代碼。它會告訴我某年某人在學校上學的年份。學年如何分解是200703是07年夏季,200801是07年秋季,200802是春季08.因此,案例陳述給了我一個學生開始的學術學年。我只需要一個簡單的方法來計算每個學年有多少個一年級學生。我嘗試在case語句中使用Count,並嘗試設置不同的方法來計算它,但總是遇到聚合函數錯誤。任何幫助都會很棒。我在SQL Server寫這篇2008 R2使用計數函數SQL
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)
使用這種整個語句的FROM()表 - 由然後選擇(去混疊)列和組的計數列 – Randy 2013-04-03 19:16:20
謝謝,完美的工作 – 2013-04-03 19:23:00