-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBike_script.sql
More file actions
65 lines (49 loc) · 1.4 KB
/
Bike_script.sql
File metadata and controls
65 lines (49 loc) · 1.4 KB
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
-- Table overview --
select * from bike_share_yr_0;
select * from bike_share_yr_1;
select * from cost_table;
-- Creating staged tables --
select *
into bike_share_yr1
from
bike_share_yr_0;
select *
into bike_share_yr2
from
bike_share_yr_1;
select *
into cost_table1
from
cost_table;
select * from bike_share_yr1;
select * from bike_share_yr2;
select * from cost_table1;
-- Stannderdizing data --
select convert(date, dteday, 103) as date
from bike_share_yr1;
update bike_share_yr1
set dteday = convert(date, dteday, 103);
select convert(date, dteday, 103) as date
from bike_share_yr2;
update bike_share_yr2
set dteday = convert(date, dteday, 103);
exec sp_rename 'bike_share_yr1.dteday', 'date', 'column';
exec sp_rename 'bike_share_yr2.dteday', 'date', 'column';
-- Dropping columns not needed --
alter table bike_share_yr1
drop column mnth, holiday, workingday, weathersit, temp, atemp, hum, windspeed;
select * from bike_share_yr1;
alter table bike_share_yr2
drop column mnth, holiday, workingday, weathersit, temp, atemp, hum, windspeed;
select * from bike_share_yr2;
-- Stacking tables, performimg join and aggregation --
with cte as
(select * from bike_share_yr1
union
select * from bike_share_yr2)
select *,
riders * price as revenue,
riders * price - COGS as profit
from cte as bk
left join cost_table1 as ct
on bk.yr = ct.yr;