Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 saving the select result to a table

Author  Topic 

jcarrallo
Starting Member

35 Posts

Posted - 2014-06-21 : 05:39:22
Hi SQL Team,

Thanks for your invaluable help.

I have the select statement below - where I use some external functions - and I would like the result to be saved to another table.

After executing the statement I end up with a table containing the following columns:

accountno
d.date_start
d.date_end
TWRR_Month
TWRR_Cum

Normally I would use the command 'INTO TableName' but since the statement is so long I don´t know where to place it.
Please help!
Jay

The select statement is as follows:

IF OBJECT_ID('tempdb..#trn') IS NOT NULL
DROP TABLE #trn

IF OBJECT_ID('tempdb..#mv') IS NOT NULL
DROP TABLE #mv

SELECT PTR_sequence as trno, PTR_CLIENTACCOUNTNUMBER as accountno, PTR_DATE as date_trn,
CASE PTR_TAC
WHEN 'BUY' THEN 0
ELSE PTR_LOCALAMT
END as amt_trn
INTO #trn
FROM MPR_Portfolio_Transactions

SELECT PVL_CLIENTACCOUNTNUMBER as accountno, PVL_DATE as date_mv, PVL_Value as amt_mv
INTO #mv
FROM MPR_Portfolio_Valuations

;with mycte(accountno,dt,cf,mv) as (
SELECT accountno
,date_mv
,amt_mv
,'True'
FROM #mv
UNION ALL
SELECT accountno
,date_trn
,amt_trn
,NULL
FROM #trn
) SELECT d.accountno
,d.date_start
,d.date_end
,wct.GTWRR(t1.cf,t1.dt,t1.mv,2) as TWRR_Month
,wct.GTWRR(t2.cf,t2.dt,t2.mv,2) as TWRR_Cum
FROM (
SELECT m1.accountno as accountno
,m1.date_mv as date_start
,MIN(m2.date_mv) as date_end
FROM #mv m1
JOIN #mv m2
ON m1.accountno = m2.accountno
AND m2.date_mv > m1.date_mv
GROUP BY m1.date_mv
,m1.accountno
) d
JOIN mycte t1
ON t1.accountno = d.accountno
AND t1.dt between d.date_start and d.date_end
JOIN mycte t2
ON t2.accountno = d.accountno AND
t2.dt between '2011-12-31' and d.date_end
GROUP BY d.accountno, d.date_start, d.date_end



Thanks in advanced.
Jay

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-06-21 : 06:15:57
Like below assuming you dont have table already existing
Otherwise you need to use INSERT ...SELECT


IF OBJECT_ID('tempdb..#trn') IS NOT NULL
DROP TABLE #trn

IF OBJECT_ID('tempdb..#mv') IS NOT NULL
DROP TABLE #mv

SELECT PTR_sequence as trno, PTR_CLIENTACCOUNTNUMBER as accountno, PTR_DATE as date_trn,
CASE PTR_TAC
WHEN 'BUY' THEN 0
ELSE PTR_LOCALAMT
END as amt_trn
INTO #trn
FROM MPR_Portfolio_Transactions

SELECT PVL_CLIENTACCOUNTNUMBER as accountno, PVL_DATE as date_mv, PVL_Value as amt_mv
INTO #mv
FROM MPR_Portfolio_Valuations

;with mycte(accountno,dt,cf,mv) as (
SELECT accountno
,date_mv
,amt_mv
,'True'
FROM #mv
UNION ALL
SELECT accountno
,date_trn
,amt_trn
,NULL
FROM #trn
)
SELECT d.accountno
,d.date_start
,d.date_end
,wct.GTWRR(t1.cf,t1.dt,t1.mv,2) as TWRR_Month
,wct.GTWRR(t2.cf,t2.dt,t2.mv,2) as TWRR_Cum
INTO YourTableName
FROM (
SELECT m1.accountno as accountno
,m1.date_mv as date_start
,MIN(m2.date_mv) as date_end
FROM #mv m1
JOIN #mv m2
ON m1.accountno = m2.accountno
AND m2.date_mv > m1.date_mv
GROUP BY m1.date_mv
,m1.accountno
) d
JOIN mycte t1
ON t1.accountno = d.accountno
AND t1.dt between d.date_start and d.date_end
JOIN mycte t2
ON t2.accountno = d.accountno AND
t2.dt between '2011-12-31' and d.date_end
GROUP BY d.accountno, d.date_start, d.date_end




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

jcarrallo
Starting Member

35 Posts

Posted - 2014-06-21 : 06:21:35
Perfect!!!
many thanks
Jay
Go to Top of Page
   

- Advertisement -