Мой запрос работает, но когда я помещаю его в SSRS, он возвращает данные за все дни и для всех заводов (что должно быть):
Мои столбцы 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