[英]select count distinct column in table new filed
我有一个查询结果,并且有3个不同的p_id(3160、3301、41)
我想选择count(distinct p_id)并如图p_id_count_true所示(参见图片),p_id_count不正确,我如何编写子查询并获取p_id_count_true
您似乎想要:
select . . .,
count(distinct pid) over () as num_pids
from t
遗憾的是,Postgres不支持窗口功能的distinct
。 一种解决方法是:
select . . .,
sum( (seqnum_pid = 1)::int ) over () as num_pids
from (select t.*, row_number() over (partition by pid order by pid) as seqnum_pid
from t
) t
有些人更喜欢dense_rank()
方法:
select . . .,
max( seqnum_pid ) over () as num_pids
from (select t.*, dense_rank() over (order by pid) as seqnum_pid
from t
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.