子查询返回的值超过1。更新选择减号

时间:2015-05-26 22:00:10

标签: sql sql-server

我有以下更新scrpit ..

UPDATE LIMRA_Retail_Current 
SET AnnualizedWOExcessPremium = (SELECT   
                        ISNULL(ul.AnnualizedWOExcessPremium, 0)- 
                        isnull((select iul.AnnualizedWOExcessPremium   
                        from dbo.LIMRA_Retail_Current  iul
                        where ul.Distribution = iul.distribution
                        and   ul.company    = iul.company
                        and   ul.Year       = iul.year
                        and   ul.Quarter    = iul.quarter
                        and   iul.Market = 'Retail'
                        and   replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
            ),0) UL_MINUS_IUL
FROM  dbo.LIMRA_Retail_Current  UL
WHERE ul.Product    like 'Universal Life%'
AND   ul.Market = 'Retail')

如何使用UL_MINUS_IUL的所有值更新YearizedWOExcessPremium列。我想在更新中使用所有值。

1 个答案:

答案 0 :(得分:0)

看起来你的子选择只需要一个SUM:

(SELECT   
    SUM(ISNULL(ul.AnnualizedWOExcessPremium, 0)-  isnull((select iul.AnnualizedWOExcessPremium   )
    from dbo.LIMRA_Retail_Current  iul
    where ul.Distribution = iul.distribution
    and   ul.company    = iul.company
    and   ul.Year       = iul.year
    and   ul.Quarter    = iul.quarter
    and   iul.Market = 'Retail'
    and   replace(ul.Product, 'Universal Life', 'Index Universal Life') = iul.product
),0) UL_MINUS_IUL