Date and Time
Date and Time
SELECT
market_start_datetime,
DATE_ADD(market_start_datetime, INTERVAL 150 MINUTE),
DATE_SUB(market_start_datetime, INTERVAL 150 MINUTE)
FROM `farmers_market.datetime_demo`
Today is 17th July, find out how many orders were placed in the last 30 days.
SELECT
COUNT(DISTINCT order_id)
FROM orders
WHERE market_date BETWEEN DATE_SUB("2023-07-17", INTERVAL 30 DAY) AND
"2023-07-17"
What is the time period for which the data is recorded in the farmer’s
market dataset?
SELECT
MIN(market_date) AS first_prch_date,
MAX(market_date) AS last_prch_date,
DATE_DIFF(MAX(market_date), MIN(market_date), DAY)
FROM `farmers_market.customer_purchases`
Customer Profiling
- First purchase
- Most recent purchase
- How many times they have visited the market
SELECT
customer_id,
MIN(market_date) AS first_prch_date,
MAX(market_date) AS last_prch_date,
DATE_DIFF(MAX(market_date), MIN(market_date), DAY) AS customer_time_period,
COUNT(DISTINCT market_date) AS uniq_visits
FROM `farmers_market.customer_purchases`
GROUP BY customer_id
ORDER BY customer_id