我为什么要“宣布@Sales_Total

时间:2011-11-11 18:29:58

标签: sql ssrs-2008

运行此查询时,我收到错误“必须声明@sales_total”:

--temp Department table needed to handle plywood mills where sawlines are not yet 
DECLARE @Department TABLE
(Department_number uniqueidentifier
,Process_number uniqueidentifier
,Department_Name nvarchar(100))

INSERT @Department 
(Department_number 
,Process_number 
,Department_Name)
((SELECT 'D3BC304C-E3EF-4119-AF9B-02253114B4F2', '87B1D819-06A6-4551-A8AE- 349232F652EC','Elgin.Sawline')  -- Elgin Sawline from Layup
UNION (SELECT '6A5A052C-65B3-4F09-8A85-7E1CD5EF5003', '5CA0310F-D9AA-4E0E-AFAB-DFB77A2A19AD','KF.Sawline')  -- KF Sawline from Layup
UNION (SELECT '81F4E6F2-AC8B-4002-8A40-0D8A632CB041','C86711E2-F86B-4F20-AF44-378791D0369F' ,'MedfordPly.Sawline')  --Medford Sawline from spray line
UNION (SELECT '81F4E6F2-AC8B-4002-8A40-0D8A632CB041','BA420B4D-B413-4F53-A34E-7E1A43B42824' ,'MedfordPly.Sawline')  --Medford Sawline from curtain coater
UNION (SELECT '335465EB-54A1-48E2-A69F-671A38BF7B84','36FD8A9A-0CCE-4B86-972F-F0DD3DF4813D' ,'Oakdale.Sawline') --Oakdale actual sawline
UNION (SELECT '089B086A-E431-4691-B2F0-5F84EBE31F80','86EDB559-4C53-4261-BB32-372677CD4231'              ,'Florien.Sawline') --Florien actual sawline
)
-- temp production table
Declare @Production TABLE
(Plant_Number uniqueidentifier
,Plant_name       nvarchar (50)
,Department_number uniqueidentifier
,Production_Date datetime
,Production_Volume decimal(18,6))

Insert into @Production 
(Plant_Number
,Plant_name
,Department_number
,Production_Date
,Production_Volume)

(
SELECT p.[Plant_Number]
           ,p.plant_name    
  ,d.department_number
  ,pf.date
  ,(ppf.Good_Output/1000)

FROM @Department D
Inner join [TrueOpportunity].[dbo].[Department] dpt
on d.department_number = dpt.department_number

inner join [TrueOpportunity].[dbo].[Plant] p
on p.plant_number = dpt.plant_number

inner join [TrueOpportunity].[dbo].[Process] prc
on d.process_number = prc.process_number

inner join [TrueOpportunity].[dbo].[Production_Fact] pf
on prc.process_number = pf.process_number
--and Month (pf.date) = Month (@monthtodate)

inner join [TrueOpportunity].[dbo].[Production_Process_Fact] ppf
on ppf.production_number = pf.production_number  

--Group by p.Plant_Number, d.Department_number, pf.date, p.plant_name, ppf.Good_Output
)  

Declare @ProdTotal TABLE
(Plant_Number uniqueidentifier
,Plant_name       nvarchar (50)
,Production_Volume decimal(18,6)
,Prod_date datetime)

Insert into @ProdTotal 
(Plant_Number 
,Plant_name 
,Production_Volume
,Prod_date
)


(SELECT   
p.plant_number
,p.plant_name
,sum(p.production_volume) 
,p.production_date
from @Production p
--where plant_name = 'Medford Plywood'
where Production_date between @BeginningDate and @EndingDate

group by p.plant_number
,p.plant_name
,p.production_date)

Declare @Sales TABLE
(Plant_Number uniqueidentifier
,Plant_name       nvarchar (50)
,Actual_Volume decimal(18,6)
,Budget_Volume decimal(18,6)
,Actual_Sales_Dollars decimal(18,6)
,Average_Price decimal(18,6)
,sales_date datetime)

Insert into @Sales 
(Plant_Number 
,Plant_name 
,Actual_Volume
,Budget_Volume
,Actual_Sales_Dollars
,Average_Price
,Sales_Date)

(SELECT   P.[Plant_Number]
          ,pls.plant_code
          ,(pls.[Actual_Volume])  
          ,(pls.[Budget_Volume])
          ,(pls.[Actual_Sales_Dollars])
          ,CASE                    
            WHEN   coalesce (pls.[Actual_Volume],0) = 0 and
                   coalesce (pls.[Actual_Sales_Dollars],0) = 0
                   THEN 0
            ELSE ((pls.[Actual_Sales_Dollars]/pls.[Actual_Volume]))     
            END   AS 'Average Price'
           ,pls.production_date 

FROM woodproduction.dbo.plywood_layup_sales pls
inner join @Production p
on p.plant_number = pls.plant_number
           and p.production_date = pls.production_date

left join woodproduction.dbo.process_inventory pinv
on pinv.plant_code = pls.plant_code
and pinv.inventory_date = pls.production_date

left join trueopportunity.dbo.process prc
on pinv.department_number = prc.department_number




WHERE 
    pls.production_date between @BeginningDate and @EndingDate
    and pls.actual_volume <> 0

GROUP BY
P.[Plant_Number]
,pls.plant_code
,pls.production_date
,pls.[Actual_Volume]  
,pls.[Budget_Volume]
,pls.[Actual_Sales_Dollars]

)
--select * from @Sales

Declare @Sales_TOTAL TABLE
(Plant_Number uniqueidentifier
,Plant_name       nvarchar (50)
,Actual_Volume decimal(18,6)
,Budget_Volume decimal(18,6)
,Actual_Sales_Dollars decimal(18,6)
,Average_Price decimal(18,6)
,sales_date datetime)

Insert into @Sales_TOTAL
(Plant_Number 
,Plant_name 
,Actual_Volume
,Budget_Volume
,Actual_Sales_Dollars
,Average_Price
,sales_date)

(Select
s.Plant_Number 
,s.Plant_name 
,sum(s.Actual_Volume)
,sum(s.Budget_Volume)
,sum(s.Actual_Sales_Dollars)
,CASE                    
WHEN coalesce (sum(s.Actual_Volume),0) = 0 
THEN 0
ELSE (sum(s.Actual_Sales_Dollars)/sum(s.Actual_Volume))     
END                      
AS 'Average Price'
,s.sales_date

 from @Sales s
where s.sales_date between @BeginningDate and @EndingDate

group by s.plant_number, s.plant_name, s.sales_date

)

--Select * from @Sales_Total


Declare @Sales_Prod TABLE
(Plant_Number uniqueidentifier
,Plant_name       nvarchar (50)
,Actual_Volume decimal(18,6)
,Budget_Volume decimal(18,6)
,Actual_Sales_Dollars decimal(18,6)
,Average_Price decimal(18,6)
,Production_Volume decimal(18,6)
,sales_date datetime)

Insert into @Sales_Prod
(Plant_Number 
,Plant_name 
,Actual_Volume
,Budget_Volume
,Actual_Sales_Dollars
,Average_Price
,Production_Volume
,sales_date)

(Select
st.Plant_Number 
,st.Plant_name 
,st.Actual_Volume
,st.Budget_Volume
,st.Actual_Sales_Dollars
,st.Average_Price
,pt.Production_Volume
,st.sales_date

from @Sales_Total st
,@ProdTotal pt
where st.sales_date = pt.prod_date
and st.plant_number = pt.plant_number
and st.sales_date between @BeginningDate and @EndingDate
)

--Select * from @Sales_Prod

当我在Management Studio中运行此查询并将我的日期参数替换为实际日期时,我会得到我期望的数据。因此,我将其复制/粘贴到SSRS 2008中,然后确保删除了所有临时日期,并将其替换为我的'@beg .. @ end'参数。但由于某种原因@sales_total显示为'定义查询参数'并且我一直在查看它并且无法看到除了DECLARE @Sales_Total TABLE之外的哪个地方@sales_total本身作为单独的参数出现。

有人可以看看,看看我错过了什么。也许我一直在盯着它看?提前致谢。

1 个答案:

答案 0 :(得分:0)

区分大小写......您声明 Sales_TOTAL ,并尝试从 Sales_Total 中选择。