我正在运行查询:
Select s.scheme, c.surname, p.policynumber, cp.type, ce.TotalReceived
from clients c, schemes s, policies p, commnPremiums cp, CommnEntries ce
where c.clientref = p.clientref and
s.schemeref = p.schemeref and
p.policyref = cp.policyref and
cp.CommnPremRef = ce.CommnPremRef and
s.schemeref = '164003232' and c.surname = 'Smith'
返回以下记录:
scheme surname policynumber type TotalReceived
AxaElevate Smith XEL123 7 1147.38
AxaElevate Smith XEL123 7 285.14
AxaElevate Smith XEL123 7 501.16
AxaElevate Smith XEL123 1 2688.62
AxaElevate Smith XEL123 5 149.49
AxaElevate Smith XEL123 1 94.7
AxaElevate Smith XEL123 5 163.69
AxaElevate Smith XEL123 5 166.65
我想将totalReceived按类型相加以产生:
scheme surname policynumber type TotalReceived
AxaElevate Smith XEL123 7 1933.68
AxaElevate Smith XEL123 1 2783.32
AxaElevate Smith XEL123 5 479.83
答案 0 :(得分:1)
你需要使用group by和sum:
Select s.scheme,
c.surname,
p.policynumber,
cp.type,
sum(ce.TotalReceived )
from clients c, schemes s, policies p, commnPremiums cp, CommnEntries ce
where c.clientref = p.clientref and
s.schemeref = p.schemeref and
p.policyref = cp.policyref and
cp.CommnPremRef = ce.CommnPremRef and
s.schemeref = '164003232' and c.surname = 'Smith'
GROUP BY
s.scheme,
c.surname,
p.policynumber,
cp.type
使用聚合函数(如sum)后,您必须在select中的每个字段上进行聚合或分组。
答案 1 :(得分:0)
你可以分组,然后你可以做总和
Select s.scheme, c.surname, p.policynumber, cp.type,
sum(ce.TotalReceived)
from clients c, schemes s, policies p, commnPremiums cp, CommnEntries ce
where c.clientref = p.clientref and
s.schemeref = p.schemeref and
p.policyref = cp.policyref and
cp.CommnPremRef = ce.CommnPremRef and
s.schemeref = '164003232' and c.surname = 'Smith'
GROUP BY
s.scheme,
c.surname,
p.policynumber,
cp.type