索引视图:如何使用索引视图将值插入其他 table?
Indexed views: How to insert value into other table with index views?
我的table:
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)
);
长什么样:
我希望它自动将Amount_Monthly插入到一个新的table中,所以它看起来像这样:
例如如果它说 In_Months = 2 它应该填写一月和二月的 Balance_monthly 到 7058,82。我想让它自动计算就像我让它根据输入自动计算remaining_days。
谢谢!
您需要 12 行,每行代表一个从 1 到 12 的月份。为此,我在 CTE 中使用了一个简单的 union all 查询,但您可能已经有 table 个数字可供使用。然后加入月份数小于或等于 [in_Month] 列的位置。该连接现在会自动将 table 的行乘以所需的月数。
;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] 或类似内容包含在新的 table 中,以便您可以追溯到源行 ID。
如果 CTE 是一个问题,只需使用 "derived table" 代替,例如
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
今天的日期 新的结束日期 剩余天数 每日 2015-11 2015-12 2016-01 2016-02 2016-03 2016-04 2016-05 2016-06 2016-07 2016-08 2016-09 2016-10 2016 -11 2016-12
11/30/2015 12/31/2015 1/31/2016 2/28/2016 3/31/2016 4/30/2016 5/31/2016 6/30/2016 7/31/2016 8/31/2016 9/30/2016 10/31/2016 11/30/2016 12/31/2016
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 $- $- $- $- $- $- $- $- $- $- $-
应该是这样的
@马赫什
所以,@Usedbyalready 的回答似乎有点矫枉过正,我尝试自己在更新中使用 case 来制作它并且它完美地工作。
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;
我还制作了一个触发器,自动将值插入我的月份 table:
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
此处金额未正确拆分,例如,如果余额中的剩余天数如果今天日期为 2015-12-16 且结束日期为 2016-01-31,则剩余天数可能为 46 天,此处金额需要拆分为十二月,即当前月份和一月,谁能告诉我如何实现它
我的table:
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)
);
长什么样:
我希望它自动将Amount_Monthly插入到一个新的table中,所以它看起来像这样:
例如如果它说 In_Months = 2 它应该填写一月和二月的 Balance_monthly 到 7058,82。我想让它自动计算就像我让它根据输入自动计算remaining_days。
谢谢!
您需要 12 行,每行代表一个从 1 到 12 的月份。为此,我在 CTE 中使用了一个简单的 union all 查询,但您可能已经有 table 个数字可供使用。然后加入月份数小于或等于 [in_Month] 列的位置。该连接现在会自动将 table 的行乘以所需的月数。
;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] 或类似内容包含在新的 table 中,以便您可以追溯到源行 ID。
如果 CTE 是一个问题,只需使用 "derived table" 代替,例如
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
今天的日期 新的结束日期 剩余天数 每日 2015-11 2015-12 2016-01 2016-02 2016-03 2016-04 2016-05 2016-06 2016-07 2016-08 2016-09 2016-10 2016 -11 2016-12
11/30/2015 12/31/2015 1/31/2016 2/28/2016 3/31/2016 4/30/2016 5/31/2016 6/30/2016 7/31/2016 8/31/2016 9/30/2016 10/31/2016 11/30/2016 12/31/2016
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 $- $- $- $- $- $- $- $- $- $- $-
应该是这样的
@马赫什 所以,@Usedbyalready 的回答似乎有点矫枉过正,我尝试自己在更新中使用 case 来制作它并且它完美地工作。
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;
我还制作了一个触发器,自动将值插入我的月份 table:
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
此处金额未正确拆分,例如,如果余额中的剩余天数如果今天日期为 2015-12-16 且结束日期为 2016-01-31,则剩余天数可能为 46 天,此处金额需要拆分为十二月,即当前月份和一月,谁能告诉我如何实现它