SUMing每行中的多个列

时间:2013-06-04 19:20:10

标签: sql-server sum aggregate

我想做的事情非常简单,我甚至认为我之前已经做过了;我只是记不住了。

ReinType    Coverage    ReCompany   AmountInForce   StatReserves    StatReserveExtra    ExcessCashValue DeficientPremiumReserve Stat2PUAReserve Stat2OYTReserve SubstandardOYT
RPRREIN BASE    ALLIANZ (K) 3305506.00  35979.82    2778.16 0   0   0.00    0.00    0
ULRPR   BASE    ALLIANZ (K) 1323210.00  24966.79    5697    0   0   0.00    0.00    0
ULRPR   BASE    BENEFICIAL LIFE (BL)    18339535.00 54467.53    790.13  0   0   0.00    0.00    0
MODCO   BASE    BUS MEN ASSUR (E)   1006577.00  566569.17   2289.57 4605.44 92.45   78415.61    400.64  0
MODCO   NEWDIM  BUS MEN ASSUR (E)   658.00  385.65  0   0   0   0.00    0.00    NULL
RPRREIN BASE    BUS MEN ASSUR (P)   65260.00    5283.45 0   0   0   0.00    0.00    NULL
COINSURE    BASE    CNA (9) 19152525.00 117831.7    57618.22    0   0   0.00    0.00    0
COINSURE    BIO CNA (9) 6808203.00  8004.2  0   0   0   0.00    0.00    NULL
COINSURE    FLATX   CNA (9) 1259425.00  1445.72 0   0   0   0.00    0.00    NULL
RPRREIN BASE    CNA (9) 198104996.00    733905.880000001    55875.78    0   0   0.00    0.00    0
RPRREIN TERM RDR    CNA (9) 8642375.00  10476.13    2699.85 0   0   0.00    0.00    0
ULFLATX UL FLATX    CNA (9) 212720.00   118.13  0   0   0   0.00    0.00    NULL
ULRPR   BASE    CNA (9) 223013469.00    1980612.21  51517.54    0   0   0.00    0.00    0
ULRPR   TERM RDR    CNA (9) 469200.00   1026.11 256 0   0   0.00    0.00    0
COINSURE    BASE    COLOGNE (C) 1848128.00  1248961.51  2537.26 2053.8  308.37  66626.65    1758.88 0
COINSURE    DEF ADDS    COLOGNE (C) 0.00    13750   0   0   0   0.00    0.00    NULL
COINSURE    DIS WP  COLOGNE (C) 2627.00 10412.14    0   0   0   0.00    0.00    NULL
COINSURE    MPRAR   COLOGNE (C) 502252.50   421721.33   0   0   0   0.00    0.00    NULL
COINSURE    NEWDIM  COLOGNE (C) 1416.00 937.79  0   0.01    0   0.00    0.00    NULL
COINSURE    WP  COLOGNE (C) 2488.58 91.93   0   0   0   0.00    0.00    NULL
COINSURE    FLATX   COLOGNE (X) 1595706.00  4552.49 0   0   0   0.00    0.00    NULL
RPRREIN BASE    COLOGNE (X) 202172511.00    514207.93   117499.93   0   0   0.00    0.00    0

我对桌子的质量感到抱歉。在本网站上提问时,我仍然不知道如何正确地制作一个。无论如何我想要做的是有一个列,它将对最后7列的每一行求和。

这是查询:

SELECT top 100 percent 
tblReinsurance.ReinType, 
tblReinsurance.Coverage, 
tblReinsurance.ReCompany,
Sum(tblReinsurance.AMOUNT_INFORCE) AS AmountInForce,
Sum(tblReinsurance.StatReserve) AS StatReserves,
SUM(tblReinsurance.StatResX) AS StatReserveExtra, 
Sum(tblReinsurance.ExcessCashValue) AS ExcessCashValue,
Sum(tblReinsurance.DefPremiumReserve) AS DeficientPremiumReserve,
Sum(tblReinsurance.STAT2_PUA_RES) AS Stat2PUAReserve, 
Sum(tblReinsurance.STAT2_OYT_RES) AS Stat2OYTReserve, 
Sum(tblReinsurance.OYTRS2X) AS SubstandardOYT


FROM tblReinsurance join tblCoverageFaceAmountMultiplier

ON tblReinsurance.Coverage = tblCoverageFaceAmountMultiplier.Coverage

WHERE (((tblReinsurance.TERM_REASON)='A') AND ((tblReinsurance.ISSUE_DATE)<=tblReinsurance.[VAL_DATE])) and COMPANY_CODE = 'NL' 
--AND tblCoverageFaceAmountMultiplier.FaceAmountMultiplier = 1

GROUP BY 
tblReinsurance.ReCompany,
tblReinsurance.ReinType, 
tblReinsurance.Coverage
order by 
tblReinsurance.ReCompany,
tblReinsurance.ReinType, 
tblReinsurance.Coverage

1 个答案:

答案 0 :(得分:1)

你要求WITH ROLLUP吗?我发现你的问题有点难以理解。

MSDN Documentation


EDIT 那么为什么不只是在选择列表中添加另一列。

Sum(tblReinsurance.StatReserve) + SUM(tblReinsurance.StatResX) + Sum(tblReinsurance.DefPremiumReserve) AS SUMMED_SOME_COLUMNS