操作数数据类型 varchar(max) 对于 avg 运算符无效
Operand data type varchar(max) is invalid for avg operator
有人可以帮我解决这个错误吗?
SELECT
a.id, a.name, a.Report, b.Agentid,
count(distinct b.pyID),
count(distinct b.SourceID),
AVG(b.ChatDurationMinute)
from table_1 a
left join table_b b on a.id = b.agentid
where
StartChatTime >= ''20200701'' and
LastChatTime <= ''20200831''
GROUP BY a.id, a.name, a.Report, b.AgentID
出现这样的错误:
Operand data type varchar(max) is invalid for avg operator.
我该怎么办?感谢所有帮助过我的人。
ChatDurationMinute 列的数据类型似乎是 varchar - 因此您需要对其进行转换
SELECT
a.id, a.name, a.Report, b.Agentid,
count(distinct b.pyID),
count(distinct b.SourceID),
AVG(cast(b.ChatDurationMinute as float))
from table_1 a
left join table_b b on a.id = b.agentid
where
StartChatTime >= ''20200701'' and
LastChatTime <= ''20200831''
GROUP BY a.id, a.name, a.Report, b.AgentID
根据查询中的命名约定,可以非常合理地假设 StartChatTime
和 LastChatTime
在 b
而不是 a
中。如果是这样,那么 WHERE
子句会将 LEFT JOIN
变成 INNER JOIN
.
此外,在GROUP BY
中包含b.AgentId
是多余的,因为ON
子句指定它应该与a.id
相同。
在字符串中存储数字是一个非常糟糕的设计。但是,如果您遇到非常糟糕的设计,您可能应该使用 try_()
函数之一。
让我假设 ChatDurationMinute
是一个整数。那么:
SELECT a.id, a.name, a.Report
COUNT(DISTINCT b.pyID),
COUNT(DISTINCT b.SourceID),
AVG(TRY_CONVERT(int, b.ChatDurationMinute) * 1.0)
FROM table_1 a LEFT JOIN
table_b b
ON a.id = b.agentid AND
b.StartChatTime >= '20200701'' AND
b.LastChatTime <= '20200831'
GROUP BY a.id, a.name, a.Report;
如果这些日期确实在 a
中,那么您可以将它们保留在 WHERE
子句中。
有人可以帮我解决这个错误吗?
SELECT
a.id, a.name, a.Report, b.Agentid,
count(distinct b.pyID),
count(distinct b.SourceID),
AVG(b.ChatDurationMinute)
from table_1 a
left join table_b b on a.id = b.agentid
where
StartChatTime >= ''20200701'' and
LastChatTime <= ''20200831''
GROUP BY a.id, a.name, a.Report, b.AgentID
出现这样的错误:
Operand data type varchar(max) is invalid for avg operator.
我该怎么办?感谢所有帮助过我的人。
ChatDurationMinute 列的数据类型似乎是 varchar - 因此您需要对其进行转换
SELECT
a.id, a.name, a.Report, b.Agentid,
count(distinct b.pyID),
count(distinct b.SourceID),
AVG(cast(b.ChatDurationMinute as float))
from table_1 a
left join table_b b on a.id = b.agentid
where
StartChatTime >= ''20200701'' and
LastChatTime <= ''20200831''
GROUP BY a.id, a.name, a.Report, b.AgentID
根据查询中的命名约定,可以非常合理地假设 StartChatTime
和 LastChatTime
在 b
而不是 a
中。如果是这样,那么 WHERE
子句会将 LEFT JOIN
变成 INNER JOIN
.
此外,在GROUP BY
中包含b.AgentId
是多余的,因为ON
子句指定它应该与a.id
相同。
在字符串中存储数字是一个非常糟糕的设计。但是,如果您遇到非常糟糕的设计,您可能应该使用 try_()
函数之一。
让我假设 ChatDurationMinute
是一个整数。那么:
SELECT a.id, a.name, a.Report
COUNT(DISTINCT b.pyID),
COUNT(DISTINCT b.SourceID),
AVG(TRY_CONVERT(int, b.ChatDurationMinute) * 1.0)
FROM table_1 a LEFT JOIN
table_b b
ON a.id = b.agentid AND
b.StartChatTime >= '20200701'' AND
b.LastChatTime <= '20200831'
GROUP BY a.id, a.name, a.Report;
如果这些日期确实在 a
中,那么您可以将它们保留在 WHERE
子句中。