To calculate the conversion rate by month, excluding park employees based on their wristband numbers, and display the first day of each month along with the conversion rate, we will assume a few things about your database schema:
- There is a
date or datetime column in the PIZZA table that logs when each pizza order was made.
- The total number of unique visitors per month is known or can be extracted from the data. If this information (total visitors per month) is not tracked in your database, calculating an accurate monthly conversion rate can be challenging without an estimated total visitors count per month.
- You are looking for the calculation of conversion rate as the ratio of unique visitors (excluding employees) who ordered pizza to the total number of unique visitors for each month.
Given these points, the SQL query could look something like this:
WITH MonthlyOrders AS (
SELECT
DATE_TRUNC('month', date) AS month_start,
COUNT(DISTINCT bracelet_id) AS unique_orders
FROM
PIZZA
WHERE
bracelet_id NOT IN (145738, 145759, 145773, 145807, 145815, 145821, 145873, 145880)
GROUP BY
month_start
),
TotalVisitorsPerMonth AS (
-- Replace this with the actual calculation or static values of total visitors per month
SELECT
DATE_TRUNC('month', YourDateColumn) as month_start,
COUNT(DISTINCT visitor_id) as total_visitors
FROM
YourVisitorTable
GROUP BY
month_start
)
SELECT
mo.month_start,
mo.unique_orders * 1.0 / tv.total_visitors AS conversion_rate
FROM
MonthlyOrders mo
JOIN
TotalVisitorsPerMonth tv ON mo.month_start = tv.month_start
ORDER BY
mo.month_start;
A few things to note:
- Replace
YourDateColumn and YourVisitorTable with the actual column/table names that store the visitor data and their visit dates. We're assuming you have this information available.
- The
DATE_TRUNC('month', date) function is used to truncate the date to the first day of the month, allowing us to group orders and visitors by month.
- We're using a Common Table Expression (CTE) to first calculate the number of unique orders per month, excluding employees, and then calculate or use the total number of unique visitors per month from another table or data source.
- The query excludes park employees by filtering out their bracelet IDs with
WHERE bracelet_id NOT IN (...).
- This query assumes you have a way to calculate or provide the total number of visitors (
total_visitors) per month. Adjustments might be needed based on your schema.
Remember, this query might need tweaking depending on your exact database schema and the way data is stored and organized in your system.