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.
| Author |
Topic |
|
inou
Starting Member
9 Posts |
Posted - 2012-06-18 : 17:26:14
|
Could some one help me with a query to do the below.I have 2 tablesquote: tblCustomer:ID1 ID2 Fname LnameA1 1 Simth M.A1 2 Bob B.A2 1 David B.A3 2 Derek B.A4 3 Tammy N.A4 1 Ann J.tblRequest:ReID CustID1 CustID21 A1 12 A1 13 A2 14 A2 15 A3 26 A1 17 A2 18 A4 39 A1 210 A4 1
How can I get the result below:quote: CustID1 CustID2 Fname Lname TotalRequestsA1 1 Smith M. 3A1 2 Bob B. 1A2 1 David B. 3A3 2 Derek B. 1A4 3 Tammy N. 1A4 1 Ann J. 1
I have tried:quote: SELECT A.[Fname], A.[Lname], A.[ID1], A.[ID2], COUNT(B.[CustID1]) AS TotalRequestFROM [tblCustomer] AS AINNER JOIN [tblRequest] AS B ON B.[CustID2] = A.[ID2]WHERE A.[ID1] = B.[CustID1]GROUP BY A.[Fname], A.[Lname], A.[ID1], A.[ID2]ORDER BY A.[ID2], A.[ID1]
But not getting the right results.Please help!Thanks in advanced. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-18 : 22:40:01
|
| [code]SELECT r.CustID1,r.CustID2,COUNT(r.ReID) AS TotalRequests,c.Fname,c.LnameFROM tblCustomer cINNER JOIN tblRequest rON r.CustID1 = c.ID1AND r.CustID2 = c.ID2GROUP BY r.CustID1,r.CustID2,c.Fname,c.Lname[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
inou
Starting Member
9 Posts |
Posted - 2012-06-19 : 10:59:57
|
quote: Originally posted by visakh16
SELECT r.CustID1,r.CustID2,COUNT(r.ReID) AS TotalRequests,c.Fname,c.LnameFROM tblCustomer cINNER JOIN tblRequest rON r.CustID1 = c.ID1AND r.CustID2 = c.ID2GROUP BY r.CustID1,r.CustID2,c.Fname,c.Lname Thanks, My request tbl contents 900K records but it returns only 400K. Something is missing.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-06-19 : 11:16:24
|
| Perhaps not all customers have requests? Some customers have multiple requests?How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-19 : 14:05:36
|
quote: Originally posted by inou
quote: Originally posted by visakh16
SELECT r.CustID1,r.CustID2,COUNT(r.ReID) AS TotalRequests,c.Fname,c.LnameFROM tblCustomer cINNER JOIN tblRequest rON r.CustID1 = c.ID1AND r.CustID2 = c.ID2GROUP BY r.CustID1,r.CustID2,c.Fname,c.Lname Thanks, My request tbl contents 900K records but it returns only 400K. Something is missing.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
only you'll able to understand whats missing as we cant see neither know about your datamay be you've only 400K unique customer who raised those 900 K requests------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|