grouping-sets

Calculating multiple averages across different parts of the table?

安稳与你 提交于 2021-02-11 13:12:16
问题 I have the following transactions table: customer_id purchase_date product category department quantity store_id 1 2020-10-01 Kit Kat Candy Food 2 store_A 1 2020-10-01 Snickers Candy Food 1 store_A 1 2020-10-01 Snickers Candy Food 1 store_A 2 2020-10-01 Snickers Candy Food 2 store_A 2 2020-10-01 Baguette Bread Food 5 store_A 2 2020-10-01 iPhone Cell phones Electronics 2 store_A 3 2020-10-01 Sony PS5 Games Electronics 1 store_A I would like to calculate the average number of products purchased

Optimizing query with multiple sums?

删除回忆录丶 提交于 2021-02-10 15:46:26
问题 I have table products : +----------+-----------+----------+---------+ |family_id |shopper_id |product_id|quantity | +----------+-----------+----------+---------+ |A |1 |Kit Kat |10 | |A |1 |Kit Kat |5 | |A |1 |Snickers |9 | |A |2 |Kit Kat |7 | |B |3 |Kit Kat |2 | +----------+---------- +----------+---------+ For each product, I want to calculate 2 totals: total quantity per shopper total quantity per family. Sum of total quantities for all shoppers in the same family. The final table should