Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selective agregate function in Postgres

We have a Postgres table that looks like below. How to create a three column one row view that shows summary of the table:

  1. Maximum of all values
  2. Maximum of all values with category 1
  3. Maximum of all values with category 2
category value
1 1
2 2
1 3
2 4

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use conditional aggregation

select 
 max(value) as max_all, 
 max(case when category = 1 then value end) as max_cat1, 
 max(case when category = 2 then value end) as max_cat2
From 
 table1
max_all max_cat1 max_cat2
4 3 4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading