Postgres 13 on Windows. Let's assume I have a table like this:
Job_type Department User_id Op_code Times_executed ------------------------------------------------------------------------------------- ACCOUNTING MANAGER Accounting Alice VA01 578 ACCOUNTING MANAGER Accounting Alice FBL1N 2256 ACCOUNTING MANAGER Accounting Alice FBL3N 16272 ACCOUNTING MANAGER Accounting Alice ME23N 1682 ACCOUNT ASSISTANT General Bob ME2L 876 ACCOUNT ASSISTANT General Bob VA04 25700 ACCOUNT ASSISTANT General Bob VF77 12393 ACCOUNT ASSISTANT General Bob MEXX 5182 I'd like to use the crosstab function in Postgres to get a pivot table like this:
Job_type Department User_id Op1 times1 Op2 times2 Op3 times3 Op4 times4 ACCOUNTING MANAGER Accounting Alice VA01 578 FBL1N 2256 FBL3N 16272 ME23N 1682 ACCOUNT ASSISTANT General Bob ME2L 876 VA04 25700 VF77 12393 MEXX 5182 I tried with my basic knowledge of crosstab but I only managed to get the operation codes, but not the times:
SELECT * FROM crosstab( 'select user_id, job_type, tcode from mytable order by 1,2') AS ct(row_name text, category_1 text, category_2 text, category_3 text, category_4 text); Is it possible to get it all in the format above?
Thanks a lot in advance.