81.7k views
1 vote
As part of the development of summary reports for the warehouse accounting system, create a query that returns certain metrics based on statistical data, such as: - the customer who owns the smallest warehouse- the customer who owns the largest warehouse - the customer who owns the fewest warehouses - the customer who owns the most warehouses

User Paul N
by
7.9k points

1 Answer

5 votes

Answer:

SELECT

MIN(warehouse_size) AS smallest_warehouse,

MAX(warehouse_size) AS largest_warehouse,

customer_id,

COUNT(*) AS num_warehouses

FROM

warehouse_table

GROUP BY

customer_id

ORDER BY

num_warehouses ASC, warehouse_size ASC

Step-by-step explanation:

This query retrieves the minimum and maximum warehouse sizes across all customers, as well as the number of warehouses owned by each customer. The GROUP BY clause groups the results by the customer_id column, while the MIN, MAX, and COUNT functions aggregate the data for each customer.

The results are ordered by ascending order of the number of warehouses owned (num_warehouses) and then by ascending order of the warehouse size (warehouse_size). This allows us to identify the customers who own the smallest and largest warehouses, as well as those who own the fewest and most warehouses.

User Chitrang
by
9.0k points