Project

General

Profile

Bug #1064 ยป Query Calculate OM Recap.txt

Query calculate OM Recap - ikawati basri, 04/18/2022 02:43 PM

 
1
DECLARE @last_success AS varchar(14) = '20111001000000' 
2
SELECT 
3
  TOP (1) @last_success = dt.LastAt 
4
FROM 
5
  PCMEPSVCLOGHEADER AS dt WITH (NOLOCK) 
6
WHERE 
7
  dt.SVCType = 'EMP_OM_RECAP' 
8
  AND dt.Status = 'S' 
9
ORDER BY 
10
  dt.LastAt DESC DECLARE @emptable AS TABLE (
11
    empID varchar(20), 
12
    posID varchar(20), 
13
    orgID varchar(20), 
14
    empStat varchar(20)
15
  ) INSERT INTO @emptable 
16
SELECT 
17
  md2.EmployeeID, 
18
  md2.Position, 
19
  Organization, 
20
  md2.EmployeeStatus 
21
FROM 
22
  PHRPA0002 AS md2 WITH (NOLOCK) 
23
WHERE 
24
  md2.StartDate <= @now8 
25
  AND md2.EndDate >= @now8 
26
  AND md2.ChangeDate >= @last_success 
27
  AND (
28
    (
29
      @object_from <> '' 
30
      AND @object_to <> '' 
31
      AND md2.EmployeeID >= @object_from 
32
      AND md2.EmployeeID <= @object_to
33
    ) 
34
    OR (
35
      @object_from = '' 
36
      AND @object_to = ''
37
    )
38
  ) DECLARE @i1 AS int = 1 DECLARE @countEmp AS int = 0 
39
SELECT 
40
  @countEmp = COUNT(*) 
41
FROM 
42
  @emptable 
43
WHERE 
44
  empStat = '04' 
45
  AND (
46
    posID <> '' 
47
    OR posID IS NOT NULL
48
  ) DECLARE @empResign AS TABLE (
49
    id int IDENTITY, 
50
    empID varchar(20)
51
  ) INSERT INTO @empResign 
52
SELECT 
53
  empID 
54
FROM 
55
  @emptable 
56
WHERE 
57
  empStat = '04' 
58
  AND (
59
    posID <> '' 
60
    OR posID IS NOT NULL
61
  ) WHILE (@i1 <= @countEmp) BEGIN DECLARE @emp_nya AS varchar(8) 
62
SELECT 
63
  @emp_nya = empID 
64
FROM 
65
  @empResign 
66
WHERE 
67
  id = @i1 
68
DELETE FROM 
69
  @emptable 
70
WHERE 
71
  empID = @emp_nya INSERT INTO @emptable 
72
SELECT 
73
  TOP(1) md2.EmployeeID, 
74
  md2.Position, 
75
  Organization, 
76
  md2.EmployeeStatus 
77
FROM 
78
  PHRPA0002 AS md2 WITH (NOLOCK) 
79
WHERE 
80
  md2.EmployeeID = @emp_nya 
81
  AND md2.EmployeeStatus <> '04' 
82
ORDER BY 
83
  md2.EndDate DESC 
84
SET 
85
  @i1 = @i1 + 1 END DECLARE @tableOrgID AS TABLE (
86
    id int IDENTITY, 
87
    org varchar(20)
88
  ) INSERT INTO @tableOrgID 
89
SELECT 
90
  RelationshipObject 
91
FROM 
92
  PHROM0002 AS rel WITH (NOLOCK) 
93
  INNER JOIN PHROM0001 AS tipe WITH (NOLOCK) ON rel.ObjectID = tipe.ObjectID 
94
  AND tipe.PositionHead = '1' 
95
WHERE 
96
  rel.ObjectClass = 'P' 
97
  AND rel.RelationshipClass = 'O' 
98
  AND rel.RelationshipType = '001' 
99
  AND rel.StartDate <= @now8 
100
  AND rel.EndDate >= @now8 
101
  AND rel.ObjectID IN (
102
    SELECT 
103
      posID 
104
    FROM 
105
      @emptable
106
  ) DECLARE @i AS int = 1 DECLARE @countOrg AS int = 0 
107
SELECT 
108
  @countOrg = COUNT(*) 
109
FROM 
110
  @tableOrgID WHILE (@i <= @countOrg) BEGIN DECLARE @org_nya AS varchar(8) 
111
SELECT 
112
  @org_nya = org 
113
FROM 
114
  @tableOrgID 
115
WHERE 
116
  id = @i DECLARE @t TABLE (
117
    organization varchar(20)
118
  ) INSERT INTO @t 
119
SELECT 
120
  @org_nya INSERT INTO @t 
121
SELECT 
122
  rel_obj 
123
FROM 
124
  dbo.GetObjectChildAll(@org_nya, 'O', 'O', @now8) INSERT INTO @emptable (empID) 
125
SELECT 
126
  DISTINCT om2pe.RelationshipObject AS EmployeeID 
127
FROM 
128
  PHROM0002 AS om2 
129
  LEFT JOIN PHROM0002 AS om2pe ON om2pe.ObjectID = om2.RelationshipObject 
130
  AND om2pe.ObjectClass = 'P' 
131
  AND om2pe.RelationshipClass = 'E' 
132
  AND om2pe.StartDate <= @now8 
133
  AND om2pe.EndDate >= @now8 
134
  LEFT JOIN dbo.PHROM0001 AS om1 ON om1.ObjectID = om2.ObjectID 
135
  AND om1.ObjectClass = 'O' 
136
  AND om1.StartDate <= @now8 
137
  AND om1.EndDate >= @now8 
138
  LEFT JOIN PHROM0002 AS om2pj ON om2pj.ObjectID = om2.RelationshipObject 
139
  AND om2pj.ObjectID = om2.RelationshipObject 
140
  AND om2pj.ObjectClass = 'P' 
141
  AND om2pj.RelationshipClass = 'J' 
142
  AND om2pj.StartDate <= @now8 
143
  AND om2pj.EndDate >= @now8 
144
  LEFT JOIN dbo.PHRPA0001 AS pa1 ON pa1.EmployeeID = om2pe.RelationshipObject 
145
  AND pa1.StartDate <= @now8 
146
  AND pa1.EndDate >= @now8 
147
WHERE 
148
  om2.ObjectID IN (
149
    SELECT 
150
      DISTINCT * 
151
    FROM 
152
      @t
153
  ) 
154
  AND om2.ObjectClass = 'O' 
155
  AND om2.RelationshipType = '001' 
156
  AND om2.RelationshipClass = 'P' 
157
  AND om2.StartDate <= @now8 
158
  AND om2.EndDate >= @now8 
159
  AND om2pe.RelationshipObject <> '' 
160
SET 
161
  @i = @i + 1 END 
162
SELECT 
163
  DISTINCT empID 
164
FROM 
165
  @emptable
    (1-1/1)