r/SQL • u/dettus_Xx_ • Aug 01 '26
PostgreSQL SQL Question: Rows into Columns without TABLEFUNC() or PIVOT?
Help me Reddit! I feel especially stupid today....
So, I have this table in my Postgresql Database:
event_id | color_scheme | count----------+--------------+-------1 | red | 61 | green | 31 | blue | 51 | yellow | 33 | red | 54 | red | 35 | red | 15 | blue | 2
And I would like to turn it sideways, so that I can see EASILY how many votes each color scheme for my event has gotten (and later JOIN it with another table... )
event_id | count_red | count_green | count_blue | count_yellow----------+-----------+-------------+------------+--------------1 | 6 | 3 | 5 | 33 | 5 | 0 | 0 | 04 | 3 | 0 | 0 | 05 | 1 | 0 | 2 | 0
The colors "red" "green" "blue" and "yellow" are fixed, and will never ever change.
I have done some googling, I found examples mentioning PIVOT and TABLEFUNC, but I cannot do this on the server because of reasons(tm).
The only way I can think of doing this is with a cascade of OUTER JOIN, but is there maybe a simpler solution?
19
u/PinguinSuit Aug 01 '26
Group by event_id and then
sum(case when color = ‘xyz’ then count else 0 end) count_xyz
For each color
7
u/Imaginary__Bar Aug 01 '26 edited Aug 01 '26
Off the top of my head;
SELECT
event_id
, Max(If color_scheme = 'red' then count else 0) as count_red
, etc
, etc
Group by event_id
Edited to add: COUNT is a reserved keyword in SQL so you might have to use "count" (enclosed in double-quotes) in the SELECT statement.
4
u/Iamcalledchris Aug 01 '26
Postgres’s has a nice FILTER syntax, so probably something like
SELECT
event_id,
COALESCE(SUM(count) FILTER (WHERE color_scheme = 'red'), 0) AS count_red,
COALESCE(SUM(count) FILTER (WHERE color_scheme = 'green'), 0) AS count_green,
COALESCE(SUM(count) FILTER (WHERE color_scheme = 'blue'), 0) AS count_blue,
COALESCE(SUM(count) FILTER (WHERE color_scheme = 'yellow'), 0) AS count_yellow
FROM your_table
GROUP BY event_id
ORDER BY event_id;
1
u/itsintheletterbox Aug 01 '26
select event_Id, count_green = sum(case when colour_scheme ='green' then count else 0 end),...
From table
Group by event_id
1
u/coffeDrinkerDave Aug 01 '26
Select Event_id , sum(case when color_scheme ='red' then count else 0 end) , sum(case when color_scheme ='green' then count else 0 end) , sum(case when color_scheme ='blue' then count else 0 end) , sum(case when color_scheme ='yellow' then count else 0 end) From colors group by event_id
Next wrat it in CTE
;with colors_sideway as (
... ) select ... From ... Join colors_sideway on ...
1
u/j89k Aug 01 '26
You can use case statements with min/max commands.
Im not at my computer, but if you want code I can share.
2
u/National_Cod9546 Aug 01 '26
I do this all the time at work. Usually I'm doing a max though as I swing work order data out from one row per item to one row per account.
1
u/zbignew Aug 02 '26
Telllll us the reasonsssssss.
Not because it changes the answer, but because it’s fun to hear about other people’s terrible environments.
1
u/[deleted] Aug 01 '26
[removed] — view removed comment