我正在运行一个MTD查询,我得到30天的信息,但我想汇总它

时间:2011-12-01 17:52:30

标签: sql sql-server-2008 formatting ssrs-2008

我的查询正在运行,但是当我将其放入SSRS时,它会返回所有日期和所有工厂(它应该)的数据: results

我的WTD专栏效果很好但是你可以看到我的MTD每天都会显示每个植物,我希望它像我的WTD一样聚合。我现在要道歉,但这个问题很长。由于我不确定问题出在哪里,我觉得发布整个查询更好,而不是因为没有提前提供足够的信息而打了我的手。提前致谢!

我的老板收集了临时表(我还在学习数据所在的位置),因为其中一个工厂有两个不同的流程需要汇集在一起​​。因此它看起来很混乱的原因(可能是运行3分钟的原因)。我道歉并感谢你的时间和建设性的批评。

P.S。我不确定这是查询问题还是SSRS格式问题

Declare @endofmonth  datetime
Declare @begofmonth  datetime
Declare @monthtodate    datetime

set @endofmonth =  DATEADD(mm,-1,DATEADD(mm,  DATEDIFF(m,0,convert(varchar(10),getdate(),111)),0))--prior month end
set @begofmonth = dateadd (mm,-1,DATEADD(dd,-   (DAY(DATEADD(mm,1,convert(varchar(10),getdate(),111)))-  1),DATEADD(mm,0,convert(varchar(10),getdate(),111))))-- prior month beginning
set @monthtodate = getdate()

--temp Department table needed to handle plywood mills where sawlines are not yet     collecting data
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 @Production1 TABLE
(Plant_Number uniqueidentifier
,Plant_name nvarchar (50)
,Department_number uniqueidentifier
,Production_Date datetime
,Production_Volume decimal(18,6))

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

(
SELECT 
 dpt.[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

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

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

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

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 @production1 p

where Production_date = p.production_date 

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

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

Insert into @Sales 
(Plant_Number 
,Plant_name 
,Budget_Realization
,Actual_Volume
,Budget_Volume
,Gross_Production_Per_Hr
,Production_Volume
,Actual_Sales_Dollars
,Average_Price
,PriMoAvgPrice
,Sales_Date)

(
SELECT
P.[Plant_Number]
,p.plant_name
,avg(pls.[Budget_Realization]) AS 'BR'
,(pls.[Actual_Volume] ) AS 'AV'
,(pls.[Budget_Volume])   AS 'BV'
,(dpb.[Gross_Production_Per_Hr]) AS 'GPB'
,(p.Production_Volume) AS 'PV'
,(pls.[Actual_Sales_Dollars]) AS 'ASD'
,(sum(pls.[Actual_Sales_Dollars])/sum(pls.[Actual_Volume])) AS 'AP'
,((select(sum(pls2.[Actual_Sales_Dollars])/sum(pls2.[Actual_Volume]))
FROM woodproduction.dbo.plywood_layup_sales pls2
WHERE
(pls2.Production_Date between dateadd (mm,-1,DATEADD(dd,-(DAY(DATEADD(mm,1,convert(varchar(10),getdate(),111)))-1),DATEADD(mm,0,convert(varchar(10),getdate(),111))))
                and DATEADD(dd,-1,DATEADD(mm, DATEDIFF(m,0,convert(varchar(10),getdate(),111)),0))
                and actual_volume <> 0
                and pls2.plant_code = pls.plant_code)))
,pls.production_date

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

inner join woodproduction.dbo.department_production_budget dpb
on pls.plant_number = dpb.plant_number

inner join trueopportunity.dbo.department dpt
on dpb.department_number = dpt.department_number

WHERE 
MONTH (pls.production_date)= 11--Month (@monthtodate)
and pls.actual_volume <> 0

GROUP BY
P.[Plant_Number]
,p.plant_name
,pls.plant_code
,pls.production_date
,pls.[Actual_Volume]
,pls.[Budget_Volume]
,dpb.[Gross_Production_Per_Hr]
,p.Production_Volume
,pls.[Actual_Sales_Dollars]

)   

--select * from @Sales      

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

Insert into @Sales_TOTAL
(Plant_Number 
,Plant_name 
,Budget_Realization
,Actual_Volume
,Budget_Volume
,Gross_Production_Per_Hr
,Production_Volume
,Actual_Sales_Dollars
,Average_Price
,PriMoAvgPrice
,Sales_Date)

(select
s.Plant_Number 
,s.Plant_name 
,(s.Budget_Realization)
,(s.Actual_Volume)
,(s.Budget_Volume)
,(s.Gross_Production_Per_Hr)
,sum(s.Production_Volume)
,(s.Actual_Sales_Dollars)
,(s.Average_Price)
,(s.PriMoAvgPrice)
,s.Sales_Date

from @Sales s

group by s.plant_number, s.plant_name, s.sales_date,s.Budget_Realization, s.PriMoAvgPrice,s.Average_Price, s.Budget_Volume, s.Actual_Sales_Dollars, s.Actual_Volume, s.Gross_Production_Per_Hr

)

--Select * from @Sales_Total

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

Insert into @Sales_Prod
(Plant_Number 
,Plant_name 
,Budget_Realization
,Actual_Volume
,Budget_Volume
,Gross_Production_Per_Hr
,Production_Volume
,Actual_Sales_Dollars
,Average_Price
,PriMoAvgPrice
,Sales_Date)

(Select
st.Plant_Number 
,st.Plant_name 
,avg(st.Budget_Realization)
,(st.Actual_Volume)
,(st.Budget_Volume)
,sum(st.Gross_Production_Per_Hr)
,(pt.Production_Volume)
,(st.Actual_Sales_Dollars)
,CASE                    
WHEN coalesce (sum(st.Actual_Volume),0) = 0 
THEN 0
ELSE (sum(st.Actual_Sales_Dollars)/sum(st.Actual_Volume))     
END                     
,(st.PriMoAvgPrice)
,st.Sales_Date

from @Sales_TOTAL st
,@ProdTotal pt
Where st.plant_number = pt.plant_number

Group By st.plant_number, st.Plant_name, st.Sales_date,st.Actual_Volume,  pt.production_volume, st.budget_volume, st.actual_sales_dollars, st.PriMoAvgPrice
)

Select * from @Sales_Prod

1 个答案:

答案 0 :(得分:0)

所以解决方案是双管齐下的。一,在SSRS中,我对Plant_Name的分组丢失了。我弄乱了我的格式,必须删除它。

其次,我们将所有聚合移动到最终而不是整个。所以,用一把尺子打我的手,但这将是一个我不会忘记的教训。

谢谢大家!