27

How can I select the count from an inner join with grouping?

SELECT COUNT(table1.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act
GROUP BY table1.act

This will return the count of act's found in table2.

Adding

 SELECT table1.act, COUNT(table1.act) AS test

Returns

act     test
------- ----
17682   3
17679   3
17677   3
11636   1
11505   1

I want to receive the count of act's found total.

So I want to get 5. Can you help?

0

6 Answers 6

38

You can wrap that query into another query:

SELECT COUNT(*) FROM (
    SELECT COUNT(table1.act) AS actCount FROM table1
    INNER JOIN table2 ON table1.act = table2.act
    GROUP BY table1.act
) t
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this will be a bit slow, but will work with whatever query you actually have in there. If it is as simple as that, notice you're just counting distinct table1.act that has a match in table2.act
Fast enough for what i'm doing. Query took 0.0142 sec. Thanks again!
12

Use count distinct

SELECT COUNT(distinct table1.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act

4 Comments

I tried that too. That returns. act test ------- ---- 17682 1 17679 2 17677 1 11636 1 11505 1
That's strange. Something must have happened when you pasted the code. See this working demo on sqlfiddle sqlfiddle.com/#!3/1ab86/1
Won't this return all of the distinct acts?
Thanks, this works for me in a similar query. Maybe @StevePayne forgot to remove the "GROUP BY" from his orignal query.
2

Wrap the entire query with a SELECT COUNT statement as shown in the answer from Mosty Mostacho, just wanted to post this answer with an as statement since normally it's better to return results with a column title.

SELECT COUNT(*) as FilteredActs FROM (
    SELECT COUNT(table1.act) AS actCount FROM table1
    INNER JOIN table2 ON table1.act = table2.act
    GROUP BY table1.act
) T

Results:

output results

Comments

1
SELECT COUNT(table2.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act

1 Comment

I need the GROUP BY because there are multiple fields in table2 with the same act. In the above statement that would return 11.
1

if you just want the result count,

SELECT     COUNT(1)
FROM       table1
INNER JOIN table2 ON table1.act = table2.act
GROUP BY   table1.act

that should give you

Comments

-3
SELECT COUNT(*) AS total_records
FROM T1
INNER JOIN T2 ON T1.emp_id = T2.emp_id
WHERE T1.emp_id BETWEEN 1 AND 50;

1 Comment

Pardon me, but I don't see how your answer is related to the posted question. The names of the [database] tables are table1 and table2 (and not T1 and T2) and the name of the column is act (and not emp_id) and I don't understand why the BETWEEN operator is required. Maybe you meant to post this answer to a different 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.