EDIT: I am on an older version of Postgresql that does not support crostab.
I have a query I am trying to improve by making it easier to add new value a table that is meant to roll up information for a single user
I basically have to write a new left join and where clause each time I add a value to this column below called algorithms:
┌───────────────────────────────┐
│          algo                 │
├───────────────────────────────┤
│ Algorithm1                    │
│ Algorithm2                    │
│ Algorithm3                    │
└───────────────────────────────┘
Here is the query I wrote to genearte the output:
select a.userid, a.algo, a.algo1_cnt, b.algo, b.algo2t_cnt, c.algo, c.algo3_cnt
    from
    (select userid, algo, count(*) as algo1_cnt
    from test_table
    where (algo = 'Algorithm1')
    group by 1,2 ) as a
    LEFT OUTER JOIN
    ( select userid, algo, count(*) as algo2_cnt
    from test_table
    where (algo = 'Algorithm2')
    group by 1,2 ) as b
    ON
    (a.userid = b.userid)
    LEFT OUTER JOIN
    ( select userid, algo, count(*) as algo3_cnt
    from test_table
    where (algo = 'Algorithm3')
    group by 1,2 ) as c
    ON
    (a.userid = c.userid)
The output of the query now looks like:
┌──────────────────────┬────────────────┬───────────┬───────┬───────────┬───────────────────────────────┬───────────┐
│        userid        │     algo1      │ algo1_cnt │ algo2 │ algo2_cnt │             algo3             │ algo3_cnt │
├──────────────────────┼────────────────┼───────────┼───────┼───────────┼───────────────────────────────┼───────────┤
│ user1                │ algo1          │         3 │       │           │ algo3                         │         2 │
│ user2                │ algo1          │         2 │       │           │                               │           │
Question: What is the best way to modify the query to be able to read the distinct values from the algo column in a dynamic fashion and generate the same outpuy?
What I mean if I add a new value called Algorithm4 to the algo column can I levarage PL/PGSQL or some other dyanmic recurision to generate the same output without having to use WHERE (algo = 'Algorithm4')?
 
    