SQL: fixed bug on FLATTEN