I have two tables whose simplified structure looks like this:
RESPONSES
id
created
ACCESSORY VALUES
id
response_id
sensor_id
value
I want to create a view that flattens all accessory values for a given response into one row over a time period (filtering on response.created). I think I want a Pivot table or Crosstab, but I'm unfamiliar with both and the examples I've found mainly deal with a known number of columns. To complicate things, a given sensor could only appear for part of the time period if a user started or stopped tracking it during the time in question. Ideally I'd hold a NULL in that column for the sensor in any rows when it was not present. Is this possible? If so, am I on the right track or looking in the wrong place?
The SQL to get the data as individual rows looks like
SELECT r.id, a.sensor_id, a.value from results_response r
INNER JOIN results_accessoryvalue a ON r.id = a.response_id
WHERE r.installation_id = 40
AND r.created BETWEEN '2013-04-01' AND '2013-05-01'
ORDER BY r.created
but I'm not having any luck trying to use it in a crosstab because I don't know how to specify dynamic columns.