如何用整数列做 group_concat?
Posted
技术标签:
【中文标题】如何用整数列做 group_concat?【英文标题】:How to do group_concat with Integer columns? 【发布时间】:2021-07-05 04:28:23 【问题描述】:此查询显示过去 2 周内连续几天的 3 个端点组合(使用某些优先级决定)的性能。
select
date,
GROUP_CONCAT(endpoint, ', ') as endpoints, -- This works fine, since endpoint is string
GROUP_CONCAT(success, ', ') as endpoints_successes, -- This fails
(sum(success) / sum(attempts) * 100) as percentage
from
some_table
where
and datadate = from_timestamp(date_sub(now(), 14), 'yyyyMMdd')
and endpoint in (
select distinct endpoint
from some_table
order by some_priority desc
limit 2
)
group by date
order by date
为了在仪表板中显示,我需要使用 group_concat
之类的东西来组合数据集的值。这似乎适用于 String
类型列。
但是,当我想组合 success
字段的值进行显示时,它会失败并出现以下错误:
No matching function with signature: group_concat(BIGINT, STRING)
我应该如何将 Int 转换为 String 以在 group_concat
中使用,或者是否有其他方法可以实现相同的效果?
【问题讨论】:
【参考方案1】:先尝试将这些数值转换为 STRING
,然后再使用 GROUP_CONCAT
进行汇总:
SELECT
date,
GROUP_CONCAT(endpoint, ', ') AS endpoints,
GROUP_CONCAT(CAST(success AS STRING), ', ') AS endpoints_successes, -- cast here
SUM(success) / SUM(attempts) * 100 AS percentage
FROM some_table
WHERE
datadate = FROM_TIMESTAMP(DATE_SUB(NOW(), 14), 'yyyyMMdd') AND
endpoint IN (SELECT endpoint FROM some_table ORDER BY some_priority DESC LIMIT 2)
GROUP BY date
ORDER BY date;
【讨论】:
以上是关于如何用整数列做 group_concat?的主要内容,如果未能解决你的问题,请参考以下文章