保留不同的键和连接列
keep distinct key and concat column
我有一个table翻译例如:
| key | language | value
----------------------------------------------
| hi | en | hello
| hi | es | hola
| delete | en | value
| delete | es | suprimir
| city | en |
| city | es | ciudad
目前有 2 种语言,但我稍后会添加更多。
我正在尝试查找 sql 查询哪个组 "key" AND concat "language" 不为空值。
我正在寻找这个输出:
| key | languages | translated
---------------------------------------------------
| hi | en,es | 100%
| delete | en,es | 100%
| city | es | 50%
有什么想法吗?
Group_concat 值并返回自身计算字段:
SELECT key,languages,translated_values/COUNT(key) translated
FROM table b
INNER JOIN
(SELECT key, GROUP_CONCAT( languages SEPARATOR ',') languages,
COUNT(*) as translated_values
FROM table
WHERE translated!=""
GROUP BY key) a ON a.key=b.key
GROUP BY key,translated_values,b.languages;
您可以在单个查询中这样做
select `key`,
group_concat(
case when `value` is not null
then `language`
else null end) languages,
concat(
(sum(`value` is not null )/ count(distinct `language`) ) * 100,
'%') translated
from table1
group by `key`
我有一个table翻译例如:
| key | language | value
----------------------------------------------
| hi | en | hello
| hi | es | hola
| delete | en | value
| delete | es | suprimir
| city | en |
| city | es | ciudad
目前有 2 种语言,但我稍后会添加更多。 我正在尝试查找 sql 查询哪个组 "key" AND concat "language" 不为空值。
我正在寻找这个输出:
| key | languages | translated
---------------------------------------------------
| hi | en,es | 100%
| delete | en,es | 100%
| city | es | 50%
有什么想法吗?
Group_concat 值并返回自身计算字段:
SELECT key,languages,translated_values/COUNT(key) translated
FROM table b
INNER JOIN
(SELECT key, GROUP_CONCAT( languages SEPARATOR ',') languages,
COUNT(*) as translated_values
FROM table
WHERE translated!=""
GROUP BY key) a ON a.key=b.key
GROUP BY key,translated_values,b.languages;
您可以在单个查询中这样做
select `key`,
group_concat(
case when `value` is not null
then `language`
else null end) languages,
concat(
(sum(`value` is not null )/ count(distinct `language`) ) * 100,
'%') translated
from table1
group by `key`