__STYLES__

SQL Customer Sales Summary retrieving and joining data from multiple sources

SQL Customer Sales Summary retrieving and joining data from multiple sources

About this project

The above SQL query helps to retrieves data from multiple tables and calculates the total amount spent by customers in US dollars. Let's break down the query step by step:

SELECT TOP(100): This specifies that we only want to retrieve the top 100 rows from the result set. The number can be adjusted to retrieve a different number of rows.

CONCAT(dc.FirstName,' ',dc.LastName) AS CustomerName: This concatenates the FirstName and LastName columns from the DimCustomer table and assigns it an alias CustomerName.

dc.EmailAddress AS EmailAddress: This selects the EmailAddress column from the DimCustomer table.

SUM(fs.SalesAmount) AS AmountSpent: This calculates the sum of the SalesAmount column from the FactInternetSales table and assigns it an alias AmountSpent. It represents the total amount spent by each customer.

FROM FactInternetSales AS fs: This specifies the source table FactInternetSales and assigns it the alias fs.

INNER JOIN DimCustomer AS dc ON fs.CustomerKey = dc.CustomerKey: This performs an inner join between the FactInternetSales and DimCustomer tables using the CustomerKey column as the common key.

INNER JOIN DimCurrency AS dcy ON fs.CurrencyKey = dcy.CurrencyKey: This performs another inner join between the FactInternetSales and DimCurrency tables using the CurrencyKey column as the common key.

WHERE dcy.CurrencyName = N'US Dollar': This specifies a condition to filter the rows where the CurrencyName from the DimCurrency table is equal to 'US Dollar'. This ensures that only transactions in US dollars are considered.

GROUP BY dc.FirstName, dc.LastName, dc.EmailAddress: This groups the result set by the FirstName, LastName, and EmailAddress columns from the DimCustomer table. This allows for aggregating the sales amount for each unique combination of these columns.

ORDER BY AmountSpent DESC: This sorts the result set in descending order based on the AmountSpent column. Customers who have spent the most will appear at the top of the list.

The commented lines (--dcy.CurrencyName AS Currency and --HAVING dcy.CurrencyName = N'US Dollar') are not executed as they are commented out.

They are included for reference purposes or to test different variations of the query.

Discussion and feedback(0 comments)
2000 characters remaining
Cookie SettingsWe use cookies to enhance your experience, analyze site traffic and deliver personalized content. Read our Privacy Policy.