Search Postgresql Archives

Removing duplicates from multiple self left joins

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am dynamically generating a query like below that creates different combinations of rules by left joining (any number of times) on itself and avoiding rules with some of the same attributes as part of the joins conditions e.g. 
 
    SELECT count(*) 
    FROM rules AS t1 
    LEFT JOIN rules AS t2
     ON t1.id != t2.id
     AND ...
    LEFT JOIN rules AS t3
     ON t1.id != t2.id AND t1.id != t3.id AND t2.id != t3.id
     AND ...

I am currently removing duplicates by creating an array of ids from the joined rows then sorting and grouping by them:

    SELECT sort(array[t1.id, t2.id, t3.id]) 
    ...
    GROUP BY ids

I would like to know if there is a better way of removing duplicate rows e.g.

    t1.ID | t2.ID | t3.ID
    ---------------------
      A   |   B   |   C
      C   |   B   |   A

Should be

    t1.ID | t2.ID | t3.ID
    ---------------------
      A   |   B   |   C

Or

    t1.ID | t2.ID | t3.ID
    ---------------------
      C   |   B   |   A

But not both.

I would like to go from a permutation of rows to a combination rows.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux