如何访问临时表的此字段?

时间:2017-03-28 22:12:01

标签: sql sql-server tsql temp-tables

我有一个复杂的查询,它将东西填充到临时表中,然后读取并组合这些临时表。最后,我有这段代码:

    Select distinct CSDirector=r1.CSDirector,
        Category = r1.Category,
        Segment = r1.Segment,
        r1.unit,
        NumUnits=r1.NumUnits,
        NumUnitsLast=r2.NumUnits,
        MonthSales=r1.MonthSales,
        MonthSalesLast=r2.MonthSales,
        MonthPerc = (r1.MonthSales - r2.MonthSales) / case when isnull(r2.MonthSales,0) = 0 then 1 else r2.MonthSales end, 
        YTDSales = r1.YTDSales, 
        YTDSalesLast = r2.YTDSales,
        YTDPerc= (r1.YTDSales - r2.YTDSales) / case when isnull(r2.YTDSales,0) = 0 then 1 else r2.YTDSales end,
        ProjSales = r1.ProjSales, 
        YTDProjSales = r1.YTDProjSales,
        YTDBudgetPerc = r1.YTDBudgetPerc,
        NewBiz = isnull((Select NewBiz from MasterUnitsProjSales where Unit = r1.unit and Cyear=@Cyear),0)  
into    #CombinedYears
From #CurrentYear r1 inner join #PriorYear r2 on r1.unit=r2.unit
order by NewBiz,r1.Unit

declare @Unit varchar(30);
declare @paramdate datetime;
set @paramdate = convert(datetime,convert(char(4),@CYear)
                +right('0'+convert(varchar(2),@CMonth),2)
                +'01') 

IF OBJECT_ID('tempdb.dbo.#Units', 'U') IS NOT NULL
DROP TABLE #Units

select distinct unit
into #Units
from ReportingMonthlySales;

select 
    u.Unit
  , New      = sum(case when ccl.Subcategory = 'New'      then rms.MonthlySales else 0 end)
  , Assumed  = sum(case when ccl.Subcategory = 'Assumed'  then rms.MonthlySales else 0 end)
  , Existing = sum(case when ccl.Subcategory = 'Existing' then rms.MonthlySales else 0 end)
  , Organic  = sum(case when ccl.Subcategory = 'Organic'  then rms.MonthlySales else 0 end)
into    #CategorizedUnits
from #Units u
join CustomerCategoryLog   ccl on u.Unit = ccl.Unit and @paramdate >= ccl.begindate and @paramdate <= ccl.enddate
join ReportingMonthlySales rms on u.Unit = rms.Unit and rms.cyear  = @cyear and rms.cmonth = @cmonth
group by u.unit;

-- Now combine combined years and categorized units
select CY.CSDirector, CY.Category, CY.Segment, CY._Unit, 
       CU.New, CU.Assumed, CU.Existing, CU.Organic, 
       CY.NumUnits, CY.NumUnitsLast, CY.MonthSales, CY.MonthSalesLast, 
       CY.MonthPerc, CY.YTDSales, CY.YTDSalesLast, CY.YTDPerc,
       CY.ProjSales, CY.YTDProjSales, CY.YTDBudgetPerc, CY.NewBiz 
from #CombinedYears CY
left join #CategorizedUnits CU on CU.Unit = CY.Unit

...但是它失败了&#34; 错误207:列名无效&#39; r1&#39;。&#34;在这一行:

select CY.CSDirector, CY.Category, CY.Segment, CY.r1.unit, 

所以,我想也许我需要改变这个:

Select distinct CSDirector=r1.CSDirector,
        Category = r1.Category,
        Segment = r1.Segment,
        r1.unit,

......对此:

Select distinct CSDirector=r1.CSDirector,
        Category = r1.Category,
        Segment = r1.Segment,
        _Unit = r1.unit,

......以及:

select CY.CSDirector, CY.Category, CY.Segment, CY.r1.unit, 

......对此:

select CY.CSDirector, CY.Category, CY.Segment, CY._Unit, 

...但是当我尝试时,我得到了&#34; 错误207:列名无效&#39; _Unit&#39;。&#34;在这一行:

select CY.CSDirector, CY.Category, CY.Segment, CY._Unit, 

那么我怎样才能抓住&#34; Unit&#34;来自#CombinedYears表的值?

更新

这也失败了:

--select CY.CSDirector, CY.Category, CY.Segment, CY.r1.unit, 
--select CY.CSDirector, CY.Category, CY.Segment, CY._Unit, 
select CY.CSDirector, CY.Category, CY.Segment, CY.unit,

...使用&#34; 错误207:列名称无效&#39;单位&#39;。&#34;

更新2

这有效:

--CREATE PROCEDURE [dbo].[sp_ReportMonthlySalesEnhanced]
--  @CYear  int;
--  @CMonth int;
--as

declare @CYear  int = 2017; -- remove before above is uncommented
declare @CMonth int = 3; -- " "

IF OBJECT_ID('tempdb.dbo.#Units', 'U') IS NOT NULL
DROP TABLE #Units

IF OBJECT_ID('tempdb.dbo.#CurrentYear', 'U') IS NOT NULL
DROP TABLE #CurrentYear

IF OBJECT_ID('tempdb.dbo.#PriorYear', 'U') IS NOT NULL
DROP TABLE #PriorYear

IF OBJECT_ID('tempdb.dbo.#CombinedYears', 'U') IS NOT NULL
DROP TABLE #CombinedYears

IF OBJECT_ID('tempdb.dbo.#CategorizedUnits', 'U') IS NOT NULL
Drop Table #CategorizedUnits

Select  CSDirector,
        Category,
        Segment,
        r1.unit,
        NumUnits=isnull((Select sum(NumUnits) from ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear and cmonth = @Cmonth),0),    
        MonthSales=isnull((Select sum(MonthlySales) from ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear and cmonth = @Cmonth),0.00), 
        YTDSales = (Select sum(MonthlySales) From  ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear and cmonth <= @Cmonth),
        ProjSales =  (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear),
        YTDProjSales = (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear) / 12 * @Cmonth,
        YTDBudgetPerc = (Select sum(MonthlySales) From  ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear and cmonth <= @Cmonth) / 
            case when (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear) = 0 then 1 else ((Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear) / 12 * @Cmonth) end
into    #CurrentYear
From    MasterUnitsProjSales r1 
where   r1.Cyear=@CYear
order by r1.NewBiz,r1.Unit

Select  CSDirector,
        Category,
        Segment,
        r1.unit,
        NumUnits=isnull((Select sum(NumUnits) from ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear - 1 and cmonth = @Cmonth),0),
        MonthSales=isnull((Select sum(MonthlySales) from ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear - 1 and cmonth = @Cmonth),0.00),
        YTDSales = (Select sum(MonthlySales) From  ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear - 1 and cmonth <= @Cmonth),
        ProjSales =  (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear - 1 ),
        YTDProjSales = (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear - 1) / 12 * @Cmonth,
        YTDBudgetPerc = (Select sum(MonthlySales) From  ReportingMonthlySales where unit=r1.unit and 
cyear=r1.cyear - 1 and cmonth <= @Cmonth) / 
            case when (Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear - 1 ) = 0 then 1 else ((Select ProjectedSales from MasterUnitsProjSales where UNit = r1.Unit and 
Cyear=r1.cyear - 1) / 12 * @CMonth) end
into    #PriorYear
From    MasterUnitsProjSales r1 
where r1.Cyear=@CYear
order by r1.NewBiz,r1.Unit

Select distinct CSDirector=r1.CSDirector,
        Category = r1.Category,
        Segment = r1.Segment,
        r1.unit [_Unit],  
        NumUnits=r1.NumUnits,
        NumUnitsLast=r2.NumUnits,
        MonthSales=r1.MonthSales,
        MonthSalesLast=r2.MonthSales,
        MonthPerc = (r1.MonthSales - r2.MonthSales) / case when isnull(r2.MonthSales,0) = 0 then 1 else r2.MonthSales end, 
        YTDSales = r1.YTDSales, 
        YTDSalesLast = r2.YTDSales,
        YTDPerc= (r1.YTDSales - r2.YTDSales) / case when isnull(r2.YTDSales,0) = 0 then 1 else r2.YTDSales end,
        ProjSales = r1.ProjSales, 
        YTDProjSales = r1.YTDProjSales,
        YTDBudgetPerc = r1.YTDBudgetPerc,
        NewBiz = isnull((Select NewBiz from MasterUnitsProjSales where Unit = r1.unit and Cyear=@Cyear),0)  
into    #CombinedYears
From #CurrentYear r1 inner join #PriorYear r2 on r1.unit=r2.unit
order by NewBiz,r1.Unit

declare @Unit varchar(30);
declare @paramdate datetime;
set @paramdate = convert(datetime,convert(char(4),@CYear)
                +right('0'+convert(varchar(2),@CMonth),2)
                +'01') 

select distinct unit
into #Units
from ReportingMonthlySales;

select 
    u.Unit
  , New      = sum(case when ccl.Subcategory = 'New'      then rms.MonthlySales else 0 end)
  , Assumed  = sum(case when ccl.Subcategory = 'Assumed'  then rms.MonthlySales else 0 end)
  , Existing = sum(case when ccl.Subcategory = 'Existing' then rms.MonthlySales else 0 end)
  , Organic  = sum(case when ccl.Subcategory = 'Organic'  then rms.MonthlySales else 0 end)
into    #CategorizedUnits
from #Units u
join CustomerCategoryLog   ccl on u.Unit = ccl.Unit and @paramdate >= ccl.begindate and @paramdate <= ccl.enddate
join ReportingMonthlySales rms on u.Unit = rms.Unit and rms.cyear  = @cyear and rms.cmonth = @cmonth
group by u.unit;

-- Now combine combined years and categorized units
select CY.CSDirector, CY.Category, CY.Segment, CY._Unit, 
       CU.New, CU.Assumed, CU.Existing, CU.Organic, 
       CY.NumUnits, CY.NumUnitsLast, CY.MonthSales, CY.MonthSalesLast, 
       CY.MonthPerc, CY.YTDSales, CY.YTDSalesLast, CY.YTDPerc,
       CY.ProjSales, CY.YTDProjSales, CY.YTDBudgetPerc, CY.NewBiz 
from #CombinedYears CY
left join #CategorizedUnits CU on CU.Unit = CY._Unit

1 个答案:

答案 0 :(得分:3)

开始...

select * from #CombinedYears  

应该能够识别字段名称 或者在下面的查询中明确命名字段... [_ Unit]。

  Select distinct CSDirector=r1.CSDirector,
        Category = r1.Category,
        Segment = r1.Segment,
        r1.unit [_Unit],   -- here
        NumUnits=r1.NumUnits,
        NumUnitsLast=r2.NumUnits,
        MonthSales=r1.MonthSales,
        MonthSalesLast=r2.MonthSales,
        MonthPerc = (r1.MonthSales - r2.MonthSales) / case when isnull(r2.MonthSales,0) = 0 then 1 else r2.MonthSales end, 
        YTDSales = r1.YTDSales, 
        YTDSalesLast = r2.YTDSales,
        YTDPerc= (r1.YTDSales - r2.YTDSales) / case when isnull(r2.YTDSales,0) = 0 then 1 else r2.YTDSales end,
        ProjSales = r1.ProjSales, 
        YTDProjSales = r1.YTDProjSales,
        YTDBudgetPerc = r1.YTDBudgetPerc,
        NewBiz = isnull((Select NewBiz from MasterUnitsProjSales where Unit = r1.unit and Cyear=@Cyear),0)  
into    #CombinedYears
From #CurrentYear r1 inner join #PriorYear r2 on r1.unit=r2.unit
order by NewBiz,r1.Unit