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

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

nitagar3107
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..

sodeep
Flowing Fount of Yak Knowledge

USA
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 e.city,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 e.city
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 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 c.city,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 c.city
           )t
On t.city = e.city


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.03 seconds. Powered By: Snitz Forums 2000