此查询:
SELECT
COUNT (a.clientid) AS NumChild
,sub.eligtype
FROM
(SELECT
CASE
WHEN eligibilitytyperecalc IN ('1', '4', '5', '7') THEN 'EstCondition'
WHEN eligibilitytyperecalc IN ('2', '6') THEN 'EstDelay'
WHEN eligibilitytyperecalc IN ('3', '8') THEN 'AtRisk'
ELSE 'Missing'
END AS EligType
FROM
ei_archive..closed_fy13_clientstatus13) AS sub
INNER JOIN
ei_archive..closed_fy13_clientstatus13 a ON sub.eligtype = a.eligibilitytyperecalc
WHERE
fy13_finalclientstatus = 14
AND programid <>40
AND programid = 01
GROUP BY
sub.eligtype
返回错误:
Msg 245,Level 16,State 1,Line 1
转换varchar值时转换失败&#39; AtRisk&#39;数据类型smallint。
我理解问题(不同的数据类型)但无法确定CONVERT子句的放置位置。我在第一个select语句,CASE语句和GROUP BY中尝试过它。
答案 0 :(得分:3)
您尝试将值'AtRisk'
转换为无法实现的数值。您可以在子查询中包含数字字段并加入:
SELECT COUNT (a.clientid) AS NumChild
, sub.eligtype
FROM
(SELECT
eligibilitytyperecalc , // include key value
CASE
WHEN eligibilitytyperecalc IN ('1', '4', '5', '7') THEN 'EstCondition'
WHEN eligibilitytyperecalc IN ('2', '6') THEN 'EstDelay'
WHEN eligibilitytyperecalc IN ('3', '8') THEN 'AtRisk'
ELSE 'Missing'
END AS EligType
FROM ei_archive..closed_fy13_clientstatus13) AS sub
INNER JOIN ei_archive..closed_fy13_clientstatus13 a
ON sub.eligibilitytyperecalc = a.eligibilitytyperecalc // join on key field
WHERE fy13_finalclientstatus = 14
AND programid <>40
AND programid = 01
GROUP BY sub.eligtype
答案 1 :(得分:0)
我相信表ei_archive..closed_fy13_clientstatus13的字段eligibilitytyperecalc是一个整数,因此将a.eligibilitytyperecalc转换为char / varchar,因为您要将它与字符串进行比较(ON sub.eligtype = a.eligibilitytyperecalc)。请尝试以下代码
SELECT
COUNT (a.clientid) AS NumChild
,sub.eligtype
FROM
(SELECT
CASE
WHEN eligibilitytyperecalc IN ('1', '4', '5', '7') THEN 'EstCondition'
WHEN eligibilitytyperecalc IN ('2', '6') THEN 'EstDelay'
WHEN eligibilitytyperecalc IN ('3', '8') THEN 'AtRisk'
ELSE 'Missing'
END AS EligType
FROM
ei_archive..closed_fy13_clientstatus13) AS sub
INNER JOIN
ei_archive..closed_fy13_clientstatus13 a ON sub.eligtype = cast(a.eligibilitytyperecalc as char(10))
WHERE
fy13_finalclientstatus = 14
AND programid <>40
AND programid = 01
GROUP BY
sub.eligtype