SQL/大查询:使用另一个表算取零
我有下表结构,每个商店的一行,每15分钟:
data_table:
Time | Payday | Hour | Store | 购买 |
---|---|---|---|---|
2022-01-23 10:00:00 | 1 | 10 | S1 | 100 |
2022-01-23 10:00 1:00 | 1 | 10 | S2 | 50 |
2022-01-23 10:00:00 | 1 | 10 | S3 | 2 |
2022-01-23 10:15:00 | 1 | 10 | S1 | NULL |
2022-01-23 10:15:00 | 1 | 10 | S2 | 120 |
2022-01-23 10:15:00 | 1 | 10 | S3 | 30 |
2022-01-23 10:30:00 | 1 | 10 | S1 | 5 |
2022-01-23 10:30:00 | 1 | 10 | S2 | NULL |
2022-01-23 10:30:00 | 1 | 10 | S3 | 50 |
我想根据同一工作日,小时和存储,使用iMpoint_table的购买列中的“ data_table”采购列中的所有零值。这两个表具有不同的形状,Impoint_table仅包含工作日,小时和商店的可能组合以及购买的价值。
iMpoint_table:
工作日 | 小时 | 商店 | 购买 |
---|---|---|---|
1 | 10 | S1 | 75.5 |
1 | 10 | S2 | 35 |
1 | 10 | S3 | 5 |
1 | 11 | S1 | 8 1 80 |
1 | 11 | S2 | 95.5 |
1 | 11 | S3 | 25 |
1 | 12 | S1 | 90.5 |
1 | 12 | S2 10 1 12 S2 | 10 |
1 | 12 | S3 | 45 |
,例如第一个NULL data_table应从iMpoint_table(工作日= 1,hour = 10 and store = s1)中归纳为75.5,第二个为35(工作日= 1,小时= 10和store = s2)。
我应该如何在SQL(大查询)中执行此操作?
I have the following table structure, one row for each store and for every 15 minutes:
data_table:
time | weekday | hour | store | purchase |
---|---|---|---|---|
2022-01-23 10:00:00 | 1 | 10 | S1 | 100 |
2022-01-23 10:00:00 | 1 | 10 | S2 | 50 |
2022-01-23 10:00:00 | 1 | 10 | S3 | 2 |
2022-01-23 10:15:00 | 1 | 10 | S1 | null |
2022-01-23 10:15:00 | 1 | 10 | S2 | 120 |
2022-01-23 10:15:00 | 1 | 10 | S3 | 30 |
2022-01-23 10:30:00 | 1 | 10 | S1 | 5 |
2022-01-23 10:30:00 | 1 | 10 | S2 | null |
2022-01-23 10:30:00 | 1 | 10 | S3 | 50 |
I want to impute all the null values in the purchase column in data_table using purchase column from imputation_table, on the basis of the same weekday, hour and store. The two tables have different shapes, imputation_table contains only the possible combinations of weekday, hour and store and a value for purchase.
imputation_table:
weekday | hour | store | purchase |
---|---|---|---|
1 | 10 | S1 | 75.5 |
1 | 10 | S2 | 35 |
1 | 10 | S3 | 5 |
1 | 11 | S1 | 80 |
1 | 11 | S2 | 95.5 |
1 | 11 | S3 | 25 |
1 | 12 | S1 | 90.5 |
1 | 12 | S2 | 10 |
1 | 12 | S3 | 45 |
For example, the first null in the data_table should be imputed with 75.5 from the imputation_table (weekday=1, hour=10 and store=S1) and the 2nd null with 35 (weekday=1, hour=10 and store=S2).
How should I do this in SQL (Big Query)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过提供的数据,请尝试以下内容:
这将导致以下内容:
ifnull 如果提供的值为null,则允许替换值。在这种情况下,如果从数据表中购买的情况为null,则返回插补购买值。主要钥匙是连接两个表,以便可以与之匹配。
With the data you provided try the following:
This results in the following:
IFNULL allows to replace the value if the provided value is null. In this scenario if purchase from the data table is null it returns the imputation purchase value. The main key is joining the two tables so it can have a result to match to.
请考虑以下较小版本
如果应用于问题中的示例数据,则在您的问题中 - 输出为
Consider below slim version
if applied to sample data in your question - output is