Skip to content

Commit

Permalink
clarify questions, instructions according to #181
Browse files Browse the repository at this point in the history
  • Loading branch information
wendy-aw committed Jun 25, 2024
1 parent 6e08675 commit 38076c1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
8 changes: 4 additions & 4 deletions data/instruct_advanced_bigquery.csv
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SPM (Selling Profit Margin) = (Total Amount from Sells - (Tax + Commission)) / T
TAC = Total Active Customers who joined within a specified timeframe
CR = Rank customers by their total transaction volume, identifying the customer with the highest transaction volume as rank 1. This involves joining price data with ticker identifiers and filtering for a specified date range."
car_dealership,bigquery,instructions_cte_join,"WITH sale_payments AS (SELECT s.id AS sale_id, s.sale_date, MAX(p.payment_date) AS latest_payment_date FROM car_dealership.sales AS s JOIN car_dealership.payments_received AS p ON s.id = p.sale_id GROUP BY s.id, s.sale_date) SELECT ROUND(AVG(DATE_DIFF(latest_payment_date, sale_date, DAY)), 2) AS avg_days_to_payment FROM sale_payments;","What is the average number of days between the sale date and payment received date, rounded to 2 decimal places?","When getting duration between sale and payment date for each sale, get the latest payment for sale by aggregating over the payments_received table first.","When getting duration between sale and payment date for each sale, get the latest payment for sale by aggregating over the payments_received table first. ASP = Calculate the average price of sales within a specific timeframe Last 30 days = Use a range from the current date minus a certain interval to the current date, always ensure to make the necessary joins before utilizing the sales data. TSC = Count of sales within a specified period"
car_dealership,bigquery,instructions_cte_join,"WITH latest_inventory_status AS (SELECT car_id, is_in_inventory, ROW_NUMBER() OVER (PARTITION BY car_id ORDER BY snapshot_date DESC NULLS FIRST, crtd_ts DESC NULLS FIRST) AS rn FROM car_dealership.inventory_snapshots) SELECT c.make, c.model, MAX(s.sale_price) AS highest_sale_price FROM car_dealership.cars AS c JOIN car_dealership.sales AS s ON c.id = s.car_id JOIN latest_inventory_status AS lis ON c.id = lis.car_id WHERE lis.is_in_inventory = FALSE AND lis.rn = 1 GROUP BY c.make, c.model ORDER BY highest_sale_price DESC NULLS FIRST;","Return the highest sale price for each make and model of cars that have been sold and are no longer in inventory, ordered by the sale price from highest to lowest.","When getting a car's inventory status, always take the latest status from the inventory_snapshots table","TSC = Count of sales within a specified period
car_dealership,bigquery,instructions_cte_join,"WITH latest_inventory_status AS (SELECT car_id, is_in_inventory, ROW_NUMBER() OVER (PARTITION BY car_id ORDER BY snapshot_date DESC NULLS FIRST, crtd_ts DESC NULLS FIRST) AS rn FROM car_dealership.inventory_snapshots) SELECT c.make, c.model, MAX(s.sale_price) AS highest_sale_price FROM car_dealership.cars AS c JOIN car_dealership.sales AS s ON c.id = s.car_id JOIN latest_inventory_status AS lis ON c.id = lis.car_id WHERE lis.is_in_inventory = FALSE AND lis.rn = 1 GROUP BY c.make, c.model ORDER BY highest_sale_price DESC NULLS FIRST;","Return the highest sale price for each make and model of cars that have been sold and are no longer in inventory, ordered by the sale price from highest to lowest. Use the most recent date in the inventory_snapshots table to determine that car's inventory status.","When getting a car's inventory status, always take the latest status from the inventory_snapshots table","TSC = Count of sales within a specified period
MoM = Change in total receivable amounts from one month to the next, comparing with the immediately preceding month.
ASP = Mean sale price for a designated start period
When getting a car's inventory status, always take the latest status from the inventory_snapshots table"
Expand All @@ -58,11 +58,11 @@ To get the number of sales made by each salesperson in the past 30 days, join th
ASP = Calculate the average sale price without specifying the period
GPM = Define gross profit margin as a ratio without specifying how to calculate total revenue or total cost"
car_dealership,bigquery,instructions_cte_window,"WITH salesperson_sales AS (SELECT salesperson_id, SUM(sale_price) AS total_sales, COUNT(*) AS num_sales FROM car_dealership.sales GROUP BY salesperson_id) SELECT s.first_name, s.last_name, ss.total_sales, ss.num_sales, RANK() OVER (ORDER BY ss.total_sales DESC NULLS FIRST) AS sales_rank FROM salesperson_sales AS ss JOIN car_dealership.salespersons AS s ON ss.salesperson_id = s.id;","Return the first name, last name, total sales amount, number of sales, and SR for each salesperson",SR = sales rank of each salesperson ordered by their total sales amount descending,"SR = sales rank of each salesperson ordered by their total sales amount descending To determine the sales performance per territory, sum the sales amount and count the sales, grouping by territory To calculate the average sale price, join the sales table with itself on the salesperson_id and find the ratio of total sales amount to number of sales To assess inventory turnover, compare inventory snapshots with sales on matching days, focusing on the quantity of items sold."
car_dealership,bigquery,instructions_cte_window,"WITH monthly_totals AS (SELECT TIMESTAMP_TRUNC(payment_date, MONTH) AS dt, SUM(payment_amount) AS total_payments FROM car_dealership.payments_received GROUP BY dt), monthly_range AS (SELECT GENERATE_DATE_ARRAY(DATE(TIMESTAMP_TRUNC(MIN(payment_date), MONTH)), DATE(TIMESTAMP_TRUNC(MAX(payment_date), MONTH)), INTERVAL 1 MONTH) AS date_range FROM car_dealership.payments_received), monthly_totals_with_zero AS (SELECT date_range AS dt, COALESCE(mt.total_payments, 0) AS total_payments FROM UNNEST((SELECT date_range FROM monthly_range)) AS date_range LEFT JOIN monthly_totals AS mt ON date_range = mt.dt) SELECT CAST(m.dt AS DATE) AS MONTH, m.total_payments, m.total_payments - LAG(m.total_payments, 1) OVER (ORDER BY m.dt) AS mom_change FROM monthly_totals_with_zero AS m ORDER BY m.dt;WITH monthly_totals AS (SELECT TIMESTAMP_TRUNC(payment_date, MONTH) AS dt, SUM(payment_amount) AS total_payments FROM car_dealership.payments_received GROUP BY dt), monthly_range AS (SELECT GENERATE_DATE_ARRAY(DATE(TIMESTAMP_TRUNC(MIN(payment_date), MONTH)), DATE(TIMESTAMP_TRUNC(MAX(payment_date), MONTH)), INTERVAL 1 MONTH) AS date_range FROM car_dealership.payments_received), monthly_totals_with_zero AS (SELECT date_range AS dt, COALESCE(mt.total_payments, 0) AS total_payments FROM UNNEST((SELECT date_range FROM monthly_range)) AS date_range LEFT JOIN monthly_totals AS mt ON date_range = mt.dt) SELECT CAST(m.dt AS DATETIME) AS MONTH, m.total_payments, m.total_payments - LAG(m.total_payments, 1) OVER (ORDER BY m.dt) AS mom_change FROM monthly_totals_with_zero AS m ORDER BY m.dt;",What is the total payments received per month? Also calculate the MoM change for each month.,MoM change = (current month value - prev month value). Return months with no payments as 0. MoM will always be zero for the first month that appears in your answer.,"To ascertain the volume of sales conducted by each salesperson over a recent period, merge the salespersons and sales tables, applying a filter for recent sales transactions.
car_dealership,bigquery,instructions_cte_window,"WITH monthly_totals AS (SELECT TIMESTAMP_TRUNC(payment_date, MONTH) AS dt, SUM(payment_amount) AS total_payments FROM car_dealership.payments_received GROUP BY dt), monthly_range AS (SELECT GENERATE_DATE_ARRAY(DATE(TIMESTAMP_TRUNC(MIN(payment_date), MONTH)), DATE(TIMESTAMP_TRUNC(MAX(payment_date), MONTH)), INTERVAL 1 MONTH) AS date_range FROM car_dealership.payments_received), monthly_totals_with_zero AS (SELECT date_range AS dt, COALESCE(mt.total_payments, 0) AS total_payments FROM UNNEST((SELECT date_range FROM monthly_range)) AS date_range LEFT JOIN monthly_totals AS mt ON date_range = mt.dt) SELECT CAST(m.dt AS DATE) AS MONTH, m.total_payments, m.total_payments - LAG(m.total_payments, 1) OVER (ORDER BY m.dt) AS mom_change FROM monthly_totals_with_zero AS m ORDER BY m.dt;WITH monthly_totals AS (SELECT TIMESTAMP_TRUNC(payment_date, MONTH) AS dt, SUM(payment_amount) AS total_payments FROM car_dealership.payments_received GROUP BY dt), monthly_range AS (SELECT GENERATE_DATE_ARRAY(DATE(TIMESTAMP_TRUNC(MIN(payment_date), MONTH)), DATE(TIMESTAMP_TRUNC(MAX(payment_date), MONTH)), INTERVAL 1 MONTH) AS date_range FROM car_dealership.payments_received), monthly_totals_with_zero AS (SELECT date_range AS dt, COALESCE(mt.total_payments, 0) AS total_payments FROM UNNEST((SELECT date_range FROM monthly_range)) AS date_range LEFT JOIN monthly_totals AS mt ON date_range = mt.dt) SELECT CAST(m.dt AS DATETIME) AS MONTH, m.total_payments, m.total_payments - LAG(m.total_payments, 1) OVER (ORDER BY m.dt) AS mom_change FROM monthly_totals_with_zero AS m ORDER BY m.dt;",What is the total payments received per month? Also calculate the MoM change for each month.,"MoM change = (current month value - prev month value). Return all months in your answer, including those where there were no payments. MoM will always be zero for the first month that appears in your answer.","To ascertain the volume of sales conducted by each salesperson over a recent period, merge the salespersons and sales tables, applying a filter for recent sales transactions.
To determine the average duration from sale date to payment date, perform a join between the sales and payments tables
To calculate the average selling price, join the sales and products tables, group by product name, and compute the ratio of total sales amount to the number of sales
MoM change = (current month value - prev month value). Return months with no payments as 0."
car_dealership,bigquery,instructions_date_join,"WITH date_range AS (SELECT ARRAY(SELECT AS STRUCT DATE_ADD(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL x MONTH) AS month_start FROM UNNEST(GENERATE_ARRAY(0, 5)) AS x) AS months), flattened_date_range AS (SELECT month.month_start FROM date_range, UNNEST(months) MONTH), sales_metrics AS (SELECT DATE_TRUNC(s.sale_date, MONTH) AS sale_month, COUNT(s.id) AS PMSPS, SUM(s.sale_price) AS PMSR FROM car_dealership.sales AS s JOIN car_dealership.salespersons AS sp ON s.salesperson_id = sp.id WHERE EXTRACT(YEAR FROM sp.hire_date) BETWEEN 2022 AND 2023 AND s.sale_date >= DATE_ADD(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL -6 MONTH) AND s.sale_date < DATE_TRUNC(CURRENT_DATE(), MONTH) GROUP BY sale_month) SELECT dr.month_start, COALESCE(sm.PMSPS, 0) AS PMSPS, COALESCE(sm.PMSR, 0) AS PMSR FROM flattened_date_range AS dr LEFT JOIN sales_metrics AS sm ON dr.month_start = sm.sale_month ORDER BY dr.month_start ASC;","What are the PMSPS and PMSR in the last 6 months excluding the current month, for salespersons hired between 2022 and 2023 (both inclusive)? Include months where metrics are 0. Order by month ascending.",PMSPS = per month salesperson sales count. PMSR = per month sales revenue in dollars. Truncate date to month for aggregation.,"PMSPS = per month salesperson sales count. PMSR = per month sales revenue in dollars. Truncate date to month for aggregation.
MoM change = (current month value - prev month value). Return all months in your answer, including those where there were no payments."
car_dealership,bigquery,instructions_date_join,"WITH date_range AS (SELECT ARRAY(SELECT AS STRUCT DATE_ADD(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL x MONTH) AS month_start FROM UNNEST(GENERATE_ARRAY(0, 5)) AS x) AS months), flattened_date_range AS (SELECT month.month_start FROM date_range, UNNEST(months) MONTH), sales_metrics AS (SELECT DATE_TRUNC(s.sale_date, MONTH) AS sale_month, COUNT(s.id) AS PMSPS, SUM(s.sale_price) AS PMSR FROM car_dealership.sales AS s JOIN car_dealership.salespersons AS sp ON s.salesperson_id = sp.id WHERE EXTRACT(YEAR FROM sp.hire_date) BETWEEN 2022 AND 2023 AND s.sale_date >= DATE_ADD(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL -6 MONTH) AND s.sale_date < DATE_TRUNC(CURRENT_DATE(), MONTH) GROUP BY sale_month) SELECT dr.month_start, COALESCE(sm.PMSPS, 0) AS PMSPS, COALESCE(sm.PMSR, 0) AS PMSR FROM flattened_date_range AS dr LEFT JOIN sales_metrics AS sm ON dr.month_start = sm.sale_month ORDER BY dr.month_start ASC;","What are the PMSPS and PMSR in the last 6 months excluding the current month, for salespersons hired between 2022 and 2023 (both inclusive)? Return all months in your answer, including those where metrics are 0. Order by month ascending.",PMSPS = per month salesperson sales count. PMSR = per month sales revenue in dollars. Truncate date to month for aggregation.,"PMSPS = per month salesperson sales count. PMSR = per month sales revenue in dollars. Truncate date to month for aggregation.
ASP = Average Sale Price during a specific timeframe
To calculate the average days between a sale date and when the payment was received, join the relevant tables.
TSC = Total Sales Count for a given period"
Expand Down
Loading

0 comments on commit 38076c1

Please sign in to comment.