我有3个表agencytbl,branchtbl和tourtbl。我试图计算与分支机构相关的总分行数和与分行相关的总体行程数。这是我的查询:集团按分支发行
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
,但如果我运行此我得到同样的机构重复,因为该组由branchid如下:
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
我怎么能只列出一个机构和总数属于该机构的分支机构,然后计算属于该分支机构的旅行总数并且不重复机构名称。
+1感谢这么多的第一种方法,我认为解决我的问题 – Zaki 2012-03-05 13:21:50
@样本1:不客气! – ruakh 2012-03-05 13:38:03