Solved

Count(*) from multiple tables

Posted on 2007-03-23
4
218 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS Conditional Split 7 31
Email Notifications for SQL 2005 9 27
SqlServer no dupes 25 34
VB.NET 2008 - SQL Timeout 9 23
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

813 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

18 Experts available now in Live!

Get 1:1 Help Now