More SQL fixes for when SMW ID is not unique

Change-Id: I4138c502cd55272791be85eaf82a3107895bd584
Merge "More SQL fixes for when SMW ID is not unique"