SQL/大查询:使用另一个表算取零

发布于 2025-01-27 20:40:44 字数 2032 浏览 4 评论 0原文

我有下表结构,每个商店的一行,每15分钟:

data_table:

TimePaydayHourStore购买
2022-01-23 10:00:00110S1100
2022-01-23 10:00 1:00110S250
2022-01-23 10:00:00110S32
2022-01-23 10:15:00110S1NULL
2022-01-23 10:15:00110S2120
2022-01-23 10:15:00110S330
2022-01-23 10:30:00110S15
2022-01-23 10:30:00110S2NULL
2022-01-23 10:30:00110S350

我想根据同一工作日,小时和存储,使用iMpoint_table的购买列中的“ data_table”采购列中的所有零值。这两个表具有不同的形状,Impoint_table仅包含工作日,小时和商店的可能组合以及购买的价值。

iMpoint_table:

工作日小时商店购买
110S175.5
110S235
110S35
111S18 1 80
111S295.5
111S325
112S190.5
112S2 10 1 12 S210
112S345

,例如第一个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:

timeweekdayhourstorepurchase
2022-01-23 10:00:00110S1100
2022-01-23 10:00:00110S250
2022-01-23 10:00:00110S32
2022-01-23 10:15:00110S1null
2022-01-23 10:15:00110S2120
2022-01-23 10:15:00110S330
2022-01-23 10:30:00110S15
2022-01-23 10:30:00110S2null
2022-01-23 10:30:00110S350

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:

weekdayhourstorepurchase
110S175.5
110S235
110S35
111S180
111S295.5
111S325
112S190.5
112S210
112S345

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2025-02-03 20:40:45

通过提供的数据,请尝试以下内容:

select
  dt.time,
  dt.weekday,
  dt.hour,
  dt.store,
  ifnull(dt.purchase, it.purchase)
from data_table dt
left join imputation_table it
 on dt.weekday=it.weekday
 and dt.hour=it.hour
 and dt.store=it.store

这将导致以下内容:

”在此处输入图像说明“

ifnull 如果提供的值为null,则允许替换值。在这种情况下,如果从数据表中购买的情况为null,则返回插补购买值。主要钥匙是连接两个表,以便可以与之匹配。

With the data you provided try the following:

select
  dt.time,
  dt.weekday,
  dt.hour,
  dt.store,
  ifnull(dt.purchase, it.purchase)
from data_table dt
left join imputation_table it
 on dt.weekday=it.weekday
 and dt.hour=it.hour
 and dt.store=it.store

This results in the following:

enter image description here

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.

夏天碎花小短裙 2025-02-03 20:40:45

请考虑以下较小版本

select d.* replace(ifnull(d.purchase, i.purchase) as purchase)
from data_table d
left join imputation_table i
using (weekday, hour, store)        

如果应用于问题中的示例数据,则在您的问题中 - 输出为

”在此处输入图像描述”

Consider below slim version

select d.* replace(ifnull(d.purchase, i.purchase) as purchase)
from data_table d
left join imputation_table i
using (weekday, hour, store)        

if applied to sample data in your question - output is

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文