Speedwheelftw
Speedwheelftw

Reputation: 393

Select Count Distinct values from two tables

Users Table:

+-----------+----------+------------+-------------+
| name      | fb_id    | date       | flipbook    |
+-----------+----------+------------+-------------+
| Tia       | 66783722 | 1975-09-18 |  june 2014  |
| Nikki     | 10438259 | 1972-03-04 |  july 2014  |
| Yamila    | 73370629 | 1972-03-04 | august 2014 | 
+-----------+----------+------------+-------------+

Visits Table:

+-----------+----------+------------+-------------+
| Name      | fb_id    | Date       |  Flipbook   |
+-----------+----------+------------+-------------+
| Tia       | 66783722 | 1975-09-18 | june 2014   |
| Nikki     | 10438259 | 1972-03-04 | august 2014 |
| Nikki     | 10438259 | 1972-04-04 | october 2014|
+-----------+----------+------------+-------------+

I want the query to return all users from from user Table and count the number of flipbooks, for example:

[1]
name => Tia, 
fb_id = 66783722,
date => 1975-09-18,
count_flip => 1 (june 2014 is in both tables so we count it as 1)

[2]
name => Nikki, 
fb_id = 10438259,
date => 1972-03-04,
count_flip => 3 (because in the first table we have june 2014 and in the second table we have august 2014 and october 2014, so no duplicates)

[3]
name => Yamila, 
fb_id = 73370629,
date => 1972-03-04,
count_flip => 1 (because we have august 2014 in the first table and she is not mentioned in the second one)

I tryed to do this query:

SELECT u.*, (SELECT COUNT(Distinct flipbook) FROM visits v WHERE v.fb_id = u.fb_id) as count_flip FROM users u

But the problem is I am missing the users that are in the users table but not in the visits table. So for the example above I wouldn't see "Yamila" in my query.

Any thoughts how to fix this?

Upvotes: 1

Views: 499

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269633

Another way of thinking about the logic is that you want to count 1 for the users table and then add up all non-matching flipbook values for the name in the visits table. This suggests a correlated subquery:

select u.*,
       (select 1 + count(*)
        from visits v
        where v.name = u.name and v.flipbook <> u.flipbook
       ) as nr
from users u;

EDIT:

Another way to do what you want is less efficient (assuming you have the right indexes for the above query):

select u.*, uv.nr
from users u join
     (select u.name, count(distinct flipbook) as nr
      from ((select name, flipbook from users) union all
            (select name, flipbook from visits)
           ) uv
      group by u.name
     ) uv
     on u.name = uv.name;

Upvotes: 1

Kickstart
Kickstart

Reputation: 21513

I think you can just do this:-

SELECT users.name, users.fb_id, users.date, COUNT(DISTINCT visits.Flipbook) + 1 AS count_flip
FROM users
LEFT OUTER JOIN visits
ON users.fb_id = visits.fb_id
AND users.flipbook != visits.flipbook
GROUP BY users.name, users.fb_id, users.date

Does a LEFT OUTER JOIN to get a row for each matching user with a different date. Then just ad one to the resulting count (to count the original field on the users table).

This avoids any sub queries.

Upvotes: 1

Related Questions