Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I get the totals of all tables combined?

Posted on 2011-03-24
6
Medium Priority
?
324 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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