-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgrouping-sets.sql
78 lines (75 loc) · 1.38 KB
/
grouping-sets.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
/*
Take a look at the source data
*/
select top(10) * from dbo.timesheet
go
/*
Return data for a dashboard
*/
select
project,
month(reported_on) as [year],
sum(hours_worked) as hours_worked
from
dbo.timesheet
where
reported_on between '20210101' and '20211231'
group by
grouping sets
(
(project),
(month(reported_on))
)
go
/*
Return data for a matrix report
*/
select
[project],
month([reported_on]) as [month],
sum([hours_worked]) as [hours_worked]
from
dbo.timesheet
where
reported_on between '20210101' and '20211231'
group by
grouping sets
(
([project], month([reported_on])),
([project]),
(month([reported_on])),
()
)
order by
[project],
[month]
go
/*
Return data for a matrix report in a pivoted format
*/
with reportData as
(
select
[project],
month([reported_on]) as [month],
sum([hours_worked]) as [hours_worked]
from
dbo.timesheet
where
reported_on between '20210101' and '20211231'
group by
grouping sets
(
([project], month([reported_on])),
([project]),
(month([reported_on])),
()
)
)
select
*
from
reportData
pivot
( sum(hours_worked) for [month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) ) as p
go