lundi 20 avril 2015

Getting another aggregate value from a previously aggregated value

If I have an Sql query like this :

select 
    ..., count(t2.value) as t2_count, count(t3.value) as t3_count
from 
    table t1
left join
    table2 t2
on
    t2.id = t1.id
left join
    table3 t3
on
    t3.id = t1.id
group by
    t1.id
order by
    t1.id;

This will result to a table like this :

|... | ... | t2_count | t3_count
------------------------------------
|... | ... | 3        | 4
|... | ... | 3        | 3 

Right now I'm just using the count aggregate in Postgres, what I want to achieve is another aggregate column using the values of t2_count and t3_count as parameters. Specifically to check If they match like :

|... | ... | t2_count | t3_count | (aggregate, match?)
-----------------------------------------
|... | ... | 3        | 4        | false
|... | ... | 3        | 3        | true

How do I do this in Postgresql ? Thank you!

Aucun commentaire:

Enregistrer un commentaire