***********
- ********************************************************************************************
*************************************
.- *******************************************
Table: orders
An eCommerce company's online order table.
col_name | col_type --------------+------------------- order_id | bigint product_id | bigint customer_id | bigint order_dt | date qty | integer unit_price_usd| float channel | varchar(20) -- mobile, desktop
Sample results
product_id | sum ------------+----- 10000023 | 92 10000093 | 114 10000077 | 92 10000095 | 77 10000045 | 136 10000036 | 75 10000019 | 88 10000096 | 113 10000039 | 96 10000010 | 113 10000038 | 105 10000084 | 97
Solution postgres
SELECT product_id, SUM(qty)
FROM orders
WHERE order_dt >= '2021-08-01'
AND order_dt < '2021-09-01'
GROUP BY product_id;
Explanation
This query is selecting the product_id and the sum of quantities ordered for each product within a specific time period. The time period is defined by the WHERE clause, which specifies that only orders placed between August 1, 2021 and September 1, 2021 should be included in the results.
The results are grouped by product_id using the GROUP BY clause. This means that the sum of quantities ordered will be calculated for each unique product_id in the orders table.
Overall, this query is useful for analyzing sales data for specific products during a particular time frame.
Expected results