admin

总和与众不同不会改变结果吗?

sql

我是一个新手,目前为止还没有运气尝试过此工作,非常感谢您的帮助!!!

Select Distinct
   (AB.agency_no || '-' || ab.branch_no) AS "AGENCY-BRANCH",
   count (AB.agency_no || '-' || ab.branch_no) AS Occurences,
   A.AGY_NAME AS AGENCY,
   Sum(AB.annual_premium) as Premium
 From Agency_Book_View AB, Agency A, Branch B
 Where AB.agency_no = A.Agency_No
   AND B.EXPIRATION_DATE = TO_DATE('12-31-2078', 'MM-DD-YYYY')
   AND B.EFFECTIVE_DATE <= sysdate and b.effective_date >=sysdate - 364
 Group by AB.agency_no || '-' || ab.branch_no, A.Agy_Name, ab.annual_premium
 Order by AB.agency_no || '-' || ab.branch_no

因此,我试图返回每个“机构分支机构”的年度总保费,并且出现了多次“机构分支机构”的情况。我正在尝试使每个代理商分支机构只有一条生产线。我希望这是清楚的。我试图包含结果集,但不允许在我的帖子中包含图片。

非常感谢!


阅读 165

收藏
2021-06-07

共1个答案

admin

试试这个 :

SELECT (AB.agency_no || '-' || AB.branch_no) AS "AGENCY-BRANCH",
    COUNT(AB.agency_no || '-' || AB.branch_no) AS Occurences,
    A.AGY_NAME AS AGENCY,
    SUM(AB.annual_premium) AS Premium
FROM Agency_Book_View AB, Agency A, Branch B
WHERE AB.agency_no = A.Agency_No AND AB.branch_no = B.branch_no
  AND B.EXPIRATION_DATE = TO_DATE('12-31-2078', 'MM-DD-YYYY')
  AND B.EFFECTIVE_DATE <= SYSDATE AND B.effective_date >= SYSDATE - 364
GROUP BY AB.agency_no || '-' || AB.branch_no, A.Agy_Name
ORDER BY AB.agency_no || '-' || AB.branch_no

我加入了B表和AB表,删除了DISTINCT和GROUPED BY ab.annual_premium。

2021-06-07