0

I have a query that generated matched data like this. For each parent I need to select a child but not repeat the same combination or parent or child. In the picture below, the black border shows the groups and the blue highlighted rows are the rows I want returned.

Data Table

I also have the case where there are 6 parents and only 3 children. In this case I only want 3 rows max, the child and parent ids can't repeat. I just want the first matched children to parents.

2
  • What is your expect result?
    – D-Shih
    May 31, 2018 at 12:44
  • The rows highlighted in blue is the rows I wanted returned
    – Tim OBrien
    Jun 2, 2018 at 12:25

2 Answers 2

0

So I conducting some research and found options that were close but didn't do the trick. I finally got exactly what I wanted.

    WITH firstmatched AS (
    SELECT parent, 
           child, 
           ROW_NUMBER() OVER(PARTITION BY child 
                                 ORDER BY parent DESC) AS rowkey1,
           ROW_NUMBER() OVER(PARTITION BY parent 
                                 ORDER BY child DESC) AS rowkey2    
      FROM mytable)
    SELECT *
    FROM firstmatched where rowkey1 = rowkey2

Take the case where there are 6 parents and only children, this query without the where clause (where rowkey1 = rowkey2) looks like this.

Matched Data

Then with the where clause added it reduces to this.

Reduced Final Data

Hopefully this helps someone with a similar issue.

0

Rock&Roll-method: just make all assignments and skip the errors:


\i tmp.sql

 -- The data [in non-graphical form]
CREATE TABLE tableau(
        parent integer NOT NULL
        , child integer NOT NULL
        , PRIMARY KEY (parent,child)
        ) ;

INSERT INTO tableau(parent,child) VALUES
( 450759,450768) , ( 450759,450771) , ( 450759,450773)
, ( 450763,450768) , ( 450763,450771) , ( 450763,450773)
, ( 450765,450768) , ( 450765,450771) , ( 450765,450773)
        ;

-- Receptor table for the results:
CREATE TEMP TABLE assignment(
        parent integer NOT NULL UNIQUE
        , child integer NOT NULL UNIQUE
        );

-- Just do it!
INSERT INTO assignment(parent,child)
SELECT parent,child
FROM tableau
ON CONFLICT DO NOTHING --<< MAGIC!
  ;

SELECT * FROM tableau;
SELECT * FROM assignment;

Results:


CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 9
CREATE TABLE
INSERT 0 3
 parent | child  
--------+--------
 450759 | 450768
 450759 | 450771
 450759 | 450773
 450763 | 450768
 450763 | 450771
 450763 | 450773
 450765 | 450768
 450765 | 450771
 450765 | 450773
(9 rows)

 parent | child  
--------+--------
 450759 | 450768
 450763 | 450771
 450765 | 450773
(3 rows)

Note: this solution is greedy; it can fail to find an optimal solution on some types of tableau data.

5
  • That is very cool, didn’t know that was an option, And I have a use for that on something else I’m working on.
    – Tim OBrien
    Jun 2, 2018 at 13:09
  • It is not perfect.(but neither is your solution) Jun 2, 2018 at 13:10
  • I’m not saying anything negative about your solution. I was looking for a solution that selected the data correctly. However I don’t know which executes faster. I have to conduct some testing, my query might be too slow on 1 million rows. I will let u know how the select performs, I might have to switch to your insert solution to temp table.
    – Tim OBrien
    Jun 2, 2018 at 13:36
  • I am not saying anything negative. I just added a remark that both solutions could fail to reach an optimal solution on some types of input. (your example data is rather good-shaped) Jun 2, 2018 at 14:07
  • Ok, sorry, misread that, your right. The data in prod will be millions of rows so I’m not sure how fast that sql statement will be. Your insert solution I know will be fast into a temp table. I’m curious to find out.
    – Tim OBrien
    Jun 2, 2018 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.