目标:我正在尝试为每个学校添加一个摘要行,我在Fiddle中设置了这个。
需要:
列出项目我想要史密斯小学,琼斯小学等每年的报名总数。
列表项我还想拥有所有级别的摘要行(ES,MS,HS)。
列表项我还希望所有级别都有一个总计行。
这在TSQL中是否可行?我正在运行SQL Server 2008 R2。
SELECT
schoolid,
sitename,
level,
area,
grade,
20122013ActualEnrollment ,
20132014ActualEnrollment,
20152016ActualEnrollment
FROM supportContacts
ORDER BY
CASE
WHEN Level= 'ES' THEN '1'
WHEN Level= 'MS' THEN '2'
WHEN Level= 'HS' THEN '3'
ELSE '4'
END
, SiteName
, CASE
WHEN Grade = 'K' THEN '1'
WHEN Grade = '1' THEN '2'
WHEN Grade = '2' THEN '3'
WHEN Grade = '3' THEN '4'
WHEN Grade = '4' THEN '5'
WHEN Grade = '5' THEN '6'
ELSE '7'
END
输出:
感谢您的帮助!
答案 0 :(得分:0)
请注意这在黑暗中几乎是一个镜头。写这篇文章时,我没有结构或样本数据。
SELECT
SY12.[SchoolID]
, MAX(SY12.[SiteName])
, MAX(SY12.[Level])
, MAX(S.[AreaName])
, SY12.[Grade]
, SY12.[TotalEnrollment] AS '2012-2013 Actual Enrollment'
, SY13.[TotalEnrollment] AS '2013-2014 Actual Enrollment'
, P.[2016] AS '2015-2016 Projected Enrollment'
FROM
[2012Cycle1_Data] AS SY12
LEFT OUTER JOIN
[2013Cycle1_Data] AS SY13 ON SY12.SchoolID = SY13.[SchoolID]
AND SY12.Grade = SY13.Grade
LEFT OUTER JOIN
[Projections] AS P ON SY12.SchoolID = P.[SchoolID]
AND SY12.Grade = P.Grade
LEFT OUTER JOIN
v_Sites AS S ON SY12.SchoolID = S.SchoolID
GROUP BY SY12.[SchoolID], SY12.[Grade]
ORDER BY
CASE
WHEN SY12.Level= 'ES' THEN '1'
WHEN SY12.Level= 'MS' THEN '2'
WHEN SY12.Level= 'HS' THEN '3'
ELSE '4'
END
, SiteName
, CASE
WHEN SY12.Grade = 'K' THEN '1'
WHEN SY12.Grade = '1' THEN '2'
WHEN SY12.Grade = '2' THEN '3'
WHEN SY12.Grade = '3' THEN '4'
WHEN SY12.Grade = '4' THEN '5'
WHEN SY12.Grade = '5' THEN '6'
ELSE '7'
END
看起来我在查询中有拼写错误。 :(
SELECT
schoolid,
MAX(sitename),
MAX(level),
MAX(area),
grade,
SUM(20122013ActualEnrollment),
SUM(20132014ActualEnrollment),
SUM(20152016ActualEnrollment)
FROM supportContacts
GROUP BY schoolid, grade WITH ROLLUP
这适合你的小提琴。我知道它不是按顺序排列的,但也许你可以使用它吗?