I have a table containing some names and their associated ID, along with a snapshot:
snapshot, systemid, name[, some, other, columns]
I need to identify all the unique names that a systemid has had across all snapshots, but only where there has been at least once change.
For example, with the data:
'DR1', 0, 'MOUSE_SPEED'
'DR1', 1, 'MOUSE_POS'
'DV8', 0, 'MOUSE_BUTTONS'
'DV8', 1, 'MOUSE_POS'
'DR6', 0, 'MOUSE_BUTTONS'
'DR6', 1, 'MOUSE_POS'
'PP2', 0, 'MOUSE_SPEED'
'PP2', 1, 'MOUSE_POS'
…I’d like a query that will return (in any order):
0, 'MOUSE_SPEED'
0, 'MOUSE_BUTTONS'
Additionally, it would be useful to have the inverse – a list of systemids that have remained stable across all snapshots (that is, where the name has never changed).
I am using PostgreSQL v8.4.2.
EDIT: Updated to reflect comments (sorry for the original less-than-perfect post, I am new here!).
Following is with SQL Server but it does not use any SQL Server specific constructs. It should be portable to postgresql.
SQL Statement
Test data