-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetrack_init.sql
177 lines (162 loc) · 3.35 KB
/
timetrack_init.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
CREATE TABLE
IF NOT EXISTS "type"(
"id" INTEGER PRIMARY KEY,
"status" TEXT UNIQUE NOT NULL,
"work" INTEGER NOT NULL,
"comment" TEXT
);
CREATE TABLE
IF NOT EXISTS "entries"(
"id" INTEGER PRIMARY KEY,
"date" DATETIMENOT NULL,
"type_id" INTEGER NOT NULL,
"user_id" INTEGER NOT NULL,
"comment" TEXT,
FOREIGN KEY("type_id") REFERENCES "type"("id"),
FOREIGN KEY("user_id") REFERENCES "users"("id")
);
CREATE TABLE
IF NOT EXISTS "users"(
"id" INTEGER PRIMARY KEY,
"stampkey" TEXT NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT UNIQUE NOT NULL,
"position" TEXT,
"department_id" INTEGER,
FOREIGN KEY("department_id") REFERENCES "departments"("id")
);
CREATE TABLE
IF NOT EXISTS "departments"(
"id" INTEGER PRIMARY KEY,
"name" TEXT UNIQUE NOT NULL
);
CREATE VIEW
IF NOT EXISTS "work_hours" AS WITH work_intervals AS(
SELECT
u.name AS user_name,
t.status,
t.work,
DATETIME(e.date) AS start_time,
IFNULL(
(
SELECT
DATETIME(next_e.date)
FROM
entries AS next_e
WHERE
next_e.user_id = e.user_id
AND DATETIME(next_e.date) > DATETIME(e.date)
ORDER BY
DATETIME(next_e.date) ASC
LIMIT 0,
1
),
DATETIME('now')
) AS end_time
FROM
entries AS e
JOIN users AS u ON u.id = e.user_id
JOIN type AS t ON t.id = e.type_id
ORDER BY
DATETIME(e.date) ASC
) SELECT
user_name,
DATE(start_time) AS work_date,
ROUND(
SUM(
(
JULIANDAY(end_time) - JULIANDAY(start_time)
) * 24 * work
),
2
) AS work_hours
FROM
work_intervals
GROUP BY
user_name,
DATE(start_time);
CREATE VIEW
IF NOT EXISTS "current_status" AS SELECT
users.id AS user_id,
users.name AS user_name,
type.id AS type_id,
type.status AS status,
entries.date AS date
FROM
entries
JOIN(
SELECT
user_id,
MAX(date) AS latest_date
FROM
entries
GROUP BY
user_id
) AS latest_entry ON entries.user_id = latest_entry.user_id
AND entries.date = latest_entry.latest_date
JOIN users ON users.id = entries.user_id
JOIN type ON type.id = entries.type_id;
CREATE VIEW
IF NOT EXISTS "work_hours_with_type" AS WITH work_intervals AS (
SELECT
d.name AS department,
u.name AS user_name,
st.status as type,
st.work,
DATETIME(we.date) AS start_time,
IFNULL(
(
SELECT
DATETIME(next_we.date)
FROM
entries AS next_we
WHERE
next_we.user_id = we.user_id
AND DATETIME(next_we.date) > DATETIME(we.date)
ORDER BY
DATETIME(next_we.date) ASC
LIMIT 0, 1
),
DATETIME('now')
) AS end_time
FROM
entries AS we
JOIN
users AS u ON u.id = we.user_id
JOIN
departments AS d ON d.id = u.department_id
JOIN
type AS st ON st.id = we.type_id
ORDER BY
DATETIME(we.date) ASC
)
SELECT
department,
user_name,
type,
start_time,
ROUND(SUM((JULIANDAY(end_time) - JULIANDAY(start_time)) * 24), 2) AS work_hours
FROM
work_intervals
WHERE
work = 1
GROUP BY
department,
user_name,
type,
start_time;
CREATE VIEW
IF NOT EXISTS "entries_view" AS
SELECT
entries.id,
entries.date as Date,
type.status as StatusType,
type.work as isWork,
type.comment as typeComment,
users.name as UserName,
users.email as eMail,
departments.name as Department
FROM entries
LEFT JOIN type ON entries.type_id = type.id
LEFT JOIN users ON entries.user_id = users.id
LEFT JOIN departments ON users.department_id = departments.id;