|
Question : SQL #Tmp tables
|
|
I have a two temp tables and I want to place the results which are in table #2 to be the las trow in table #1 Both select statements work just fine but if i try and do a union i get a datatype error. Is there any way accomplish this. ************** TABLE 1 ***************** SELECT Cast(ActualSpend as Decimal(20,2)) as 'Spend', -- job cost (savings exluded) CAST((cast(100 * CAST(ActualSpend AS Decimal(20, 2)) / CAST(( select Total from #tmp2 ) AS Decimal(20, 2)) as decimal(20,2)))AS varchar(20)) + '%' AS '% Spend',-- percentage of total spent x as 'No.Job', -- Job Quantity Cast(Missedopportunties as Decimal(20,2)) as 'Missed Opportunties', -- one of the 4 savings categories Cast(ValueEngineering as Decimal(20,2)) as 'Value Engineering', -- one of the 4 savings categories CustomerDepartment as 'Customer Department', -- client department Cast(Marketrate as Decimal(20,2)) as 'Marketrate', -- one of the 4 savings categories Cast((Missedopportunties + ValueEngineering + Referencepricing + Marketrate) as Decimal(20,2)) as 'Total Savings by Dept', -- total savings by department Cast((ActualSpend + Missedopportunties + ValueEngineering + Referencepricing + Marketrate ) as Decimal(20,2)) as 'Base Line Spend', -- job cost total without savings Cast((cast((Cast((Missedopportunties + ValueEngineering + Referencepricing + Marketrate) as Decimal(20,2))/Cast((ActualSpend + Missedopportunties + ValueEngineering + Referencepricing + Marketrate) as Decimal(20,2)) * 100) as decimal(20,2))) as varchar) + '%' as '% Savings' -- percentage of savings FROM #tmp
******************* TABLE 2 ************* SELECT Sum(ActualSpend) as 'Total', '' as '%' , sum(x) as TotJobs, 0 as 'Missed Opportunities', 0 as 'Value Engineering', '' as 'Customer Department', 0 as 'Marketrate', sum(Missedopportunties + ValueEngineering + Referencepricing + Marketrate) as 'Total Savings', Sum(ActualSpend + Missedopportunties + ValueEngineering + Referencepricing + Marketrate ) as 'Base Line', 0 as 'Average' INTO #tmp2 from #tmp
update #tmp2 set Average = (select cast(sum(cast(Savings as Decimal(20,2)))/ count(*)as Decimal(20,2)) from #tmp4) ********************
Thanks
|
|
Answer : SQL #Tmp tables
|
|
insert into #Tab2 (field1,field2.... select field1,field2.... from #tab
|
|
|
|