-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_leaves.sql
88 lines (78 loc) · 2.02 KB
/
script_leaves.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
SELECT
(CASE
WHEN tl.idleave = 1 THEN 'Sick Leave'
WHEN tl.idleave = 2 THEN 'Vacation Leave'
WHEN tl.idleave = 17 THEN 'Compensatory Leave'
ELSE 'N/A'
END) AS `leave_type`
,
tl.hrs AS `Unit Hours`,
IF(tl.hrs = 8, 1.0, 0.5) AS `Unit Days`,
(CASE tl.stat
WHEN 1 THEN 'Approved'
WHEN 2 THEN 'Disapproved'
WHEN 3 THEN 'Pending'
WHEN 4 THEN 'Cancelled'
ELSE
'Pending'
END)
AS `App Status`,
tl.`date` AS `App Date`,
(SELECT empid FROM vw_dataemployees WHERE id=tl.idacct) AS empid,
(SELECT CONCAT(fname,' ',lname) FROM tblaccount WHERE id = tl.idacct) AS Employee,
bu.name AS `Business Unit`,
tl.date_create,
tl.remarks,@temp:=tl.approver1 AS approver1,
IF(tl.approver1 LIKE '%,%',
(SELECT GROUP_CONCAT(sub.empname SEPARATOR '- ') AS `names`
FROM (SELECT i.empname FROM vw_dataemployees AS i WHERE FIND_IN_SET(i.id, @temp)) AS sub)
,
(SELECT CONCAT(fname,' ',lname) FROM tblaccount WHERE id = tl.approver1)
)
AS approver1_name
,
(CASE tl.approver1_stat
WHEN 1 THEN 'Approved'
WHEN 2 THEN 'Disapproved'
WHEN 3 THEN 'Pending'
WHEN 4 THEN 'Cancelled'
ELSE
'Pending'
END) AS `status1`
,
tl.approver2,
(SELECT CONCAT(fname,' ',lname) FROM tblaccount WHERE id = tl.approver2) AS approver2_name
,
(CASE tl.approver2_stat
WHEN 1 THEN 'Approved'
WHEN 2 THEN 'Disapproved'
WHEN 3 THEN 'Pending'
WHEN 4 THEN 'Cancelled'
ELSE
NULL
END) AS `status2`,
tl.approver3,
(SELECT CONCAT(fname,' ',lname) FROM tblaccount WHERE id = tl.approver3) AS approver3_name
,
(CASE tl.approver3_stat
WHEN 1 THEN 'Approved'
WHEN 2 THEN 'Disapproved'
WHEN 3 THEN 'Pending'
WHEN 4 THEN 'Cancelled'
ELSE
NULL
END) AS `status3`,
tl.approver4,
(SELECT CONCAT(fname,' ',lname) FROM tblaccount WHERE id = tl.approver4) AS approver4_name,
(CASE tl.approver4_stat
WHEN 1 THEN 'Approved'
WHEN 2 THEN 'Disapproved'
WHEN 3 THEN 'Pending'
WHEN 4 THEN 'Cancelled'
ELSE
NULL
END) AS `status4`
FROM tbltimeleaves AS tl
LEFT JOIN tblaccountjob aj ON aj.idacct = tl.idacct
LEFT JOIN tblbunits bu ON bu.id = aj.idunit
WHERE tl.stat = 3 ORDER BY tl.idleave,tl.idacct,bu.id