SQL Operations
SQL Operations
1. Running the first command to retrieve the data on Big Query to get data.
4. The JSON path for the UberEATS JSON object contains a variable. Since JSON path query cannot be
dynamic, we will drill down on the node we require up until the variant.
CREATE TEMP FUNCTION EXTRACT_KV_PAIRS(json_str STRING)
RETURNS ARRAY<STRUCT<key STRING, value STRING>>
LANGUAGE js AS """
try{
const json_dict = JSON.parse(json_str);
const all_kv = Object.entries(json_dict).map(
(r)=>Object.fromEntries([["key", r[0]],["value",
JSON.stringify(r[1])]]));
return all_kv;
} catch(e) { return [{"key": "error","value": e}];}
""";
SELECT EXTRACT_KV_PAIRS(TO_JSON_STRING(response.data.menus)), slug from
`silicon-alpha-390017.loop_assignment.ubereats`;
5. Extracting the day of the week, opening, and closing times of the restaurants registered with
Ubereats.
WITH regularHours AS (
SELECT JSON_QUERY_ARRAY(ujk.f0_[SAFE_OFFSET(0)].value, '$.sections.0.regularHours')
AS regularHoursArray, ujk.slug, CONCAT(ue.b_name, ' ', ue.vb_name) AS name
FROM `ordinal-link-390505.dataset.ubereats_initial_3_var` AS ujk
JOIN `ordinal-link-390505.dataset.ubereats_initial_2` AS ue
ON ujk.slug = ue.slug
),
regularHoursUnnested AS (
SELECT
JSON_QUERY(regularHour, '$.daysBitArray') AS daysBitArray,
JSON_EXTRACT_SCALAR(regularHour, '$.endTime') AS endTime,
JSON_EXTRACT_SCALAR(regularHour, '$.startTime') AS startTime,
slug, name
FROM regularHours
CROSS JOIN UNNEST(regularHoursArray) AS regularHour
)
SELECT
(CASE
WHEN daysBitArray = '[true,false,false,false,false,false,false]' THEN 'Sunday'
WHEN daysBitArray = '[false,true,false,false,false,false,false]' THEN 'Monday'
WHEN daysBitArray = '[false,false,true,false,false,false,false]' THEN 'Tuesday'
WHEN daysBitArray = '[false,false,false,true,false,false,false]' THEN 'Wednesday'
WHEN daysBitArray = '[false,false,false,false,true,false,false]' THEN 'Thursday'
WHEN daysBitArray = '[false,false,false,false,false,true,false]' THEN 'Friday'
WHEN daysBitArray = '[false,false,false,false,false,false,true]' THEN 'Saturday'
ELSE 'Unknown'
END) AS dayOfWeek, startTime, endTime, name, slug
FROM regularHoursUnnested
ORDER BY name, (CASE dayOfWeek
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6
WHEN 'Sunday' THEN 7
ELSE 8 END)
6. Unnesting the day of the week column in the Grubhub table so it can be analyzed further.
SELECT
gh.slug AS Grubhub_Slug,gh.from_time, gh.to_time,
TIME_DIFF(gh.to_time, gh.from_time, MINUTE) +
(CASE
WHEN gh.to_time = TIME '00:00:00' THEN 1440
WHEN gh.from_time > TIME '12:00:00' AND gh.from_time > gh.to_time THEN 1440
WHEN gh.from_time > gh.to_time THEN 720
ELSE 0
END) AS GH_operating,
ue.slug AS Ubereats_Slug, ue.from_time, ue.to_time,
TIME_DIFF(ue.to_time, ue.from_time, MINUTE) +
(CASE
WHEN ue.to_time = TIME '00:00:00' THEN 1440
WHEN ue.from_time > TIME '12:00:00' AND ue.from_time > ue.to_time THEN 1440
WHEN ue.from_time > ue.to_time THEN 720
ELSE 0
END) AS UE_operating
FROM `ordinal-link-390505.dataset.ubereats_initial_5` AS ue
INNER JOIN `ordinal-link-390505.dataset.grubhub_initial_5` AS gh
ON ue.name = gh.name AND ue.dayOfWeek = gh.dayOfWeek
ORDER by ue.name;
10. Creating the final table in accordance with the desired output of the assignment.
Notes: -
Some businesses in the Uber Eats dataset had timings for days labelled ‘Unknown’. Such rows
were dropped automatically when Inner Joining. Similarly, some businesses had multiple
entries for the same days of the week. In this case, the first entry for the weekday was selected.