3

I have calculated a number of new columns in my query and I was wondering if it is possible to save the query results into a new table/sheet. So when I open the new table I can see the query results without having to re-run the query every time upon opening SQL.

Here is the code I am using:

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' 

The query ultimately generates a function called 'CPR' for bond analysis and I would like to add these results and the other columns to a permanent table.

I am using SQL Server 2012. Thanks!

1
  • 3
    Have you checked out SELECT ... INTO? Commented Jun 24, 2013 at 16:53

4 Answers 4

3
SELECT a.[CUSIP NUMBER],
       ...
INTO newTable
FROM  DBO.mbs032013 a
...
Sign up to request clarification or add additional context in comments.

2 Comments

This works but I can't find the new table that I have created. Where does it go?
@user2501239: It's called 'newTable'
2

Yes, the syntax is shown here: http://www.w3schools.com/sql/sql_select_into.asp

SELECT *
INTO newtable [IN externaldb]
FROM table1;

Comments

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' 

Comments

1

A table that is specifically a subset of another/set of others is best represented in a 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' 

3 Comments

Except that a VIEW will rerun the query every time it is accessed. From the phrasing of the question, the OP apparently wants to freeze the data as it is without needing to regenerate it.
Wouldn't INSERT INTO newTable SELECT * FROM ...... etc have to be used since this will be a historical table and SELECT INTO creates a newTable everytime?
Yes I just want to freeze the data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.