Search code examples
sqlpostgresqlbooleanquery

how to combine results of query of multiple rows into one row


I have this results from this query.

select distinct "CVE_ID","V19_200","V20","V20_100","V21","V21_100","V21_200" 
from cve_table 
where "CVE_ID"='CVE-2022-22965';
CVE_ID V19_200 V20 V20_100 V21 V21_100 V21_200
CVE-2022-22965 f f f f f f
CVE-2022-22965 f f f f t f
CVE-2022-22965 f f f t f f
CVE-2022-22965 f f t f f f
CVE-2022-22965 f t f f f f
CVE-2022-22965 t f f f f f

What I want is this.

CVE_ID V19_200 V20 V20_100 V21 V21_100 V21_200
CVE-2022-22965 t t t t t f

I have tried to UNION and UNION ALL but it didn't work for me or I couldn't do it the right way.


Solution

  • Use BOOL_OR and GROUP BY as follows:

    SELECT "CVE_ID", 
           BOOL_OR("V19_200") as "V19_200", 
           BOOL_OR("V20") as "V20", 
           BOOL_OR("V20_100") as "V20_100", 
           BOOL_OR("V21") as "V21", 
           BOOL_OR("V21_100") as "V21_100", 
           BOOL_OR("V21_200") as "V21_200" 
    FROM cve_table 
    WHERE "CVE_ID" = 'CVE-2022-22965' 
    GROUP BY "CVE_ID";