Project

General

Profile

Feature #1860 » CBIACLOSINGBJR_20221209.sql

Tri Rizqiaty, 12/09/2022 10:16 AM

 
1
??/****** Object:  StoredProcedure [dbo].[CBIACLOSINGBJR]    Script Date: 09/12/2022 10.03.48 ******/
2

3
SET ANSI_NULLS ON
4

5
GO
6

7
SET QUOTED_IDENTIFIER ON
8

9
GO
10

11
ALTER PROCEDURE [dbo].[CBIACLOSINGBJR]
12

13
AS
14

15

16

17
DECLARE @Now VARCHAR(20) = CONVERT(VARCHAR, GETDATE(), 112) 
18

19
DECLARE @Month VARCHAR(20) = CONVERT(VARCHAR, MONTH(GETDATE()), 112) 
20

21
DECLARE @Yesterday VARCHAR(20) = CONVERT(VARCHAR,DATEADD(day, -1, CAST(GETDATE() AS date)),112)
22

23
DECLARE @YesterdayDate VARCHAR(20) = CONVERT(VARCHAR,DAY(DATEADD(day, -1, CAST(GETDATE() AS date))),112)
24

25
DECLARE @StartDateLast VARCHAR(20) = CONVERT(VARCHAR,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0), 112)
26

27
DECLARE @EndDateLast VARCHAR(20) = CONVERT(VARCHAR, DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)), 112)
28

29
DECLARE @StartDate VARCHAR(20) = CONVERT(VARCHAR,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0), 112)
30

31
DECLARE @EndDate VARCHAR(20) = CONVERT(VARCHAR,dateadd(ms, -3, dateadd(mm, datediff(mm, 0, getdate()) + 1, 0)), 112)
32

33
------ YesterdayDate = hari terakhir data masuk yg akan dihitung berdasarkan createdate, harus minus 1 dari now (tanggal berjalannya job)
34

35
------ job dijalankan daily setiap jam 1 malam
36

37

38

39
IF(@YesterdayDate = (SELECT DISTINCT Value1 FROM dbo.PCMEPGENPARAM WHERE Parameter = 'CBIACLOSINGBJRDATE')) 
40

41
BEGIN
42

43

44

45
	DECLARE @TblMD TABLE
46

47
	(
48

49
		BlockID VARCHAR(20)
50

51
		,AreaHA VARCHAR(20)
52

53
		,TBS DECIMAL(22,2)
54

55
	)
56

57
	INSERT INTO @TblMD
58

59
	SELECT md.BlockID, md.AreaHA, SUM(md.TBS)
60

61
	FROM dbo.CUSPYPREMIPANEN AS md
62

63
	WHERE ((LEFT(md.ChangeDate,8) BETWEEN @StartDateLast AND @Yesterday))
64

65
		AND (md.StartDate BETWEEN @StartDateLast AND @EndDateLast)
66

67
	GROUP BY md.BlockID, md.AreaHA
68

69

70

71
	INSERT INTO CUSPERIODBJR
72

73
	SELECT @StartDate AS SatartDate
74

75
			,@EndDate AS EndDate
76

77
			,LEFT(@Now,4) AS [Year]
78

79
			,tblmd.BlockID
80

81
			,@Month AS [Month]
82

83
			,CONVERT(DECIMAL(18,2),(tblmd.TBS/CONVERT(DECIMAL(22,2),blk.AreaHAmeter)) * 300000) AS BJR
84

85
			,'Service' AS CreateBy
86

87
			,REPLACE(CONVERT(varchar, GETDATE(),101),'/','') + REPLACE(CONVERT(varchar, GETDATE(),108),':','') AS CreateDate
88

89
			,'Service' AS ChangeBy
90

91
			,REPLACE(CONVERT(varchar, GETDATE(),101),'/','') + REPLACE(CONVERT(varchar, GETDATE(),108),':','') AS ChangeDate
92

93
	FROM @TblMD AS tblmd
94

95
	LEFT JOIN dbo.CUSMDBLOCK AS blk
96

97
		ON tblmd.BlockID = blk.BlockID
98

99
END
100

101

(1-1/6)