Search code examples
sqlmariadbmariasql

SQL join on 4 tables not returning correct count


I have 4 tables.

Sequences

id -> primary key
name 
user_id -> foreign key   (users id)

people:

id-> primary key
name-> varchar
seq_id -> foreign key from sequences
user_id -> foreign key from users

links_clicked:

id-> primary key
src_id -> foreign key (sequences id)
views -> Boolean

people_sequence

sequence_id-> foreign key   (sequences id)
people_id ->  foreign key   (people id)

I need to get the sequences details , count of link clicks from the link table , count of people if the seq_id is in there and count of sequences from the sequence from the people_sequence table. I think I need to use the left joins and I wrote the query as below:

SELECT  sequences.*
       ,sum(link_clicked.view) as click_rate 
       ,count(people.seq_id) as prospect 
       ,count(people_sequences.sequence_id) as seqret 
FROM sequences 
LEFT JOIN link_clicked ON sequences.id=link_clicked.src_id 
LEFT JOIN people ON sequences.id=people.seq_id
LEFT JOIN people_sequences ON sequences.id=people_sequences.sequence_id
where sequences.user_id = 1
group by sequences.id

but this is returning the counts for each of the count column as same

Unable to figure out what is wrong with the SQL

id            | name |  user_id |click_rate |   prospect|seqret
5ac77be52d06e | seq1 |1         | 414       |    1890   | 1890
5ae790b48fea9 | seq2 |1         | 8         |    43     | 0
5ae790b6e0b12 | seq3 |1         | 2         |    0      | 0
5ae835c5153b5 | seq4 |1         | NULL      |    0      | 0

this is the result I am getting , but if I do the SQL the sum of views are different.and different for the counts also

SELECT `src_id` ,sum(`view`) FROM `link_clicked ` group by src_id

src_id         | sum(`view`)
5ac77be52d06e  | 23
5ae790b48fea9  | 8
5ae790b6e0b12  |2

Solution

  • I think those left joins will send your results crazy ... you will get multiple repeat entries for everything as all the permutations of the joins are generated as results leading to nonsense sums/counts. To see all the duplicate results, remove the aggregation and you'll get an extremely long repetitive and misleading list. Try maybe 3 subqueries instead?

    SELECT S.*
           ,(SELECT COUNT(IIF([view], 1, 0)) FROM link_clicked WHERE src_id=S.id) click_rate
           ,(SELECT COUNT(*) FROM people WHERE seq_id=S.id) prospect
           ,(SELECT COUNT(*) FROM people_sequences WHERE sequence_id=S.id) seqret
    FROM sequences S
    WHERE S.user_id=1;