Using the DISTINCT keyword causes this error: not a SELECTed expression Using the DISTINCT keyword causes this error: not a SELECTed expression sql sql

Using the DISTINCT keyword causes this error: not a SELECTed expression


You are trying to order your result with columns that are not being calculated. This wouldn't be a problem if you didn't have the DISTINCT there, but since your query is basically grouping only by share.rooms column, how can it order that result set with other columns that can have multiple values for the same share.rooms one?


This post is a little old but one thing I did to get around this error is wrap the query and just apply the order by on the outside like so.

SELECT COLFROM (   SELECT DISTINCT COL, ORDER_BY_COL   FROM TABLE   // ADD JOINS, WHERE CLAUSES, ETC.) ORDER BY ORDER_BY_COL;

Hope this helps :)