I have a large table of data in a PostgreSQL database from a research study where a number of test subjects performed a number of tasks, and several sensors gathered data while they performed these tasks. I need to create a table with only one row for each subject, and columns corresponding to the sensor measurements for each task. A simplified example of what I am trying to achieve:
A couple of key issues:
- I have already aggregated the data I need so there is only one row for each subject/task combination.
- Not all subjects are guaranteed to have completed every task. If a subject has not completed a task, there is no corresponding subject/task row.
- The output needs to have a column corresponding to every possible combination of sensor and task.
- The actual table I am working with has 37 columns of sensor data, 24 tasks, and each task was performed twice in a pre and post session. Thus the number of columns I need is
37x24x2=1776
columns of data!
I could create a new view for every task and then do an outer join on the subject column, but that would still require manually creating a separate view for all 24 tasks and specifying names for all 1776 columns.
How do I build a query which automatically creates the necessary columns?