我想将一列转换为字符串,其中该列是一个选择语句,然后与另一列连接。这是我在使用CONVERT或CAST时感到困惑的地方。
例子:
SELECT employeeID ,name ,location ,(SELECT COUNT(DISTINCT loginsFailed) FROM users WHERE (users.employeedID = userDetails.employeeID) AND (users.startdate = 01-01-2013) as LoginCountFailed ,(SELECT COUNT(DISTINCT logins) FROM users WHERE (users.employeedID = userDetails.employeeID) AND (users.startdate = 01-01-2013) as LoginCount FROM userDetails
现在,此查询非常完美,因为它提供了正确的登录次数,但失败为整数。但是,我想将这些整数用作字符串,因此我可以使用一列。有一个原因为什么它需要作为字符串的一列。
我只想有4列,而不是5列。我想拥有的登录列是loginCountFailed / LoginCount。例如:3/12。我需要将其作为字符串,因为您无法将其除以0,并且有时分母为0。
要在MSSQL-2005中合并数字,应使用CAST
CAST(loginsFailed AS VARCHAR(10)) + '/' + CAST(LoginCount AS VARCHAR(10))
上面的loginsFailed和loginCount实际上是您的select count distinct片段
select count distinct
我希望这行得通
CAST ((SELECT COUNT(DISTINCT loginsFailed) FROM users WHERE users.employeedID = userDetails.employeeID AND users.startdate = 01-01-2013) AS VARCHAR(10)) + '/' + CAST ((SELECT COUNT(DISTINCT logins) FROM users WHERE users.employeedID = userDetails.employeeID AND users.startdate = 01-01-2013) AS VARCHAR(10))