4. Aggregate Functions for Data Analysis
Activity 4.01: Analyzing Sales Data Using Aggregate Functions
Solution:
- Open
pgAdmin
, connect to thesqlda
database, and open SQL query editor. - Calculate the total number of unit sales the company has made:
SELECT COUNT(*) FROM sales;
The result is as follows:
Note that because each sales transaction contains a product ID, there is no NULL
value in the product_id
column. So, COUNT(product_id)
will also work. Similarly, COUNT(sales_amount)
will also work.
- Calculate the total sales amount in dollars for each state:
SELECT c.state, SUM(s.sales_amount)::DECIMAL(12,2) FROM sales s JOIN customers c ON s.customer_id = c.customer_id GROUP BY c.state ORDER BY 1;
The result is as follows:
- Identify the top five best dealerships in terms of the most units...