Inner join within Common Table Expression in PostgreSQL -
i have table "table1", contain following rows:
cola colb ------------- p4 x1 p4 z p4 z p5 s p5 t p5 y1 p8 x1 p8 y1 p8 x1 p9 x1 p10 z1 p1 z1 p2 z1 p3 z1
now need display table this:(as expected)
cola colb result ---------------------- p4 x1 0 p8 x1 0 p9 x1 0 p5 y1 0 p8 y1 0 p1 z1 0 p10 z1 0 p2 z1 0 p3 z1 0
for writing query:
with cte ( select colb,cola,dense_rank() over(partition colb order cola asc) tem table1 ) select s.cola, s.colb, 0 result tem_table table1 s inner join cte c on c.colb = s.colb tem > 1 group s.cola,s.colb order s.colb;
but dont want use inner join in above query because of low perfromance. trying query:
with cte ( select colb,cola,dense_rank() over(partition colb order cola asc) tem table1 ) select cola,colb,0 result tem_table cte tem > 1 group cola,colb order colb;
this gives output:(which not correct)
cola colb result ---------------------- p8 x1 0 p9 x1 0 p8 y1 0 p10 z1 0 p2 z1 0 p3 z1 0
how can expected output without using inner join in cte because self joining getting more time execute.
select cola, colb, 0 ( select cola, colb, count(*) on (partition colb) cnt ( select distinct cola, colb mytable ) q ) q cnt > 1
Comments
Post a Comment