我有一个 INT、varchar 的数据集。整数可以分为 1 到 5 的小集合,我想查看所有相应文本的计数。例如。输入表(格式为 apols)
1,a
2,a
2,b
3,a
3,q
3,z
4,a
我可以按 Int 进行分组,但不知道如何跨行连接。在上面我想知道:
a,2
ab,1
aqz,1
我可以纯粹用 SQL 来完成此操作,最好是采用与数据库无关的通用方式吗?
GROUP_CONCAT
将每个整数值的字符连接成字符串,然后 COUNT
每个字符串的出现次数:
SELECT str, COUNT(*) AS count
FROM (SELECT GROUP_CONCAT(str ORDER BY str SEPARATOR '') AS str
FROM data
GROUP BY i) d
GROUP BY str
ORDER BY str
LISTAGG
:
SELECT str, COUNT(*) AS count
FROM (SELECT LISTAGG(str, '') WITHIN GROUP (ORDER BY str) AS str
FROM data
GROUP BY i) d
GROUP BY str
ORDER BY str
在 SQL Server 2017 及更高版本中,您可以使用
STRING_AGG
:
SELECT str, COUNT(*) AS count
FROM (SELECT STRING_AGG(str, '') WITHIN GROUP (ORDER BY str) AS str
FROM data
GROUP BY i) d
GROUP BY str
ORDER BY str
STRING_AGG
:
SELECT str, COUNT(*) AS count
FROM (SELECT STRING_AGG(str, '' ORDER BY str) AS str
FROM data
GROUP BY i) d
GROUP BY str
ORDER BY str
在所有情况下,输出都是:
STR COUNT
a 2
ab 1
aqz 1
LIST()
功能。 所以:
select chrs, count(*) as cnt
from (select t.number, list(t.chr, '' order by t.chr) as chrs
from t
group by t.number
) n
group by chrs
order by count(*) desc;
您可以通过在 SQL Server 中使用
string_agg()
来完成。在这里查看演示sqlfiddle
select
cnct_str,
count(*) as count
from
(
select
string_agg(chars, '') within group (order by chars) as cnct_str
from tabs
group by id
) val
group by cnct_str