Solved

How do I get the totals of all tables combined?

Posted on 2011-03-24
6
321 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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 125 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 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