2

I have two tables that I need to get a count of entries per person summed between the two tables.

I need to join the following queries together in one grouped by the name.

select count(entryid) as table1count, reqname1 from table1

And the second query:

select count(comid) as table2count, reqname2 from table2

How would I put these together so that I ended up with output like this:

jsmith 236
jsnow 13
etc.

Sometimes the same reqname will be in both tables, but it could only be in one or the other. So I want to show all reqnames between the two tables and their counts summed between the two tables.

Any ideas on how to do this?

2 Answers 2

2

You can use UNION ALL:

select count(entryid) as table1count, reqname1 from table1
UNION ALL
select count(comid) as table2count, reqname2 from table2
Sign up to request clarification or add additional context in comments.

1 Comment

This will produce duplicate names when they occur in both tables.
1

You could use a sub query that performs a union all, and then group and count:

select name, count(id) 
from (
        select reqname1 as name, entryid as id from table1
        union all
        select reqname2, comid from table2
     ) as combined
group by name

3 Comments

One more thing. How would I work in some date range checking. Say I wanted to get the same results when either date1 is between 8/21/2017 and 8/28/2017 or date2 is between 8/21/2017 and 8/28/2017?
That is a completely different question: after selecting those date fields, add where date1 between #2017-08-21# and #2017-08-28# or date2 between #2017-08-21# and #2017-08-28#. If you cannot make that work, please ask a new question. The comments section is not intended for new questions. ;-)
Thanks generating a new question.

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.