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

はい、構文は次のとおりです: http://www.w3schools.com/sql/sql_select_into.asp

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