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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql query help
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 11/19/2012 :  04:13:59  Show Profile  Reply with Quote
Hi,I am trying to execute a sql query which says to count the number of customer who live in same city of employees and also find their average checking balance. I have four tables customer(cid,city ,cname)..employees(eid,city,ename)....transactions(tid,aid,cid,eid,trans_date,amount,trans_type)... and accounts(aid,balance,account_type,date_opened).. Please anyone can help me out it will be great... Thanks..

Flowing Fount of Yak Knowledge

7174 Posts

Posted - 11/19/2012 :  13:11:54  Show Profile  Reply with Quote
Though your question is not clear:

Here is my shot:

Select,Count(Cid),AVG(balance) as CheckingBalance
from customer c
inner join transactions t on t.cid = c.cid
inner join employees e on e.eid = t.eid
inner join accounts a on a.tid = t.tid
Where account_type = 'Checking'
Group by
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 11/20/2012 :  02:11:34  Show Profile  Reply with Quote

SELECT e.*,t.CustCnt,t.AvgBal
FROM employees e
INNER JOIN (SELECT,COUNT(DISTINCT c.cid) AS CustCnt, AVG(a.balance*1.0) AS AvgBal
            FROM customer c
            INNER JOIN transactions t
            ON t.cid = c.cid
            INNER JOIN accounts a
            ON a.aid = t.aid
            WHERE a.account_type='Checking'
            GROUP BY
On =

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.13 seconds. Powered By: Snitz Forums 2000