64

I have a SQL query, looks something like this:

select name, count (*) from Results group by name order by name

and another, identical which loads from a archive results table, but the fields are the same.

select name, count (*) from Archive_Results group by name order by name

How would I combine the two in just one query? (So the group by would still function correctly). I tried with union all, however it won't work. What am I missing?

David Božjak
  • 16,887
  • 18
  • 67
  • 98

5 Answers5

113
SELECT tem.name, COUNT(*) 
FROM (
  SELECT name FROM results
  UNION ALL
  SELECT name FROM archive_results
) AS tem
GROUP BY name
ORDER BY name
Artyom Ionash
  • 405
  • 7
  • 17
krdluzni
  • 799
  • 2
  • 9
  • 10
  • 3
    Thank you. All what I was missing was the "as tem" part ... Forgot that I have to name the "table" I create in order for this to work. – David Božjak Aug 12 '09 at 15:06
  • 8
    This will give the wrong answer. In fact, it will give a count of 1 for every name, because UNION is by default UNION DISTINCT. Use UNION ALL. – Steve Kass Aug 12 '09 at 19:59
  • Thank you Steve Kass, however I already knew that I needed to use UNION ALL. As stated above all what I was missing was the "as" term. – David Božjak Aug 13 '09 at 10:55
  • 2
    @Rekreativc: No problem. I commented because you marked the solution as Best Answer, and I didn't want future readers to think it was a correct answer. – Steve Kass Aug 13 '09 at 16:47
11

If you have supporting indexes, and relatively high counts, something like this may be considerably faster than the solutions suggested:

SELECT name, MAX(Rcount) + MAX(Acount) AS TotalCount
FROM (
  SELECT name, COUNT(*) AS Rcount, 0 AS Acount
  FROM Results GROUP BY name
  UNION ALL
  SELECT name, 0, count(*)
  FROM Archive_Results
  GROUP BY name
) AS Both
GROUP BY name
ORDER BY name;
Artyom Ionash
  • 405
  • 7
  • 17
Steve Kass
  • 7,144
  • 20
  • 26
9

Is your goal...

  1. To count all the instances of "Bob Jones" in both tables (for example)
  2. To count all the instances of "Bob Jones" in Results in one row and all the instances of "Bob Jones" in Archive_Results in a separate row?

Assuming it's #1 you'd want something like...

SELECT name, COUNT(*) FROM
(SELECT name FROM Results UNION ALL SELECT name FROM Archive_Results)
GROUP BY name
ORDER BY name
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • what about case 2? – Surya Mar 30 '20 at 06:05
  • For #2, you're probably better off just running two queries and consolidating them in software. But you could also just do `SELECT name, COUNT(1) FROM Results GROUP BY name UNION SELECT name, COUNT(1) FROM Archive_Results` if you absolutely had to union the two. – VoteyDisciple Mar 30 '20 at 11:23
2
select T1.name, count (*)
from (select name from Results 
      union 
      select name from Archive_Results) as T1
group by T1.name order by T1.name
0
SELECT SUM(*) 
FROM (
  SELECT COUNT(*) AS cnt FROM IP_IRRADIATE_SUBMISSION_PHOTON WHERE IRRADIATESUBMISSIONMASTERID = 84                        
  UNION SELECT COUNT(*) AS cnt FROM IP_IRRADIATE_SUBMISSION_ELECTRON WHERE IRRADIATESUBMISSIONMASTERID = 84
);

This worked in Oracle. The "As TMP" was throwing it. Slightly different query in that in sums several rows of counts.

Rob
  • 2,363
  • 7
  • 36
  • 54