我有一张桌子:
我的选择
select regexp_split_to_table(t3."Id"::character varying,'') as s
from (select t1."Id" from table1 t1
union all
select t2."Id"from table2 t2) t3
order by s
或者我也可以通过以下方式获得字符串“ 22173345566179111111134546175622323811”:
select string_agg(t3."Id"::character varying,'') as s
from (select t1."Id" from table1 t1
union all
select t2."Id"from table2 t2) t3
我需要获得一个包含 number | count 数据的表,我的意思是任何数字都可以在选择中获得重复计数,例如:
1 | 9
2 | 5
3 | 5
以此类推。
PostgreSQL DBMS
答案 0 :(得分:0)
这是您想要的吗?
select id, count(*)
from (select t1."Id" from table1 t1
union all
select t2."Id" from table2 t2
) t3
group by id
order by id;
答案 1 :(得分:0)
如果我理解正确,那么您想要一个包含所有数字的列表,该列表存在于两个表的一组ID中,并且每个数字的计数(在所有这些ID中出现的频率)。如果是这样,您只需要GROUP BY
位数字并使用count()
。
SELECT s.d,
count(*) count
FROM (SELECT t1."Id"
FROM table1 t1
UNION ALL
SELECT t2."Id"
FROM table2 t2) t3
CROSS JOIN LATERAL regexp_split_to_table(t3."Id"::character varying, '') s(d)
GROUP BY s.d
ORDER BY s.d;
答案 2 :(得分:0)
最简单的方法
select regexp_split_to_table(t3."Id"::character varying,'') s, count(*) count
from (select t1."Id" from table1 t1 union all select t2."Id"from table2 t2) t3
group by s