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
 Merging temp tables together at the end of a cte.

Author  Topic 

kotonikak
Yak Posting Veteran

92 Posts

Posted - 2012-08-09 : 11:40:28
Hi all,

I have 4 temporary tables all with the same columns that I want to merge together (union operator) at the end of a cte. I have something like this...

with ___ as
(
lots of temp tables
)


select RptLOBPrintOrder,
RptLOB,
SequentialPeriod,
UnitMonth,
PatternType,
PaymentPattern
into dbo.PaymentPatternECMLOB
from
(select * from WeightedBegPPAnnual
union
select * from WeightedBegPPQuart
union
select * from WeightedInPPAnnual
union
select * from WeightedInPPQuart
)


This is not working but I basically just want to put all of these tables into dbo.PaymentPatternECMLOB. How can I do this??

I would really appreciate any feedback! Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 12:04:06
sorry you question is not clear. where's cte accessed? are you trying to join cte to query?
or are you trying to include union result also to cte

you've not shown name of CTE so i'm not sure whether you're accesing it after

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kotonikak
Yak Posting Veteran

92 Posts

Posted - 2012-08-09 : 12:10:54
the cte has multiple temp tables which calculate what I need...so...

with WeightedBegPPAnnual as
(
)
, WeightedBegPPQuart as
(
)
,WeightedInPPAnnual as
(
)
,WeightedInPPQuart as
(
)

select RptLOBPrintOrder,
RptLOB,
SequentialPeriod,
UnitMonth,
PatternType,
PaymentPattern
into dbo.PaymentPatternECMLOB
from
(select * from WeightedBegPPAnnual
union
select * from WeightedBegPPQuart
union
select * from WeightedInPPAnnual
union
select * from WeightedInPPQuart
)


Those are not my only temp tables, there's more which do the actual calculations but these 4 are the ones I would like to merge together. All of these tables have the same columns (the 6 specified in the select statement)
Go to Top of Page

kotonikak
Yak Posting Veteran

92 Posts

Posted - 2012-08-09 : 12:11:46
the Union is still in the cte clause
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 12:39:57
quote:
Originally posted by kotonikak

the cte has multiple temp tables which calculate what I need...so...

with WeightedBegPPAnnual as
(
)
, WeightedBegPPQuart as
(
)
,WeightedInPPAnnual as
(
)
,WeightedInPPQuart as
(
)

select RptLOBPrintOrder,
RptLOB,
SequentialPeriod,
UnitMonth,
PatternType,
PaymentPattern
into dbo.PaymentPatternECMLOB
from
(select * from WeightedBegPPAnnual
union
select * from WeightedBegPPQuart
union
select * from WeightedInPPAnnual
union
select * from WeightedInPPQuart
)


Those are not my only temp tables, there's more which do the actual calculations but these 4 are the ones I would like to merge together. All of these tables have the same columns (the 6 specified in the select statement)


i can see multiple ctes here

so are you trying to merge data from ctes?
if yes, the ctes should have same number of columns and also corresponding columns should be of compatible datatype

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kotonikak
Yak Posting Veteran

92 Posts

Posted - 2012-08-09 : 12:42:52
Yes my bad, i meant to say multiple instead of one. My terminology is still wacky.

I'm trying to merge the data from those 4 cte's. They have the same number of columns and all are of the same datatype.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 12:55:59
then its just a matter of adding an alias


;with WeightedBegPPAnnual as
(
)
, WeightedBegPPQuart as
(
)
,WeightedInPPAnnual as
(
)
,WeightedInPPQuart as
(
)

select RptLOBPrintOrder,
RptLOB,
SequentialPeriod,
UnitMonth,
PatternType,
PaymentPattern
into dbo.PaymentPatternECMLOB
from
(select * from WeightedBegPPAnnual
union
select * from WeightedBegPPQuart
union
select * from WeightedInPPAnnual
union
select * from WeightedInPPQuart
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kotonikak
Yak Posting Veteran

92 Posts

Posted - 2012-08-09 : 12:57:56
Thanks I just got it doing something else but I'm sure that's another way of doing it. Thank you for your time and help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 15:05:40
quote:
Originally posted by kotonikak

Thanks I just got it doing something else but I'm sure that's another way of doing it. Thank you for your time and help!


what other way are you trying to implement? are you trying to dispense with cte? unless you explain your attempt clearly we wont be able to provide accurate help. We cant read minds!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -