I think the reason is because of the associatve array being returned, so one of the fields is used as the key for the array(can't quite determine how it works out which one to use - probably the first in the select statement?).
So the solution ought to be to return an id as first field in the select list.
Unfortunately I can't do this because it's a group by query (grouping on an id number wouldn't quite have the result I want!!), and it's being grouped on 2 fields. So my SQL is along the lines of:
SELECT fieldX, fieldY, COUNT(fieldBlah) FROM mdl_table
GROUP BY fieldX, fieldY
So when my results are returned if there are (legitimately) 2 or more rows with the same value in fieldX, only one of these is returned by get_records_sql().
I see there is the get_recordset_sql() function, but in the comments above the function is says it's 'internal to datalib and should NEVER be called directly' - hence why I'm trying to avoid using that.
I pretty sure someone else must have come across this somewhere else in Moodle, so would be grateful if you'd let me know how you got around it!! Or if anyone has any suggestions as to what I should do
cheers, TIA,
Alex