-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bellabeat Data Analysis.sql
65 lines (51 loc) · 2.1 KB
/
Bellabeat Data Analysis.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
SELECT * FROM dailyActivity_merged$
SELECT * FROM sleepDay_merged$
-- Counting distinct Id count from each tables used.
SELECT COUNT(DISTINCT Id) FROM dailyActivity_merged$ --33
SELECT COUNT(DISTINCT Id) FROM sleepDay_merged$ --24
-- Converting Date to a better format in dailyActivity_merged$
ALTER TABLE dailyActivity_merged$
ADD NewActivityDate Date;
UPDATE dailyActivity_merged$
SET NewActivityDate = CONVERT(Date,ActivityDate)
-- Adding Day of the Week Name column in dailyActivity_merged$
ALTER TABLE dailyActivity_merged$
ADD DayWeek char(10);
UPDATE dailyActivity_merged$
SET DayWeek = DATENAME(dw, NewActivityDate)
-- Converting Date to a better format in sleepDay_merged$
ALTER TABLE sleepDay_merged$
ADD NewSleepDay Date;
UPDATE sleepDay_merged$
SET NewSleepDay = CONVERT(Date,SleepDay)
-- Adding Day of the Week Name column in sleepDay_merged$
ALTER TABLE sleepDay_merged$
ADD DayWeek char(10);
UPDATE sleepDay_merged$
SET DayWeek = DATENAME(dw, SleepDay)
-- Sum of activity leves for each (distinct) ids.
SELECT DISTINCT Id,
SUM(SedentaryMinutes) as Sedentary,
SUM(LightlyActiveMinutes) as LightlyActive,
SUM(FairlyActiveMinutes) as FairlyActive,
SUM(VeryActiveMinutes) as VeryActive
INTO NewSUMAct
FROM dailyActivity_merged$
GROUP BY Id
-- Combining dailyActivity_merged$ and sleepDay_merged$ into a new table
SELECT Activity.*, Sleep.TotalSleepRecords, Sleep.TotalMinutesAsleep, Sleep.TotalTimeInBed
INTO ActivitySleep
FROM dailyActivity_merged$ Activity
INNER JOIN sleepDay_merged$ Sleep
ON Activity.Id = Sleep.Id AND Activity.NewActivityDate = Sleep.NewSleepDay
-- Adding Total Active Minutes column
SELECT *,
(VeryActiveMinutes+FairlyActiveMinutes+LightlyActiveMinutes) TotalActiveMinutes,
(VeryActiveDistance+ModeratelyActiveDistance+LightActiveDistance) TotalActiveDistance
INTO CompleteData
FROM ActivitySleep
SELECT *,
(VeryActiveMinutes+FairlyActiveMinutes+LightlyActiveMinutes) TrueTotalActiveMinutes,
(VeryActiveDistance+ModeratelyActiveDistance+LightActiveDistance) TrueTotalActiveDistance
INTO TrueDailyActivity
FROM dailyActivity_merged$