logo

Back to questions

Purchasing Activity by Product Type

Medium

This is the same question as problem #4 in the SQL Chapter of Ace the Data Science Interview!

Assume you are given the table below for purchasing activity by product type. Write a query to calculate the cumulative purchases for each product type over time in chronological order.

Output the order date, product, and the cumulative number of quantities purchased (conveniently abbreviated as ).

Table:

Column NameType
order_idinteger
product_typestring
quantityinteger
order_datedatetime

Example Input:

order_idproduct_typequantityorder_date
213824printer2006/27/2022 12:00:00
212312hair dryer506/28/2022 12:00:00
132842printer1806/28/2022 12:00:00
284730standing lamp807/05/2022 12:00:00

Example Output:

order_dateproduct_typecum_purchased
06/27/2022 12:00:00printer20
06/28/2022 12:00:00hair dryer5
06/28/2022 12:00:00printer38
07/05/2022 12:00:00standing lamp8

Explanation

Over the course of June 27, 2022, 20 printers were purchased.

Over the course of June 28, 2022, 5 hair dryers were purchased.

PostgreSQL 14

Refer friends to get bonus content & cool prizes.