0% found this document useful (0 votes)
5 views3 pages

Scratch Work

The document contains SQL queries and data transformation logic to analyze customer ordering patterns. It includes a query to identify customers who only order at the start of the month and various transformations to extract and calculate fields related to order delivery times and tips. The transformations involve handling datetime differences and calculating percentages for tips and discounts based on order data.

Uploaded by

badnafri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Scratch Work

The document contains SQL queries and data transformation logic to analyze customer ordering patterns. It includes a query to identify customers who only order at the start of the month and various transformations to extract and calculate fields related to order delivery times and tips. The transformations involve handling datetime differences and calculating percentages for tips and discounts based on order data.

Uploaded by

badnafri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Queries:

Query to find Number of Customers that only Order at the Start of the Month: (Scratch
Work for Figure 10 in the Assignment)

/* This first piece of code is to find the consumers who have only ordered in the start of the mont
*/
WITH temp AS (SELECT
​ ​ "Consumer ID",
​ ​ count("day_Customer placed order datetime1") as NoOfOrders
FROM "Sample Data"
/* Sample Data is the name of the data set */
WHERE​ "Consumer ID" not in
​ (
​ SELECT "Consumer ID"
​ FROM "Sample Data"
​ WHERE​ "day_Customer placed order datetime1" > 5
​ )
GROUP BY "Consumer ID")

SELECT
​ ​ s."Consumer ID" as 'Consumer ID',
​ ​ t.NoOfOrders as 'Number of Orders',
​ ​ MAX(s."day_Customer placed order datetime1") as 'Day of Last Order'
FROM "Sample Data" s
INNER JOIN temp t ON s."Consumer ID" = t."Consumer ID"
Where t.NoOfOrders > 2
/* The above where clause is just to select some of the highest ordering consumers */
GROUP BY 1,
​ 2
Order By t.NoOfOrders DESC
Data Transformations:

Transformations to edit fields and add new fields into the table

1)​ Hour field extracted from Customer placed order datetime and converted into PST
Hour(add_hour( "Customer placed order datetime", -8))

2)​ Total Order Delivery Time created using logic as shown:

if( datetime_diff(minute, "Customer placed order datetime","Delivered to


consumer datetime") < 0, 44640 + datetime_diff(minute, "Customer placed order
datetime","Delivered to consumer datetime"), datetime_diff(minute, "Customer
placed order datetime","Delivered to consumer datetime"))

Note: if clause is to handle cases where the times are across calendar days

3)​ Minutes Taken for Restaurant to Receive the Order created using logic as shown:

if(datetime_diff(minute, "Customer placed order datetime","Placed order with


restaurant datetime") < 0, 44640+datetime_diff(minute, "Customer placed order
datetime","Placed order with restaurant datetime"),datetime_diff(minute,
"Customer placed order datetime","Placed order with restaurant datetime"))

4)​ Minutes taken for Driver to arrive


if(datetime_diff(minute, "Placed order with restaurant datetime","Driver at
restaurant datetime") < 0 44640 + datetime_diff(minute, "Placed order with
restaurant datetime","Driver at restaurant datetime"),datetime_diff(minute,
"Placed order with restaurant datetime","Driver at restaurant datetime"))

5)​ TIme for Driver to Deliver created using logic as shown:

iif( datetime_diff(minute,"Driver at restaurant datetime", "Delivered to consumer


datetime")) < 0, 44640 + datetime_diff(minute,"Driver at restaurant datetime",
"Delivered to consumer datetime"), )

6)​ Tip % created using logic as show:

​ ​ (sum("Sample Data"."Amount of tip")/sum("Sample Data"."Order total"))*100

7)​ Discount % created using logic as shown:



(sum("Sample Data"."Amount of discount")/sum("Sample Data"."Order
total"))*100

You might also like