0% found this document useful (0 votes)
26 views4 pages

SQL Queries for Data Warehouse Analysis

The document provides a sample dataset for a data warehouse containing transaction and customer data. It lists 5 problems to solve using SQL queries on the sample data. The problems include showing total amounts by customer age and gender, fixing errors in education data and combining degree types, calculating days past due for unpaid transactions, summarizing customer data, and providing a summary with age buckets and top/bottom transaction amounts. The expected result is SQL script files solving each problem.

Uploaded by

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

SQL Queries for Data Warehouse Analysis

The document provides a sample dataset for a data warehouse containing transaction and customer data. It lists 5 problems to solve using SQL queries on the sample data. The problems include showing total amounts by customer age and gender, fixing errors in education data and combining degree types, calculating days past due for unpaid transactions, summarizing customer data, and providing a summary with age buckets and top/bottom transaction amounts. The expected result is SQL script files solving each problem.

Uploaded by

Dhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Hi,

We are in need someone to do some query to our data


warehouse and format/structure it into something that can be
used by our analyst or data science team either in their
dashboard or data model feature. For this purpose you will be
given a sample dataset with their structure and our user’s
problem/requirement.
Dataset:
You will be working with this sample dataset and their
definition given in the next slides. Take your time to
understand the data structure so you will be able to solve the
upcoming problem.
Problem:
These will be the requirement we need you to solve. There are
5 problem you will need to work on ordered by their relative
difficulty. Good luck.
**Expected result : <sql-script-file>.sql

Table: Transaction

Table: Customer
Biz Glossary/Data Definition
 loan_status (string): whether loan already paid or not
contain ‘PAIDOFF’ or ‘ACTIVE’.
 amount (integer): total amount of that transaction
 tenure (integer): how long the installment is 7, 15, 30,
60, etc. in days.
 eff_date (date): date when the transaction happen and
the installment start counting
 due_date (date): when the transaction need to be paid
 paid_off_time (timestamp): when the customer actually
paid the transaction
 age (integer): customer’s age
 education (string): customer’s last education level
 gender (string): customer’s gender
 days_past_due (integer): how many days after
due_date customer havent make payments.

Problem:
Construct query script to solve below problem using previous
page dataset, preferably in sql code compatible with MySQL
standards:
1. Show total amount of transaction made by/grouped by
customer’s age and gender.
2. We have a typo in education, find it and fix it within your
query. Also there are still many variance of Master
degrees that need to be combined into 1 (called ‘Master
Degree’). Then after all that show total unique customer
by their education level that has transaction with us.
Problem:
Construct query script to solve below problem using previous
page dataset, preferably in sql code compatible with MySQL
standards:
3. For each customer and their tenure selection, we need
their first transaction eff_date, due_date, and
paid_off_time if they already paid. Then afterwards for
each first transaction of that hasn’t paid we need to
calculate their days_past_due compared to today. Terms
and tenure is same.

Problem:
Construct query script to solve below problem using previous
page dataset, preferably in sql code compatible with MySQL
standards:
4. Need a summarized customer table consist of below
format. For every customer their summary of total
amount, when is their first and last transaction, due date,
and not forget their favorite tenure product (either 7, 15,
30, etc.). Terms and tenure is same.

**Expected result : <sql-script-file>.sql

Problem:
Construct query script to solve below problem using previous
page dataset, preferably in sql code compatible with MySQL
standards:
5. We need a summary table of age profiling as below with
age bucket and their top and bottom amount in
transposed format. And within their age group top-3
(biggest amount) and bottom-3 (smallest amount) along
with their customer_id all in a transposed format as
below.

**Expected result : <sql-script-file>.sql

Common questions

Powered by AI

The SQL query should use a GROUP BY clause to segment data by the 'age' and 'gender' fields and sum the 'amount' field to get the total transaction amounts. Considerations include ensuring that age and gender fields are correctly categorized and that any NULL values are appropriately handled to avoid misrepresentation of data subsets .

Ensuring compliance involves using MySQL-specific functions and syntax, such as TIMESTAMPDIFF instead of DATEDIFF for time calculations and CONCAT for string manipulation. Testing queries in a MySQL environment confirms compatibility, while referring to MySQL documentation ensures correct function usage .

The SQL query needs to use a CASE statement or an UPDATE query to replace variations of 'Master degrees' with 'Master Degree.' Additionally, a WHERE clause should identify the typo in the education column, allowing for correction through the same CASE or UPDATE query .

Implementing a solution involves using RANK or ROW_NUMBER window functions to order transaction amounts within each age group. Subqueries or common table expressions (CTEs) are needed to separate and transpose the top three and bottom three transaction amounts along with their customer_ids into the final format .

Approaches include using SQL's string manipulation functions like REPLACE or CASE to standardize variations into single categories before using GROUP BY clauses to aggregate based on these standardized education levels, ensuring accurate grouping and count of unique customers .

Key elements include the original due date, calculated days past due using TIMESTAMPDIFF, and the status of the loan. Tracking requires joins or subqueries to correlate these elements accurately and report on each customer's late payments status .

Creating a transposed view involves using CTEs to first rank transactions within age brackets by amount. Next, a pivot is manually constructed, perhaps through conditionals in SELECT statements that parse these ranks into separate columns for display. This involves careful use of window functions and JOINs .

Data validation can be implemented through constraints or WHERE clauses to identify anomalies, such as negative amounts or past due 'due_dates'. Additionally, using CHECK constraints or trigger functions can prevent invalid entries at the database schema level .

To calculate days past due for unpaid transactions, use a SELECT statement to find the minimum 'eff_date' for each customer by tenure, and a subsequent DATEDIFF or TIMESTAMPDIFF function to subtract the 'due_date' from the current date for unpaid transactions. This approach efficiently isolates and calculates necessary date differences .

The SQL query should use GROUP BY customer_id and aggregate data using functions like SUM for total amount, MIN and MAX for first and last transaction dates and due dates, and a mode or frequent itemset function to determine the favorite tenure product. It requires nested queries or a window function to effectively determine the mode for tenure .

You might also like