0

I have 3 tables agencytbl, branchtbl and tourtbl. I am trying to get a count of total branches associated to agency and count of total tours associated to branches. this is my query :

SELECT  dbo.AgencyTbl.AgencyName AS [LIST OF EACH AGENCY],
(select COUNT(*) from BranchTbl where BranchTbl.AgencyID = AgencyTbl.AgencyID) as [NO OF BRANCHES],
(SELECT COUNT(*) AS Expr1 FROM dbo.TourTbl 
WHERE      (dbo.BranchTbl.BranchID = BranchID)) AS [NO TOURS EVER]

FROM dbo.AgencyTbl INNER JOIN dbo.BranchTbl ON dbo.AgencyTbl.AgencyID = dbo.BranchTbl.AgencyID 
GROUP BY AgencyTbl.AgencyName, AgencyTbl.AgencyID, dbo.BranchTbl.BranchID
order by AgencyTbl.AgencyName

but if I run this i get same agency repeated because of the group by by branchid as follow :

 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  2
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
 this estate agent  12  0
    next agency     47  2
   next agency      47  4
   next agency      47  1
   next agency      47  1

How can I just list one agency and total count of branches belonging to that agency and then total count of tours belonging to that branch and not repeating the agency names.

2 Answers 2

2

I assume that by "total count of tours belonging to that branch", you mean "total count of tours belonging to all branches belonging to that agency"?

If so, you can use either of these approaches.

Approach 1: Only use subqueries, don't join to BranchTbl:

SELECT  dbo.AgencyTbl.AgencyName AS [LIST OF EACH AGENCY],
        ( SELECT COUNT(*)
            FROM BranchTbl
           WHERE AgencyID = AgencyTbl.AgencyID
        ) AS [NO OF BRANCHES],
        ( SELECT COUNT(*)
            FROM dbo.TourTbl
           WHERE BranchID IN
                  ( SELECT BranchID
                      FROM dbo.BranchTbl
                     WHERE AgencyID = AgencyTbl.AgencyID
                  )
        ) AS [NO TOURS EVER]
  FROM dbo.AgencyTbl
 GROUP
    BY AgencyTbl.AgencyName,
       AgencyTbl.AgencyID
 ORDER
    BY AgencyTbl.AgencyName
;

Approach 2: Only use joins, don't use subqueries:

SELECT dbo.AgencyTbl.AgencyName AS [LIST OF EACH AGENCY],
       COUNT(DISTINCT dbo.BranchTbl.BranchID) AS [NO OF BRANCHES],
       COUNT(dbo.TourTbl.BranchTbl) AS [NO TOURS EVER]
  FROM dbo.AgencyTbl
  LEFT
  JOIN dbo.BranchTbl
    ON dbo.BranchTbl.AgencyID = dbo.AgencyTbl.AgencyID
  LEFT
  JOIN dbo.TourTbl
    ON dbo.TourTbl.BranchID = dbo.BranchTbl.BranchID
 GROUP
    BY AgencyTbl.AgencyName
 ORDER
    BY AgencyTbl.AgencyName
;

There are a few other approaches — for example, you can join to BranchTbl, but use a SUM(SELECT COUNT(*) FROM ...) to get the number of tours — but I think the above are the clearest.

[Disclaimer: I have not tested either of the above queries.]

1
  • +1 thanks so much the first approach i think solved my problem
    – Zaki
    Mar 5, 2012 at 13:21
1

First of all, you don't need some of your sub-queries since you are already doing a JOIN. On top of that, you are grouping by more columns that you need. Try this instead:

SELECT  dbo.AgencyTbl.AgencyName AS [LIST OF EACH AGENCY],
        COUNT(*) AS [NO OF BRANCHES],
        COUNT(dbo.TourTbl.*) AS [NO TOURS EVER],
        COUNT(CASE WHEN YEAR(DT_Started) = YEAR(GETDATE()) THEN dbo.TourTbl.BranchID ELSE NULL END)  [NO OF TOURS YTD (Year To Date)]
FROM dbo.AgencyTbl 
INNER JOIN dbo.BranchTbl 
ON dbo.AgencyTbl.AgencyID = dbo.BranchTbl.AgencyID 
LEFT JOIN dbo.TourTbl 
ON dbo.BranchTbl.BranchID = dbo.TourTbl.BranchID
GROUP BY AgencyTbl.AgencyName
ORDER BY AgencyTbl.AgencyName
2
  • thanks for that, if i add another column to count tours to date how would i do that? i used to do this before : (SELECT COUNT(*) AS Expr1 FROM dbo.TourTbl WHERE (dbo.BranchTbl.BranchID = BranchID) AND (YEAR(DT_Started) = YEAR({ fn NOW() }))) AS [NO OF TOURS YTD (Year To Date)]
    – Zaki
    Mar 5, 2012 at 13:17
  • @Sam1 - Ok, added that column
    – Lamak
    Mar 5, 2012 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.