Længst til venstre ORDER BY
elementer kan ikke være uenige med elementerne i DISTINCT
klausul. Jeg citerer manualen om DISTINCT
kode>
:
Prøv:
SELECT *
FROM (
SELECT DISTINCT ON (c.cluster_id, feed_id)
c.cluster_id, num_docs, feed_id, url_time
FROM url_info u
JOIN cluster_info c ON (c.cluster_id = u.cluster_id)
WHERE feed_id IN (SELECT pot_seeder FROM potentials)
AND num_docs > 5
AND url_time > '2012-04-16'
ORDER BY c.cluster_id, feed_id, num_docs, url_time
-- first columns match DISTINCT
-- the rest to pick certain values for dupes
-- or did you want to pick random values for dupes?
) x
ORDER BY num_docs DESC;
Eller brug GROUP BY
:
SELECT c.cluster_id
, num_docs
, feed_id
, url_time
FROM url_info u
JOIN cluster_info c ON (c.cluster_id = u.cluster_id)
WHERE feed_id IN (SELECT pot_seeder FROM potentials)
AND num_docs > 5
AND url_time > '2012-04-16'
GROUP BY c.cluster_id, feed_id
ORDER BY num_docs DESC;
Hvis c.cluster_id, feed_id
er de primære nøglekolonner for alle (begge i dette tilfælde) tabeller, som du inkluderer kolonner fra i SELECT
liste, så virker dette bare med PostgreSQL 9.1 eller senere.
Ellers skal du GROUP BY
resten af kolonnerne eller samle eller give flere oplysninger.