-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDailyAnalysis.sql
137 lines (83 loc) · 3.15 KB
/
DailyAnalysis.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
/*Checking Number of Rows on dailyActivities*/
Select Count(*)
From [dbo].[dailyActivity_merged]
/*Checking for duplicates in dailyActivity dataset*/
Select Id, ActivityDate, TotalSteps, Count(*)
From [dbo].[dailyActivity_merged]
group by id, ActivityDate, TotalSteps
Having Count(*) > 1
/*Modify date format for better understaning in sleepDay*/
Update sleepDay_merged
Set SleepDay = Convert(date, SleepDay, 101)
/*Modify date format for better understaning in dailyActivity*/
Update dailyActivity_merged
Set ActivityDate = Convert(date, ActivityDate, 101)
/*Add day_0f_week column on dailyActivities*/
Alter Table [dbo].[dailyActivity_merged]
ADD day_of_week nvarchar(50)
/*Extract datename from ActivityDate*/
Update dailyActivity_merged
SET day_of_week = DATENAME(DW, ActivityDate)
/*Add sleep data columns on dailyActivity table*/
Alter Table [dbo].[dailyActivity_merged]
ADD total_mins_sleep int,
total_mins_bed int
/*Add sleep records into dailyActivity table*/
UPDATE dailyActivity_merged
Set total_mins_sleep = t2.TotalMinutesAsleep,
total_mins_bed = t2.TotalTimeInBed
From [dbo].[dailyActivity_merged] as t1
Full Outer Join sleepDay_merged as t2
on t1.id = t2.id and t1.ActivityDate = t2.SleepDay
--------------------------------------------------------------------------
--Analysis--
--Daily Sum Analysis - No trends/patterns found
Select SUM(TotalSteps) as total_steps,
SUM(TotalDistance) as total_dist,
SUM(Calories) as total_calories,
day_of_week
From [dbo].[dailyActivity_merged]
Group By day_of_week
--Daily Average analysis - No trends/patterns found
Select AVG(TotalSteps) as avg_steps,
AVG(TotalDistance) as avg_dist,
AVG(Calories) as avg_calories,
day_of_week
From [dbo].[dailyActivity_merged]
Group By day_of_week
--Activities and colories comparison
Select Id,
SUM(TotalSteps) as total_steps,
SUM(VeryActiveMinutes) as total_Vactive_mins,
Sum(FairlyActiveMinutes) as total_Factive_mins,
SUM(LightlyActiveMinutes) as total_Lactive_mins,
SUM(Calories) as total_calories
From [dbo].[dailyActivity_merged]
Group By Id
--Average Sleep Time per user
Select Id, Avg(TotalMinutesAsleep)/60 as avg_sleep_time_h,
Avg(TotalTimeInBed)/60 as avg_time_bed_h,
AVG(TotalTimeInBed - TotalMinutesAsleep) as wasted_bed_time_m
from sleepDay_merged
Group by Id
--Sleep and calories comparison
Select t1.Id, SUM(TotalMinutesAsleep) as total_sleep_m,
SUM(TotalTimeInBed) as total_time_inbed_m,
SUM(Calories) as calories
From [dbo].[dailyActivity_merged] as t1
Inner Join [dbo].[sleepDay_merged] as t2
ON t1.Id = t2.Id and t1.ActivityDate = t2.SleepDay
Group By t1.Id
Select *
From [dbo].[dailyActivity_merged]
Select Id, count(Id) as coun
From sleepDay_merged
Group by Id
--Time Expenditure per day
Select Distinct Id, SUM(SedentaryMinutes) as sedentary_mins,
SUM(LightlyActiveMinutes) as lightly_mins,
SUM(FairlyActiveMinutes) as fairlyactive_mins,
SUM(VeryActiveMinutes) as veryactive_mins
From [dbo].[dailyActivity_merged]
where total_mins_bed IS NOT NULL
Group by Id