Database Query Writer | SQL
Creating a Table to Track New Customers Each Month
This SQL query explains how to create a table named 'new_customers_each_month' that tracks the number of brand new customers each month. The query extracts the year and month from the order date, counts the distinct customer IDs, and groups the data...
![Empty image or helper icon](/assets/data-mentor/hero-banner.jpg)
This Query related with thread "Creating a Table to Track New Customers Each Month"
Prompt
Create a table from a sales table over multiple years, that shows brand new customers each month
Answer
To create a table that shows brand new customers each month, we can use the following SQL query:
CREATE TABLE new_customers_each_month AS
SELECT EXTRACT(YEAR FROM order_date) AS year,
EXTRACT(MONTH FROM order_date) AS month,
COUNT(DISTINCT customer_id) AS new_customers
FROM sales
WHERE customer_id NOT IN (
SELECT DISTINCT customer_id
FROM sales
WHERE EXTRACT(YEAR FROM order_date) > (SELECT MIN(EXTRACT(YEAR FROM order_date)) FROM sales)
)
GROUP BY year, month;
Explanation:
- The
CREATE TABLE
statement is used to create a new table namednew_customers_each_month
. - The
SELECT
statement retrieves the required data:EXTRACT(YEAR FROM order_date)
andEXTRACT(MONTH FROM order_date)
are used to extract the year and month from theorder_date
column.COUNT(DISTINCT customer_id)
counts the number of distinct customer IDs.
- The
WHERE
clause filters the customers who placed their first order in the current year:- The subquery
(SELECT MIN(EXTRACT(YEAR FROM order_date)) FROM sales)
retrieves the earliest year from the sales data. customer_id NOT IN (...)
excludes customers who placed an order in any year after the earliest year.
- The subquery
- Finally, the
GROUP BY
clause groups the data by year and month.
Note: The SQL query assumes that the original sales table is named sales
. Make sure to replace it with the actual name of your sales table.
Description
More Database Query Writers
Apache Flink Database Query WriterApache Pig Database Query WriterAzure Data Factory Database Query WriterC/C++ Database Query WriterCouchDB Database Query WriterDAX Database Query WriterExcel Database Query WriterFirebase Database Query WriterGoogle BigQuery Database Query WriterGoogle Sheets Database Query WriterGraphQL Database Query WriterHive Database Query WriterJava Database Query WriterJavaScript Database Query WriterJulia Database Query WriterLua Database Query WriterM (Power Query) Database Query WriterMATLAB Database Query WriterMongoDB Database Query WriterOracle Database Query WriterPostgreSQL Database Query WriterPower BI Database Query WriterPython Database Query WriterR Database Query WriterRedis Database Query WriterRegex Database Query WriterRuby Database Query WriterSAS Database Query WriterScala Database Query WriterShell Database Query WriterSPSS Database Query WriterSQL Database Query WriterSQLite Database Query WriterStata Database Query WriterTableau Database Query WriterVBA Database Query Writer