-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigquery.sql
155 lines (155 loc) · 5.02 KB
/
bigquery.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
SELECT
RANK() OVER (ORDER BY FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_) ASC) AS data_id,
FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_) as Date,
'' as calendar_enabled,
'' as remote_start_enabled,
1 as vehicle_id,
'MY_DISPLAY_NAME' as display_name,
'' as color,
'' as backseat_token,
'' as notifications_enabled,
'MY_VIN' as vin,
'' as backseat_token_updated_at,
'' as id,
'0' as tokens,
'' as id_s,
if(max(d.Latitude) is not null, 'online', 'offline') as state,
max(c.Charger_Phases) as user_charge_enable_request,
'' as time_to_full_charge,
max(c.Charger_Current__A_) as charge_current_request,
'' as charge_enable_request,
'' as charge_to_max_range,
max(c.Charger_Phases) as charger_phases,
0 as battery_heater_on,
0 as managed_charging_start_time,
max(c.Ideal_Battery_Range__mi_) as battery_range,
max(c.Charger_Power__kW_) as charger_power,
90 as charge_limit_soc,
'' as charger_pilot_current,
IF(max(c.Charging_State) = 'Charging', 'Engaged', '') as charge_port_latch,
'' as battery_current,
'' as charger_actual_current,
'' as scheduled_charging_pending,
'' as fast_charger_type,
'' as usable_battery_level,
'' as motorized_charge_port,
'' as charge_limit_soc_std,
'' as not_enough_power_to_heat,
max(c.Battery_Level____) as battery_level,
max(c.Battery_Level____) - LAST_VALUE(max(c.Battery_Level____))
OVER (ORDER BY max(c.Battery_Level____) ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS charge_energy_added,
IF(max(c.Charging_State) = 'Charging', 1, 0) as charge_port_door_open,
'' as max_range_charge_counter,
'' as charge_limit_soc_max,
max(c.Ideal_Battery_Range__mi_) as ideal_battery_range,
'' as managed_charging_active,
max(c.Charging_State) as charging_state,
'' as fast_charger_present,
'' as trip_charging,
'' as managed_charging_user_canceled,
'' as scheduled_charging_start_time,
'' as est_battery_range,
max(c.Charge_Rate__mph_) as charge_rate,
max(c.Charger_Voltage__V_) as charger_voltage,
'' as charge_current_request_max,
1 as eu_vehicle,
'' as charge_miles_added_ideal,
'' as charge_limit_soc_min,
'' as charge_miles_added_rated,
max(clim.Inside_Temp____C_) as inside_temp,
max(d.Longitude) as longitude,
'' as heading,
'' as gps_as_of,
max(d.Latitude) as latitude,
max(d.Speed__mph_) as speed,
max(d.Shift_State) as shift_state,
'' as seat_heater_rear_right,
'' as seat_heater_rear_left_back,
'' as seat_heater_left,
'' as passenger_temp_setting,
'' as is_auto_conditioning_on,
'' as driver_temp_setting,
max(clim.Outside_Temp____C_) as outside_temp,
'' as seat_heater_rear_center,
'' as is_rear_defroster_on,
'' as seat_heater_rear_right_back,
'' as smart_preconditioning,
'' as seat_heater_right,
'' as fan_status,
'' as is_front_defroster_on,
'' as seat_heater_rear_left,
'' as gui_charge_rate_units,
'' as gui_24_hour_time,
'' as gui_temperature_units,
'' as gui_range_display,
'' as gui_distance_units,
'' as sun_roof_installed,
'' as rhd,
'' as remote_start_supported,
'' as homelink_nearby,
'' as parsed_calendar_supported,
'' as spoiler_type,
'' as ft,
max(d.Odometer__mi_) as odometer,
'' as remote_start,
'' as pr,
'' as has_spoiler,
'' as roof_color,
'' as perf_config,
'' as valet_mode,
'' as calendar_supported,
'' as pf,
'' as sun_roof_percent_open,
'' as third_row_seats,
'' as seat_type,
'' as api_version,
'' as rear_seat_heaters,
'' as rt,
'' as exterior_color,
'' as df,
'' as autopark_state,
'' as sun_roof_state,
'' as notifications_supported,
'' as vehicle_name,
'' as dr,
'' as autopark_style,
'' as car_type,
'' as wheel_type,
max(v.Locked) as locked,
'' as center_display_state,
'' as last_autopark_error,
'2024.2.6 1eb2c4e079b' as car_version,
'' as dark_rims,
'' as autopark_state_v2,
'' as inside_tempF,
'' as driver_temp_settingF,
'' as outside_tempF,
'' as odometerF,
0 as idleNumber,
0 as sleepNumber,
0 as driveNumber,
0 as chargeNumber,
'' as polling,
0 as idleTime,
0 as running,
0 as rerunning,
'' as maxRange,
'' as left_temp_direction,
'' as max_avail_temp,
max(clim.Climate_Enabled) as is_climate_on,
'' as right_temp_direction,
'' as min_avail_temp,
'' as rear_seat_type,
max(d.Power__kW_) as power,
'' as steering_wheel_heater,
'' as wiper_blade_heater,
'' as side_mirror_heaters,
'' as elevation
FROM `MY_PROJECT.MY_VIN.charging_states` c
LEFT JOIN `MY_PROJECT.MY_VIN.vehicle_states` v ON FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', v.Timestamp__UTC_)= FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)
LEFT JOIN `MY_PROJECT.MY_VIN.climate_states` clim ON FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', clim.Timestamp__UTC_)= FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)
LEFT JOIN `MY_PROJECT.MY_VIN.driving_states` d ON FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', d.Timestamp__UTC_)= FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)
/*LEFT JOIN `MY_PROJECT.MY_VIN.battery_states` b ON FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', b.Timestamp__UTC_)= FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)*/
WHERE FORMAT_TIMESTAMP('%Y-%m', c.Timestamp__UTC_) = 'MY_MONTH'
GROUP BY c.Timestamp__UTC_, FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)
order by FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:00', c.Timestamp__UTC_)