获取一行SQL中的列的总平均值

时间:2015-05-28 19:22:19

标签: sql average sql-server-2014

我是一家保险公司的实习生,并获得了一项任务,用于计算x客户的保费平均值。

我正在使用SQL Server 2014 !!

以下是我的问题:

SELECT  

    Customer.custno 
    , Customer.custid 
    , (Customer.firstname + ' ' + Customer.lastname) AS Client 
    , BasicPolInfo.enteredDate
    , BasicPolInfo.changedDate
    , BasicPolInfo.fulltermpremium AS Premium
    , AVG(BasicPolInfo.fulltermpremium) over(ORDER BY Customer.custno 
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS 'Total Premium'

FROM 

    Customer INNER JOIN
    BasicPolInfo ON Customer.CustId = BasicPolInfo.CustId INNER JOIN
    Transaction ON BasicPolInfo.PolId = Transaction.PolId INNER JOIN
    GeneralLedgerBranch ON Customer.GLBrnchCode = GeneralLedgerBranch.GLBrnchCode INNER JOIN
    GeneralLedgerDepartment ON Customer.GLDeptCode = GeneralLedgerDepartment.GLDeptCode INNER JOIN
    GeneralLedgerDivision ON Customer.GLDivCode = GeneralLedgerDivision.GLDivCode INNER JOIN
    Employee ON BasicPolInfo.ExecCode = Employee.EmpCode

WHERE   

    Customer.Active = 'Y'  
    AND BasicPolInfo.poltypelob = 'Homeowners'
    AND BasicPolInfo.status <> 'D'
    AND BasicPolInfo.fulltermpremium > '0.00'
    AND BasicPolInfo.polexpdate >= GetDate()


GROUP BY 

Customer.custno 
    , Customer.custid
    , Customer.firstname 
    , Customer.lastname 
    , BasicPolInfo.ChangedDate
    , BasicPolInfo.entereddate
    , Employee.firstname
    , Employee.lastname
    , Customer.enteredDate
    , basicpolinfo.fulltermpremium

ORDER BY 

    Customer.enteredDate ASC

所以我的表看起来像这样:

  

Custno | CustID |客户| EnteredDate | ChangedDate |高级| TotalPremium

     

xxxxxx | xxxxxx | xxxxxx | xxxxxxxxxxx | xxxxxxxxxxx | 1350.38 | 1350.38

     

xxxxxx | xxxxxx | xxxxxx | xxxxxxxxxxx | xxxxxxxxxxx | 3517.00 | 2433.69

     

xxxxxx | xxxxxx | xxxxxx | xxxxxxxxxxx | xxxxxxxxxxx | 2094.00 | 2320.46

     

xxxxxx | xxxxxx | xxxxxx | xxxxxxxxxxx | xxxxxxxxxxx | 1811.00 | 2193.09

所以我的老板想要的只是平均值的一列。例如,如果这只有4个客户,那么它只能显示2193.09(包括之前的所有计算)并且不会显示数千行。

  

总保费

     

2193.09

如果您有任何疑问,请发表评论。老实说,我尽我所能以最好的方式解释它......

我们试图以图形方式仅代表1总保费平均值。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

如果我正确理解了问题,您只需将整个查询包装在:

SELECT AVG(Premium)
FROM 
( 

  <your query>

) as MyQuery

答案 1 :(得分:0)

做同样事情的不同方式......

With CTE AS (SELECT  
    Customer.custno 
    , Customer.custid 
    , (Customer.firstname + ' ' + Customer.lastname) AS Client 
    , BasicPolInfo.enteredDate
    , BasicPolInfo.changedDate
    , BasicPolInfo.fulltermpremium AS Premium
    , AVG(BasicPolInfo.fulltermpremium) over(ORDER BY Customer.custno 
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS 'Total Premium'

FROM 

    Customer INNER JOIN
    BasicPolInfo ON Customer.CustId = BasicPolInfo.CustId INNER JOIN
    Transaction ON BasicPolInfo.PolId = Transaction.PolId INNER JOIN
    GeneralLedgerBranch ON Customer.GLBrnchCode = GeneralLedgerBranch.GLBrnchCode INNER JOIN
    GeneralLedgerDepartment ON Customer.GLDeptCode = GeneralLedgerDepartment.GLDeptCode INNER JOIN
    GeneralLedgerDivision ON Customer.GLDivCode = GeneralLedgerDivision.GLDivCode INNER JOIN
    Employee ON BasicPolInfo.ExecCode = Employee.EmpCode

WHERE   

    Customer.Active = 'Y'  
    AND BasicPolInfo.poltypelob = 'Homeowners'
    AND BasicPolInfo.status <> 'D'
    AND BasicPolInfo.fulltermpremium > '0.00'
    AND BasicPolInfo.polexpdate >= GetDate()


GROUP BY 

Customer.custno 
    , Customer.custid
    , Customer.firstname 
    , Customer.lastname 
    , BasicPolInfo.ChangedDate
    , BasicPolInfo.entereddate
    , Employee.firstname
    , Employee.lastname
    , Customer.enteredDate
    , basicpolinfo.fulltermpremium

ORDER BY 

    Customer.enteredDate ASC)

SELECT AVG(fulltermpremium) from cte