按列分组并显示计数

时间:2014-05-31 22:17:10

标签: sql postgresql

我有一个复杂的查询,我可以总结为:

select t0.A, t1.B, t0.C, x.count from T0 t0 
join
(
    select distinct max(D) over (partition by A order by D desc) as D,
    count(t2.A) over (partition by t0.A)
        from T0 t0 join T2 t2 on t2.E = t0.D
        where t0.B = k
) x on t0.D = x.D
join T1 t1 on t1.B = t0.B
    where not exists(select A from T2 t2 where t2.A = t0.A)

提供了所需的输出,例如:

 A     B     C    count
===   ===   ===   ===
id1   name1 sadf  3
id2   name2 dgds  2
id3   name3 dfbg  1

但跑得太慢了。运行分析后,我发现罪魁祸首是第一个count内的join

所以我试图从第一个count中取出join,以便它可以在更少的行上运行(如果我理解正确的话)。但我真的不知道如何继续。到目前为止,我写了:

select t0.A, t1.B, t0.C from T0 t0 
join
(
    select distinct max(E) over (partition by t0.A order by E desc) as D,
) x on t0.D = x.D
join T2 t2 on t2.E = t0.D
join T1 t1 on t1.B = t0.B
    where not exists(select A from T2 t2 where t2.A = t0.A)

其中输出如下:

 A     B     C  
===   ===   === 
id1   name1 sadf
id2   name2 dgds
id1   name1 xfga
id1   name1 sadf
id2   name2 nuoh
id3   name3 dfbg

但是如上所述我希望它按列A分组而不管C列中的值(即不仅仅按重复行分组,两个相同的A值可以具有相同或不同的C值),我想要显示也算如上。

group by t0.A放在底部只会出错:

ERROR: column "t1.B" must appear in the GROUP BY clause or be used in an aggregate function

我不知道在哪里放count

请帮忙!

1 个答案:

答案 0 :(得分:1)

这是您目前发布的有效且简化的版本。只是一个有根据的猜测。

SELECT t0.a, max(t1.b) AS max_b, max(t0.c) AS max_c, count(t0.a) AS ct_a
FROM  (
   SELECT DISTINCT max(d) AS max_d
   FROM   t0
   GROUP  BY a
   ) x
JOIN   t0 ON t0.e = x.max_d
JOIN   t2 USING (d)
JOIN   t1 USING (b)
WHERE  NOT EXISTS(SELECT 1 FROM t2 WHERE a = t0.a)
GROUP  BY t0.a;