0

I have 3 tables
Table1 has id cell
Table2 has id cell
Table3 has id1 cell -> corresponds to Table1.id
Table3 has id2 cell -> corresponds to Table2.id

I need a result that count how many times the pair of id1 and id2 appeared in Table3, also for a result that did not appear.

So cant use simple Select * from table3 group by id1, id2.

I used

SELECT *
FROM table1, table2, 
    (SELECT COUNT(*) AS count
    FROM table3
    GROUP BY id1, id2)
GROUP BY table1.id, table2.id   

but it is slow as molasses and it does not work.

2 Answers 2

1

Try next query:

1) First, we cross join table1 and table2 for get all combination tuples.

2) Second, we left join to table3.

3) We count the number of rows obtained for each tuple, counting not null values on table3->id1 (This assume that tuples of types <null, id> or <id, null> don't exists on table3)

SELECT
   t1.id, t2.id, COUNT(t3.id1) AS count
FROM
   table1 AS t1
CROSS JOIN
   table2 AS t2
LEFT JOIN
   table3 AS t3 ON t3.id1 = t1.id AND t3.id2 = t2.id
GROUP BY
   t1.id, t2.id
Sign up to request clarification or add additional context in comments.

3 Comments

Showing rows 0 - 24 (34188 total, Query took 13.3809 seconds.) So not really a fast query, though faster than mine :D
I think you need to add an index by (id1, id2) on table3. How much rows table3, table2 and table1 have?
I added index, and it seems much faster. t1 - 20rows t2 - 3000rows t3 - 20k rows Will test more
1

I'd cross-join table1 and table2, and then left join this on table3, and count an expression to check if combination exists:

SELECT     t1.id,
           t2.id,
           COUNT(CASE WHEN t3.id1 IS NOT NULL AND t3.id2 IS NOT NULL THEN 1 END)
FROM       t1
CROSS JOIN t2
LEFT JOIN  t3 ON t1.id = t3.id1 AND t2.id = t3.id2
GROUP BY   t1.id, t2.id

1 Comment

This is missing GROUP BY t1.id, t2.id but then its slow as anything i tried (14s), see @d-smania answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.