将nvarchar转换为int以在查询结果中添加小计行

时间:2017-04-19 16:10:38

标签: sql-server-2014

我正在尝试在查询结果中添加小计行,我试图计算" Y"学校在结果中的值,在这种情况下,它们是where子句逻辑。在谷歌搜索我明白我需要将我的II.ApprovedByPrincipal转换为INT值,这是我在FIRST选择中所做的,然后在UNION ALL语句的SECOND选择中做了COUNT但是我得到了这个错误:

转换nvarchar值时转换失败' Y'数据类型int。

任何有关如何实现这一目标的帮助/指示都将不胜感激,谢谢。

这是我的代码:

SELECT II.EnrolledSchool, II.syear, II.student_id, II.last_name, II.first_name,
    II.AddressSchool, II.Income_SchoolYear, II.NumberInHousehold, 
    II.HouseholdMember, II.PayType, II.Income, II.AddedOn, II.AddedBy,
    II.CompletedEarlyChildhoodDocumentation, CAST(II.ApprovedByPrincipal as int) as ApprovedByPrincipal, II.ApprovedByEarlyChildhood,
    QI.QualifyingInfo_SchoolYear, QI.MethodOfQualifying, QI.Notes, QI.ApprovedBy, QI.ApprovedDate
FROM II
INNER JOIN QI
ON II.student_id = QI.student_id
WHERE II.ApprovedByPrincipal IS NOT NULL
AND II.ApprovedByEarlyChildhood IS NULL
UNION ALL
SELECT II.EnrolledSchool+' Subtotal', null, null, null, null,
    null, null, null,
    null, null, null, null, null,
    null, count(II.ApprovedByPrincipal), null,
    null, null, null, null, null
FROM II
INNER JOIN QI
ON II.student_id = QI.student_id
WHERE II.ApprovedByPrincipal IS NOT NULL
AND II.ApprovedByEarlyChildhood IS NULL
GROUP BY II.EnrolledSchool
ORDER BY II.EnrolledSchool asc, II.last_name asc, II.first_name asc

0 个答案:

没有答案