【问题标题】:BigQuery SQL: How to fetch respective sales from other table w.r.t value matching from one table with multiple conditions?BigQuery SQL:如何从具有多个条件的一个表中从另一个表中获取相应的销售额w.r.t值匹配?
【发布时间】:2021-02-22 13:28:15
【问题描述】:

有两个 Bigquery 表,

表 1:

store   article  sales
11      aa       14.5
11      bb       10.0
12      aa
12      bb
12      cc
13      aa       12.0
13      bb       11.4
13      dd       12.5

表 2:

store   status      likestore
11      Active      
12      New         13
13      New

场景: 如果该商店的 tab2.status = 'New' 且 tab1.sales 为 NULL,则为该商店分配 tab2.likestore 的销售额。

试用:

select *,
    case when tab2.status = 'New' and tab1.sales IS NULL then <to add other conditions explained in Scenario>
    from tab1
    left join tab2 on tab2.store = tab1.store

最终结果:

store   article  sales
11      aa       14.5
11      bb       10.0
12      aa       12.0
12      bb       11.4
12      cc
13      aa       12.0
13      bb       11.4
13      dd       12.5

【问题讨论】:

    标签: sql google-bigquery case procedure


    【解决方案1】:

    这回答了问题的原始版本。

    嗯。 . .假设每个商店有一行然后使用left join:

    select t1.*,
           coalesce(t1.sales, tt1.sales) as imputed_sales
    from table1 t1 left join
         table2 t2
         on t1.store = t2.store and t2.status = 'New' left join
         table1 tt1
         on tt1.store = t2.likestore;
    

    注意:这里假设'' 真正意味着NULL。对于应该是数字列的内容,空字符串没有意义。但是如果你错误地将数字存储为字符串,你可以使用case 而不是coalesce()

    【讨论】:

    • 感谢您的意见。但正如您所提到的,现在已经在 Store-Article 级别编辑了表格内容。请让我知道如何更改您的解决方案
    • 您能否为当前版本的问题提供解决方案。这可能对我有很大帮助。谢谢!
    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2019-04-18
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多