Project

General

Profile

Bug #1952 » CBIATMTOPYBINAMARGATARIF.sql

Tri Rizqiaty, 01/24/2023 02:41 PM

 
1
??USE [MinovaES_BIA_Dev]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[CBIATMTOPYBINAMARGATARIF]    Script Date: 24/01/2023 11.14.32 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[CBIATMTOPYBINAMARGATARIF]
16

17
(
18

19
	@EmployeeID VARCHAR(20)
20

21
	,@StartDate VARCHAR(20)
22

23
	,@EndDate VARCHAR(20)
24

25
)
26

27
AS
28

29

30

31
--DECLARE @EmployeeID VARCHAR(20) = '12100109'
32

33
--DECLARE @StartDate VARCHAR(20) = '20230101'
34

35
--DECLARE @EndDate VARCHAR(20) = '20230131'
36

37

38

39
DECLARE @CTARIF TABLE
40

41
(
42

43
	EmployeeID VARCHAR(20)
44

45
	,PremiHelper DECIMAL(22,2)
46

47
	,UangMakan DECIMAL(22,2)
48

49
	,PremiOperator DECIMAL(22,2)
50

51
)
52

53

54

55
INSERT INTO @CTARIF
56

57
SELECT @EmployeeID, * FROM (SELECT FieldName, Tarif FROM CTARIFBINAMARGA) AS SourceTable
58

59
PIVOT (MAX(Tarif)
60

61
FOR FieldName IN
62

63
(	[PremiHelper],
64

65
	[UangMakan],
66

67
	[PremiOperator])
68

69
) AS pvt
70

71

72

73
DECLARE @TblResult TABLE
74

75
(
76

77
	EmployeeID VARCHAR(20)
78

79
	,PremiHelper VARCHAR(20)
80

81
	,UangMakan VARCHAR(20)
82

83
	,PremiOperator VARCHAR(20)
84

85
	,TarifPremiHelper DECIMAL(22,0)
86

87
	,TarifUangMakan DECIMAL(22,0)
88

89
	,TarifPremiOperator DECIMAL(22,0)
90

91
)
92

93

94

95
INSERT INTO @TblResult
96

97
SELECT DISTINCT md.EmployeeID, md.PremiHelper,  md.UangMakan,  md.PremiOperator 
98

99
				,CONVERT(DECIMAL(22,2),CASE WHEN md.PremiHelper <> '' THEN md.PremiHelper ELSE '0' END) * tarif.PremiHelper AS TarifPremiHelper
100

101
				,CONVERT(DECIMAL(22,2),CASE WHEN md.UangMakan <> '' THEN md.UangMakan ELSE '0' END) * tarif.UangMakan AS TarifUangMakan
102

103
				,CONVERT(DECIMAL(22,2),CASE WHEN md.PremiOperator <> '' THEN md.PremiOperator ELSE '0' END) * tarif.PremiOperator AS TarifPremiOperator
104

105
FROM CUSPYDATAPREMIBINAMARGA AS md 
106

107
LEFT JOIN @CTARIF AS tarif
108

109
	ON md.EmployeeID = tarif.EmployeeID
110

111
WHERE  md.EmployeeID = @EmployeeID
112

113
	AND (md.StartDate BETWEEN @StartDate AND @EndDate)
114

115

116

117
SELECT DISTINCT EmployeeID
118

119
				,SUM(TarifPremiHelper) AS TarifPremiHelper
120

121
				,SUM(TarifUangMakan) AS TarifUangMakan
122

123
				,SUM(TarifPremiOperator) AS TarifPremiOperator
124

125
FROM @TblResult
126

127
GROUP BY EmployeeID
(1-1/5)