Solved

Count(*) from multiple tables

Posted on 2007-03-23
4
219 Views
Last Modified: 2010-03-19
How do i pull a count(*) from 6 tables. Each has a Account ID that should be matched.

Account ID table1 table2 table3 table3 ...
-------------------------------------------------
0001          10         21       7         8
0004 .................
0
Comment
Question by:bender007
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 18783142
Select AccountID,
          COUNT(*) Table1Count,
          (Select COUNT(*) From Table2 t2 Where t1.AccountID = t2.AccountID) Table2Count,
          (Select COUNT(*) From Table3 t3 Where t1.AccountID = t3.AccountID) Table3Count,
...
          (Select COUNT(*) From Tablen tn Where t1.AccountID = tn.AccountID) TablenCount
From Table2 t1
Group By t1.AccountID

0
 

Author Comment

by:bender007
ID: 18783168
What is Table1Count .... I am confused.
0
 

Author Comment

by:bender007
ID: 18783234
Ok its an alias, I got that ,  why start from Table2 ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18783564
>>why start from Table2 ?<<
Look again.  I started with Table1.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

837 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