Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a <code>having</code> clause to get the list of entries that are duplicated:</p> <pre><code>SELECT abc.entry FROM table abc JOIN table1 def ON abc.entry = def.entry WHERE def.created &gt;= 'some_date' AND def.position = 1 group by abc.entry having count(*) &gt; 1 </code></pre> <p>To get the list of rows, I would use window functions:</p> <pre><code>select t.* from (SELECT abc.*, count(*) over (partition by entry) as cnt FROM table abc JOIN table1 def ON abc.entry = def.entry WHERE def.created &gt;= 'some_date' AND def.position = 1 ) t where cnt &gt; 1; </code></pre> <p>EDIT:</p> <p>I see the confusion over the terminology. <code>DISTINCT</code> has a very particular meaning in SQL. And, <code>DISTINCT ON</code> has a rather different meaning in Postgres. I think you have to do this with a subquery:</p> <pre><code>select abc.* from table abc left outer join (SELECT DISTINCT ON (abc.entry) abc.* FROM table abc JOIN table1 def ON abc.entry = def.entry WHERE def.created &gt;= 'some_date' AND def.position = 1 ) f on abc.id = f.id where f.id is NULL </code></pre> <p>A word of caution, though. You are using <code>distinct on</code> without an <code>order by</code> clause. The particular row being returned in the data is indeterminate. You should add an <code>order by</code> to your original data and to the subquery here.</p> <pre><code>select abc.* from table abc left outer join (SELECT abc.entry, max(abc.id) as maxid FROM table abc JOIN table1 def ON abc.entry = def.entry WHERE def.created &gt;= 'some_date' AND def.position = 1 group by abc.entry ) f on abc.id = f.maxid where f.maxid is NULL </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload