索引视图:如何使用索引视图将值插入到其他表中?

时间:2015-11-20 10:45:58

标签: sql sql-server indexing triggers indexed-views

我的表:

CREATE TABLE [dbo].[Balance] (
    [Id]             INT             IDENTITY (1, 1) NOT NULL,    
    [Balance]        DECIMAL (18, 2) NOT NULL,
    [Today_Date]     AS              (CONVERT([char](10),getdate(),(126))),
    [Date_end]       DATE            NOT NULL,
    [Remaining_Days] AS              (datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end])),
    [In_Months]      AS              (datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/(30),
    [Amount_Monthly] AS              CAST((case when ((datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/30) = 0 then NULL else [Balance]/((datediff(day,CONVERT([char](10),getdate(),(126)),[Date_end]))/30) end) as DECIMAL(18,2)),
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

看起来如何:

My data

我希望将Amount_Monthly自动插入到新表中,所以它看起来像这样:

My Months table

E.g。如果它说In_Months = 2它应该填写1月和2月的Balance_monthly到7058,82。我想让它自动计算就像我自己计算它会根据输入自动计算剩余天数。

谢谢!

4 个答案:

答案 0 :(得分:1)

你需要12行,每行代表一个月号1到12.我在CTE中使用了一个简单的union all查询,但你可能已经有了一个数字表来代替。然后加入月份数小于或等于[in_Month]列的位置。该连接现在会自动将表格的行数乘以所需的月数。

;with m12 as (
  select 1 as mn
  union all      select 2      union all      select 3      union all      select 4
  union all      select 5      union all      select 6      union all      select 7
  union all      select 8      union all      select 9      union all      select 10
  union all      select 11     union all      select 12
 )
select
        row_number() over(order by b.id, m12.mn) as [ID]
      , datename(month,dateadd(month,m12.mn - 1,0)) as [Month]
      , b.Amount_Monthly as Balance_Monthly
from Balance b
inner join m12 on m12.mn <= b.in_months

请参阅:http://sqlfiddle.com/#!6/4fc6f/3

请注意,您可能希望在新表中将db.balance.id包含为[balanceid]或类似内容,以便追溯到源行ID。

如果CTE是一个问题,只需使用“派生表”,例如

select
        row_number() over(order by b.id, m12.mn) as [ID]
      , datename(month,dateadd(month,m12.mn - 1,0)) as [Month]
      , b.Amount_Monthly as Balance_Monthly
from Balance b
inner join (
              select 1 as mn
              union all      select 2      union all      select 3      union all      select 4
              union all      select 5      union all      select 6      union all      select 7
              union all      select 8      union all      select 9      union all      select 10
              union all      select 11     union all      select 12
           ) as m12 on m12.mn <= b.in_months

答案 1 :(得分:0)

今天&#39; s日期新结束日期剩余天每日2015-11 2015-12 2016-01 2016-02 2016-03 2016-04 2016-05 2016-06 2016-06 2016-08 2016-09 2016-09 2016- 2016-11-11 2016-12                 2016/11/30 12/31/2015 2016/3/28 2016/3/31 4/30/2016 5/31/2016 6/30/2016 7/31/2016 8/31/2016 2016年9月30日10/31/2016 2016/11/30 12/31 10/29/2015 1/4/2016 67 $ 210.71 $ 6,321.33 $ 6,532.04 $ 842.84 $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ -
10/29/2015 1/8/2016 71 $ 283.24 $ 8,497.16 $ 8,780.40 $ 2,265.91 $ - $ - $ - $ - $ - $ - $ - $ - $ - $ - $ -

应该是这样的

答案 2 :(得分:0)

@Mahesh 所以,@ Usedbyalready的答案似乎相当矫枉过正,我试着用自己的案例自己制作一个更新内容,并且效果很好。

UPDATE Months
SET Months.Balance_monthly = 
    CASE 
        WHEN Balance.In_Months > 1 THEN Amount_Monthly          
    END
FROM Balance
JOIN Months 
ON Months.Id <= Balance.In_Months;

我还制作了一个触发器,可以自动将值插入我的Months表中:

CREATE TRIGGER [Balance_monthly]
ON [dbo].[Balance]
FOR INSERT, UPDATE
AS
BEGIN
    SET NOCOUNT ON

    UPDATE Months
    SET Months.Balance_monthly = 
    ((Balance.In_Months + 12 - Months.Id) / 12) * Amount_Monthly          
    FROM Balance
    CROSS JOIN Months;


END

答案 3 :(得分:0)

此金额未正确拆分,例如,如果今天的日期为2015-12-16且结束日期为2016-01-31,余额为余额天数,则剩余天数可能为46天,此处金额需要拆分为12月份即当前月份和1月份,任何人都可以让我知道如何实现它