Though there are a number of questions on pivoting tables, none of them seemed to address what I need to do or seem too complex and I could not get to work.
I have an app that collects data from remote telematics devices over wireless networks (e.g. GSM modems running various embedded data collection apps). My database associates each set of data with a "Modem ID". There are numerous parameters that are collected (about 100 different types, thus it would not seem practical to create a table with 100 columns for each modem, particularly as different modem apps may only use 10 of the possible 100 parameters). Thus I have a very simple 'parameter' table with fields 'modem_id', 'parm_id', and 'value' -- i.e. a classic key=value table.
I would like to write a simple SQL query that will allow me to search for specific modems, sort on a particular parm_id, and paginate results using limit and offset.
An example of some rows in Table parameter
:
modem_id | parm_id | value
100 1 '11.1'
100 4 '22.2'
100 9 '3.33'
102 1 '12.3'
102 4 '23.4'
102 9 '3.45'
I would like to group the above by modem_id into a temp table with the parm_ids in separate columns, e.g.:
modem_id | parm_id1 | parm_id4 | parm_id9
100 '11.1' '22.2' '3.33'
102 '12.3' '23.4' '3.45'
This seems like it ought to be simple, and like it would be a very common thing, but the only solutions I've seen involve dozens of lines of code, prepared statements, and strange escape sequences with lots of quote characters everywhere that leave me unable to even understand what the query is supposed to do and which I have been unable to get to work for this example.