?
Solved

How do I get the totals of all tables combined?

Posted on 2011-03-24
6
Medium Priority
?
323 Views
Last Modified: 2012-05-11
I have six tables that I need total of all of the records combined. I tried to make a query to add all the tables, but it is not working.
0
Comment
Question by:gigifarrow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35212621
--add what?  the count of rows?  the sum of a column?

;with CTE as (
  select 'Table1' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table1
  UNION
  select 'Table2' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table2
  UNION
  select 'Table3' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table3
  UNION
  select 'Table4' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table4
  UNION
  select 'Table5' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table5
  UNION
  select 'Table6' as TableName, sum(myColumn) as mySum, count(*) as myCount
  from Table6
)

select sum(mySum), sum(myCount)
from CTE
0
 

Author Comment

by:gigifarrow
ID: 35212637
count total record for each table then take all the records total from all tables.
0
 
LVL 7

Expert Comment

by:mmr159
ID: 35212680
SELECT
    (SELECT COUNT(*) FROM table1) +
    (SELECT COUNT(*) FROM table2) +
    (SELECT COUNT(*) FROM table3) +
    (SELECT COUNT(*) FROM table4) +
    (SELECT COUNT(*) FROM table5) +
    (SELECT COUNT(*) FROM table6)
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35212746
hi,

if you want row count for all tables then


SELECT object_name (i.id) TableName, rows as RowCnt
FROM sysindexes i INNER JOIN sysObjects o ON (o.id = i.id AND o.xType = 'U')
WHERE indid < 2
--AND TABLENAME IN ('table1','table2','table3','table4','table5') --You give tablename too
ORDER BY TableName

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35212748

SELECT object_name (i.id) TableName, rows as RowCnt
FROM sysindexes i INNER JOIN sysObjects o ON (o.id = i.id AND o.xType = 'U')
WHERE indid < 2
--AND TABLENAME IN ('table1','table2','table3','table4','table5') --You give tablename too
ORDER BY TableName

Open in new window

0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 35213229
From your question, it seems like your doing this in Access?  If so, create a query for each of the tables that gets the count and save them (SELECT COUNT(*) AS RECCOUNT FROM <TABLE>).  Save it something like, Table1Count, Table2Count, etc.

Then create a new query with each of the queries you saved as the "tables". SQL Will look like this:

SELECT [Table1Count].[RECCOUNT]+[Table2Count].[RECCOUNT]+[Table3Count].[RECCOUNT] AS TotalCount
FROM Table1Count, Table2Count, Table3Count;

JimD.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question