[英]Snowflake SQL Counting and Summing from Another Table based on Matching Value
[英]Snowflake SQL Sum from another table based on matching values
我真的可以在这里使用一些帮助。 知道如何使用 Excel SUMIFS 执行此操作,但表 1 对于 Excel 来说太大了 + 我的数据已经在 Snowflake SQL 中。 当表 2 与表 1 匹配时(商店、产品和日期在开始和结束日期内),我想确定表 2 中的销售额,我将使用它来确定哪些价格促销在基准期内推动了最多的销售额。 谢谢!!
店铺 | 日期 | 产品 | 销售量 |
---|---|---|---|
1 | 1-1-2021 | 一种 | 12 |
1 | 1-1-2021 | 乙 | 20 |
1 | 1-6-2021 | 一种 | 14 |
1 | 1-6-2021 | 乙 | 9 |
2 | 1-1-2021 | 一种 | 29 |
2 | 1-1-2021 | 乙 | 17 |
2 | 1-6-2021 | 一种 | 5 |
2 | 1-6-2021 | 乙 | 21 |
店铺 | 开始日期 | 结束日期 | 活动 | 产品 |
---|---|---|---|---|
1 | 1-1-2021 | 1-3-2021 | 花 100 美元,然后节省 10 美元 | 一种 |
2 | 1-2-2021 | 1-5-2021 | 15% 折扣 | 乙 |
您只是在寻找join
和聚合吗?
select t2.store, t2.start_date, t2.end_date, t2.product,
sum(t1.sales)
from table1 t1 join
table2 t2
on t1.store = t2.store and
t1.product = t2.product and
t1.date >= t2.start_date and
t1.date <= t2.end_date
group by t2.store, t2.start_date, t2.end_date, t2.product;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.