-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL query examples
More file actions
37 lines (29 loc) · 918 Bytes
/
SQL query examples
File metadata and controls
37 lines (29 loc) · 918 Bytes
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
Selecting employees from a certain period:
SELECT *
FROM employees
where employees.startdate <= '2008-07-01'
and (employees.enddate >= '2008-07-01'
OR
employees.enddate is NULL)
Employee counts by date key:
SELECT DATE_TRUNC(dates.datekey, 'month') as mt, COUNT(1)
FROM employees cross join dates
where dates.datekey >= employees.startdate
and (dates.datekey <= employees.enddate
OR
employees.enddate is NULL)
group by DATE_TRUNC(dates.datekey, 'month')
order by DATE_TRUNC(dates.datekey, 'month')
Employee count by end of date:
With `d1` as
(SELECT DISTINCT
DATE_ADD(DATE_ADD(DATE_TRUNC(dates.datekey, "month"), 1, "month" ), -1, "day") eomonth
FROM dates)
SELECT d1.eomonth, COUNT(1)
from employees cross join `d1` as d1
where d1.eomonth >= employees.startdate
and (d1.eomonth <= employees.enddate
OR
employees.enddate is NULL)
group by d1.eomonth
order by d1.eomonth