Distinct rows by order I have the following SQL, generated from ActiveRecord in Rails 4 (partly with the Geocoder gem)

problem is, that even though DISTINCT is used, hopefully to get only unique items, the items returned duplicated. The reason for the duplication is that they can occur in several shops or brands (which is why I added the distinct, which doesn't quite help)      

SELECT DISTINCT items.*,
                &lt;calculate distance from shops coordinates&gt; AS distance,
                &lt;calculate bearing&gt; AS bearing,
                items.*,
                shops.id AS closest_shop_id
FROM "items"
INNER JOIN "collection_items" ON "collection_items"."item_id" = "items"."id"
INNER JOIN "galleries" ON "galleries"."id" = "collection_items"."gallery_id"
LEFT OUTER JOIN brands ON brands.id = galleries.galleriable_id
AND galleries.galleriable_type = 'Brand'
INNER JOIN shops ON shops.brand_id = brands.id
LEFT OUTER JOIN shops AS othershops ON galleries.galleriable_id = othershops.id
AND galleries.galleriable_type = 'Shops'
WHERE &lt;shops coordinate are within some bounds&gt;
ORDER BY updated_at DESC,
         distance ASC LIMIT 10


*things in &lt; &gt; are calculation simplification to make query less messy

How can I make this query return distinct items?

Update:

Checking with PGAdmin, I notice the results are distinct, but on everything, see data 



So, to make these distincts, I need to decide which row to ignore. 

Is there a way to take only the first occurance (by the order specific) of the id column?

such that it will take the first row of id 23171?
With PostgreSQL you have to use the following if the rows are not identical considering all columns:

SELECT DISTINCT ON (column1, column2) * FROM ...