在T-SQL中,我需要为计算值指定一个别名,以便在CASE子句中使用它。
SELECT
RUT,
(11 - ((SUBSTRING(RUT, 1, 1) * 3
+ SUBSTRING(RUT, 2, 1) * 2
+ SUBSTRING(RUT, 3, 1) * 7
+ SUBSTRING(RUT, 4, 1) * 6
+ SUBSTRING(RUT, 5, 1) * 5
+ SUBSTRING(RUT, 6, 1) * 4
+ SUBSTRING(RUT, 7, 1) * 3
+ SUBSTRING(RUT, 8, 1) * 2) % 11)) AS VerifDigit,
CASE
WHEN VerifDigit = 10 THEN 'K'
WHEN VerifDigit = 11 THEN '0'
ELSE VerifDigit
END
FROM
Citizen
答案 0 :(得分:1)
您不能在定义它的select子句中使用别名。这里的一个解决方法是子查询,例如
WITH cte AS (
SELECT
RUT,(11 - ((SUBSTRING(RUT,1,1)*3
+ SUBSTRING(RUT,2,1)*2
+ SUBSTRING(RUT,3,1)*7
+ SUBSTRING(RUT,4,1)*6
+ SUBSTRING(RUT,5,1)*5
+ SUBSTRING(RUT,6,1)*4
+ SUBSTRING(RUT,7,1)*3
+ SUBSTRING(RUT,8,1)*2)%11)) AS VerifDigit
FROM Citizen
)
SELECT
VerifDigit,
CASE
WHEN VerifDigit = 10 THEN 'K'
WHEN VerifDigit = 11 THEN '0'
ELSE VerifDigit
END
FROM cte;
答案 1 :(得分:0)
您也可以使用CROSS APPLY和VALUES来计算结果,然后在CASE语句中引用该值,例如...
SELECT
RUT,
xa.VerifDigit,
CASE
WHEN xa.VerifDigit = '10' THEN 'K'
WHEN xa.VerifDigit = '11' THEN '0' -- this will never occur
ELSE xa.VerifDigit
END
FROM
Citizen
CROSS APPLY (VALUES(cast((11 - (( substring(RUT, 1, 1) * 3
+ SUBSTRING(RUT, 2, 1) * 2
+ SUBSTRING(RUT, 3, 1) * 7
+ SUBSTRING(RUT, 4, 1) * 6
+ SUBSTRING(RUT, 5, 1) * 5
+ SUBSTRING(RUT, 6, 1) * 4
+ SUBSTRING(RUT, 7, 1) * 3
+ SUBSTRING(RUT, 8, 1) * 2) % 11))
AS char(2)))) xa(VerifDigit)
值得指出的一些事情......