我在查询中计算了许多新列,我想知道是否可以将查询结果保存到新的表/表中。因此,当我打开新表时,我可以看到查询结果,而无需在每次打开SQL时重新运行查询。
以下是我正在使用的代码:
SELECT a.[CUSIP NUMBER],
a.[CURRENT BALANCE],
a.[ORIGINAL WA MATURITY],
a.[CURRENT WA MATURITY],
a.[PASS THRU RATE] [PASS THRU RATE],
a.[CURRENT FACTOR],
b.[CURRENT FACTOR],
b.[ORIGINAL BALANCE],
MonthlyRate,
Payment,
InterestPayment,
Principle,
ScheduledFace,
PreviousFace,
ScheduledFactor,
SMM,
CPR
FROM DBO.mbs032013 a
JOIN dbo.mbs042013 b ON a.[CUSIP NUMBER] = b.[CUSIP NUMBER]
CROSS APPLY (Select (a.[PASS THRU RATE]*.01)/12) CA(MonthlyRate)
CROSS APPLY (Select (a.[CURRENT BALANCE] * ((MonthlyRate)/((1-(1/power(1+ MonthlyRate, a.[CURRENT WA MATURITY]))))))) CA2(Payment)
Cross Apply (Select a.[CURRENT BALANCE] * MonthlyRate) CA3 (InterestPayment)
Cross Apply (Select Payment - InterestPayment) CA4 (Principle)
Cross Apply (Select a.[ORIGINAL BALANCE] * a.[CURRENT FACTOR]) CA5 (PreviousFace)
CROSS APPLY (Select PreviousFace - Principle) CA6(ScheduledFace)
Cross Apply (Select ScheduledFace/a.[ORIGINAL BALANCE]) CA7 (ScheduledFactor)
Cross Apply (Select 100 * (1-(b.[CURRENT FACTOR]/ScheduledFactor))) CA8(SMM)
Cross Apply (Select (1-(power(1-SMM/100,12)))*100) CA9 (CPR)
WHERE a.[CURRENT WA MATURITY] != 0 and a.[CURRENT BALANCE] != 0 and a.[CUSIP NUMBER] = '31416hag0'
查询最终生成一个名为“CPR”的函数用于债券分析,我想将这些结果和其他列添加到永久表中。
我正在使用SQL Server 2012.谢谢!
答案 0 :(得分:2)
SELECT a.[CUSIP NUMBER],
...
INTO newTable
FROM DBO.mbs032013 a
...
答案 1 :(得分:2)
是的,语法如下所示:http://www.w3schools.com/sql/sql_select_into.asp
SELECT *
INTO newtable [IN externaldb]
FROM table1;
答案 2 :(得分:2)
SELECT a.[CUSIP NUMBER],
a.[CURRENT BALANCE],
a.[ORIGINAL WA MATURITY],
a.[CURRENT WA MATURITY],
a.[PASS THRU RATE] [PASS THRU RATE],
a.[CURRENT FACTOR],
b.[CURRENT FACTOR],
b.[ORIGINAL BALANCE],
MonthlyRate,
Payment,
InterestPayment,
Principle,
ScheduledFace,
PreviousFace,
ScheduledFactor,
SMM,
CPR
INTO NewTable --the new table to be created
FROM DBO.mbs032013 a
JOIN dbo.mbs042013 b ON a.[CUSIP NUMBER] = b.[CUSIP NUMBER]
CROSS APPLY (Select (a.[PASS THRU RATE]*.01)/12) CA(MonthlyRate)
CROSS APPLY (Select (a.[CURRENT BALANCE] * ((MonthlyRate)/((1-(1/power(1+ MonthlyRate, a.[CURRENT WA MATURITY]))))))) CA2(Payment)
Cross Apply (Select a.[CURRENT BALANCE] * MonthlyRate) CA3 (InterestPayment)
Cross Apply (Select Payment - InterestPayment) CA4 (Principle)
Cross Apply (Select a.[ORIGINAL BALANCE] * a.[CURRENT FACTOR]) CA5 (PreviousFace)
CROSS APPLY (Select PreviousFace - Principle) CA6(ScheduledFace)
Cross Apply (Select ScheduledFace/a.[ORIGINAL BALANCE]) CA7 (ScheduledFactor)
Cross Apply (Select 100 * (1-(b.[CURRENT FACTOR]/ScheduledFactor))) CA8(SMM)
Cross Apply (Select (1-(power(1-SMM/100,12)))*100) CA9 (CPR)
WHERE a.[CURRENT WA MATURITY] != 0 and a.[CURRENT BALANCE] != 0 and a.[CUSIP NUMBER] = '31416hag0'
答案 3 :(得分:1)
特别是另一个/一组其他子集的表最好在VIEW中表示。
CREATE VIEW [dbo].[newView]
AS
SELECT a.[CUSIP NUMBER],
a.[CURRENT BALANCE],
a.[ORIGINAL WA MATURITY],
a.[CURRENT WA MATURITY],
a.[PASS THRU RATE] [PASS THRU RATE],
a.[CURRENT FACTOR],
b.[CURRENT FACTOR],
b.[ORIGINAL BALANCE],
MonthlyRate,
Payment,
InterestPayment,
Principle,
ScheduledFace,
PreviousFace,
ScheduledFactor,
SMM,
CPR
FROM DBO.mbs032013 a
JOIN dbo.mbs042013 b ON a.[CUSIP NUMBER] = b.[CUSIP NUMBER]
CROSS APPLY (Select (a.[PASS THRU RATE]*.01)/12) CA(MonthlyRate)
CROSS APPLY (Select (a.[CURRENT BALANCE] * ((MonthlyRate)/((1-(1/power(1+ MonthlyRate, a.[CURRENT WA MATURITY]))))))) CA2(Payment)
Cross Apply (Select a.[CURRENT BALANCE] * MonthlyRate) CA3 (InterestPayment)
Cross Apply (Select Payment - InterestPayment) CA4 (Principle)
Cross Apply (Select a.[ORIGINAL BALANCE] * a.[CURRENT FACTOR]) CA5 (PreviousFace)
CROSS APPLY (Select PreviousFace - Principle) CA6(ScheduledFace)
Cross Apply (Select ScheduledFace/a.[ORIGINAL BALANCE]) CA7 (ScheduledFactor)
Cross Apply (Select 100 * (1-(b.[CURRENT FACTOR]/ScheduledFactor))) CA8(SMM)
Cross Apply (Select (1-(power(1-SMM/100,12)))*100) CA9 (CPR)
WHERE a.[CURRENT WA MATURITY] != 0 and a.[CURRENT BALANCE] != 0 and a.[CUSIP NUMBER] = '31416hag0'