3

我在查询中计算了许多新列,我想知道是否可以将查询结果保存到新表/工作表中。因此,当我打开新表时,我可以看到查询结果,而不必每次打开 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。谢谢!

4

4 回答 4

3
SELECT a.[CUSIP NUMBER],
       ...
INTO newTable
FROM  DBO.mbs032013 a
...
于 2013-06-24T16:55:56.277 回答
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' 
于 2013-06-24T17:07:39.027 回答
2

是的,语法显示在这里:http ://www.w3schools.com/sql/sql_select_into.asp

SELECT *
INTO newtable [IN externaldb]
FROM table1;
于 2013-06-24T16:56:14.770 回答
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' 
于 2013-06-24T16:57:51.850 回答