-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEquipment_Material_Analysis.sql
66 lines (58 loc) · 2.31 KB
/
Equipment_Material_Analysis.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
USE [EquipmentAnalyssis]
GO
Create View MiningReport with Schemabinding as
WITH RESULT AS
(
Select round(sum(H.[Load Count]),2) as [Number of Loads], round(sum(H.Tonnes),2) as [Total Tonnes], H.[EQUIPID],
CAST(D.[Date] AS DATE) As Date
from [dbo].[Fact_Hauling] H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
group by [Date], H.[EQUIPID]
)
,
AvgTonnesperLoad
AS
(
select EquipID,D.Date , (sum(Tonnes)/sum([Load Count])) as TonnesperLoad from dbo.Fact_Hauling H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
group by D.Date, EQUIPID
),
TotalOreTonnes AS
(
Select [EQUIPID],D.Date ,round(sum([Tonnes]),2) as "Total Ore Tonnes" from [dbo].[Fact_Hauling] H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
where [Ore / Waste] = 'Ore'
group by D.Date, [EQUIPID]
),
TotalOreLoad AS
(
Select [EQUIPID],D.Date ,round(sum([Load Count]),2) as "Total Ore Loads" from [dbo].[Fact_Hauling] H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
where [Ore / Waste] = 'Ore'
group by D.Date, [EQUIPID]
),
TotalWasteLoad AS
(
Select D.Date, [EQUIPID], round(sum([Load Count]),2) as [Total Waste Loads] from [dbo].[Fact_Hauling] H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
where [Ore / Waste] = 'Waste'
group by D.Date, [EQUIPID]
)
,
TotalWasteTonnes AS
(
Select D.Date, [EQUIPID], round(sum([Tonnes]),2) as [Total Waste Tonnes] from [dbo].[Fact_Hauling] H
inner join [dbo].[dim_Date] D on H.[Created Date_datekey] = D.DateId
where [Ore / Waste] = 'Waste'
group by D.Date, [EQUIPID]
)
Select R.Date as [Haul Date], E.[Equipment], E.[Equipment Type], E.Description, R.[Total Tonnes], O.[Total Ore Tonnes], W.[Total Waste Tonnes],
(
select AVG(TonnesperLoad) from AvgTonnesperLoad a Where a.EQUIPID =R.EQUIPID and R.Date=a.Date group by a.Date,a.EQUIPID
) as AveragePerLoad , R.[Number of Loads], ol.[Total Ore Loads], wl.[Total Waste Loads]
from Result R
INNER JOIN TotalOreTonnes O ON R.EQUIPID = O. EQUIPID and R.Date=O.Date
INNER JOIN TotalWasteTonnes W ON R. EQUIPID = W.EQUIPID and R.Date=W.Date
INNER JOIN [dbo].[Dim_Equipment] E ON E.[EQUIPID] = R.EQUIPID
INNER JOIN TotalOreLoad ol on ol.EQUIPID=R.EQUIPID and ol.Date=r.Date
INNER JOIN TotalWasteLoad wl on wl.EQUIPID=R.EQUIPID and wl.Date=r.Date