我有一个如下所示的查询:
select id, extension, count(distinct(id)) from publicids group by id,extension;
结果如下:
id | extension | count
-------------+-------------------------+-------
18459154909 | 12333 | 1
18459154909 | 9891114 | 1
18459154919 | 43244 | 1
18459154919 | 8776232 | 1
18766145025 | 12311 | 1
18766145025 | 1122111 | 1
18766145201 | 12422 | 1
18766145201 | 14141 | 1
但我真正想要的是结果看起来像这样:
id | extension | count
-------------+-------------------------+-------
18459154909 | 12333 | 2
18459154909 | 9891114 | 2
18459154919 | 43244 | 2
18459154919 | 8776232 | 2
18766145025 | 12311 | 2
18766145025 | 1122111 | 2
18766145201 | 12422 | 2
18766145201 | 14141 | 2
我正在尝试让count字段显示具有相同ID的记录总数。
任何建议都将不胜感激
答案 0 :(得分:0)
我认为你想要计算区分扩展,而不是ID。
运行此查询:
select id
, extension
(select count(*) from publicids p1 where p.id = p1.id ) distinct_id_count
from publicids p
group by id,extension;
答案 1 :(得分:0)
这与牧师的答案大致相同。根据优化器的作用,使用更高记录数的源表可能会更快。
select p.id, p.extension, p2.id_count
from publicids p
inner join (
select id, count(*) as id_count
from publicids group by id
) as p2 on p.id = p2.id