My query is working but when I put it into SSRS it returns data for all the days and for all the plants (which it should):

My WTD columns work great but as you can see my MTD is displaying every day for every plant and I want to have it aggregated just like my WTD. I am going to apologize now but this query is long. And since I’m not sure where the problem lies, I feel it’s better to post the whole query rather than have my hand slapped for not providing enough info up front. Thanks in advance!
My boss assembled the temp tables (I’m still learning where the data is located) because one of the plants has two different processes that needed to be brought together. Hence the reason it looks messy (and is probably the reason it takes 3 minutes to run). I do apologize and thank you for your time and constructive criticism.
P.S. I’m not sure if this is a query problem or an SSRS formatting problem
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
So the solution was two pronged. One, in SSRS my grouping on Plant_Name was missing. I was messing with my formatting and must have removed it.
Secondly, we moved all of the aggregation to the end instead of throughout. So, got my hand slapped with a ruler but it’ll be a lesson I won’t forget.
Thanks everyone!