Solved

How do I get the totals of all tables combined?

Posted on 2011-03-24
6
314 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
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 57

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now