Project

General

Profile

Bug #1952 » CBIATMTOPYHINOTARIF.sql

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

 
1
??--USE [MinovaES_BIA_Dev]
2

3
--GO
4

5
--/****** Object:  StoredProcedure [dbo].[CBIATMTOPYHINOTARIF]    Script Date: 24/01/2023 11.16.34 ******/
6

7
--SET ANSI_NULLS ON
8

9
--GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[CBIATMTOPYHINOTARIF]
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) = '14060255'
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
	,QtyTrip1 DECIMAL(22,0)
46

47
	,QtyTrip2 DECIMAL(22,0)
48

49
	,QtyTrip3 DECIMAL(22,0)
50

51
)
52

53

54

55
INSERT INTO @CTARIF
56

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

59
PIVOT (MAX(Tarif)
60

61
FOR FieldName IN
62

63
(	[QtyTrip1],
64

65
	[QtyTrip2],
66

67
	[QtyTrip3])
68

69
) AS pvt
70

71

72

73

74

75
DECLARE @TblResult TABLE
76

77
(
78

79
	EmployeeID VARCHAR(20)
80

81
	,QtyTrip1 VARCHAR(20)
82

83
	,QtyTrip2 VARCHAR(20)
84

85
	,QtyTrip3 VARCHAR(20)
86

87
	,TarifQtyTrip1 DECIMAL(22,0)
88

89
	,TarifQtyTrip2 DECIMAL(22,0)
90

91
	,TarifQtyTrip3 DECIMAL(22,0)
92

93
)
94

95

96

97
INSERT INTO @TblResult
98

99
SELECT DISTINCT md.EmployeeID, md.QtyTrip1,  md.QtyTrip2,  md.QtyTrip3 
100

101
				,md.QtyTrip1 * tarif.QtyTrip1 AS TarifQtyTrip1
102

103
				,md.QtyTrip2 * tarif.QtyTrip2 AS TarifQtyTrip2
104

105
				,md.QtyTrip3 * tarif.QtyTrip3 AS TarifQtyTrip3
106

107
FROM dbo.CUSPYDATAPREMIHINO AS md 
108

109
LEFT JOIN @CTARIF AS tarif
110

111
	ON md.EmployeeID = tarif.EmployeeID
112

113
WHERE  md.EmployeeID = @EmployeeID
114

115
	AND (md.StartDate BETWEEN @StartDate AND @EndDate)
116

117

118

119
SELECT DISTINCT EmployeeID
120

121
				,SUM(TarifQtyTrip1) AS TarifQtyTrip1
122

123
				,SUM(TarifQtyTrip2) AS TarifQtyTrip2
124

125
				,SUM(TarifQtyTrip3) AS TarifQtyTrip3
126

127
FROM @TblResult
128

129
GROUP BY EmployeeID
(3-3/5)