question

DataMavn82 avatar image
DataMavn82 asked

Summing Counts from Multiple Tables

--Supplier Count SELECT id, COUNT (*) AS cnt FROM atable WHERE id IN ('1','2',3') GROUP BY id ORDER BY id --id Count SELECT id, COUNT (*) AS cnt FROM btable WHERE id IN ('1','2',3') GROUP BY id ORDER BY id How do I add the Count of id's1,2,3 from atable and btable in one piece of code without having to process atable and btable separately then adding the Counts for each supplier in Excel?
tablesctecountcountingcounts
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Dave_Green avatar image
Dave_Green answered
The [UNION][1] operator will allow you to return these counts as one set. If your data values may overlap (and you want to then group these and return the sum) as your question implies, then I suggest using UNION ALL in a CTE and then selecting from that the ID and the SUM of the counts. [1]: http://msdn.microsoft.com/en-gb/library/ms180026.aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

DataMavn82 avatar image DataMavn82 commented ·
Great, thank you
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.