0

I would like to transpose table a to table b without knowing exactly how many procedures there are. Is there a way to include a loop inside a query? Thank you in advance!

Image of tables

So far I am just checking what the maximum amount of 'procedures' are, I put all the procedures in an array, and then query all elements from this array. However I would like a query that always works without first defining the maximum amount of procedures.

Zegher V
  • 117
  • 7
  • Don't do it in SQL, do it the programming language of your application. As an alternative, aggregate all procedures into a single JSON value –  Oct 10 '19 at 07:30
  • For future questions: sample data is better presented as [formatted text](https://meta.stackoverflow.com/a/251362). See [here](https://meta.stackexchange.com/questions/81852) for some tips on how to create nice looking tables. –  Oct 10 '19 at 07:45
  • @Zegher, You must take a look at - https://stackoverflow.com/questions/28087948/dynamic-pivot-query-using-postgresql-9-3 and https://postgresql.verite.pro/blog/2018/06/19/crosstab-pivot.html – Ankit Bajpai Oct 10 '19 at 07:46
  • @Ankit Bajpai, Thanks, it seems that this thread will solve my problem. However, when I run 'create extension tablefunc', I get the error message ' ERROR: could not open extension control file "/usr/pgsql-9.5/share/extension/tablefunc.control": No such file or directory'. I cannot seem to find how to download the extensions though. Any advice? My OS is windows 10. – Zegher V Oct 17 '19 at 07:23

0 Answers0