Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get records and count of sub records

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-11-18 : 19:03:34
i have tables account and sub account

i am trying to write a query where i get all the accounts and the count of the subacccounts

the are joined by account.accountid = subaccount.accountid

how do i do this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-18 : 19:04:48
Show us sample data and expected output.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-11-18 : 19:09:35
accountid accountname

1 AccountName

Subaccountid SubAccountname SubaccountAccountID

1 SubName1 1
2 SubName2 1
3 SubName3 1

Output

accountid, accountname, count(subaccount)

1 AccountName 3
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-18 : 19:15:06
SELECT t1.accountid, t1.accountname, COUNT(*)
FROM account t1
JOIN subaccount t2
ON t1.accountid = t2.accountid
GROUP BY t1.accountid, t1.accountname

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2010-11-18 : 19:24:59
thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-18 : 19:37:04
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -