【问题标题】:Multiple columns in a subquery子查询中的多个列
【发布时间】:2021-07-05 17:54:37
【问题描述】:

我正在尝试查找上周选择的产品与本周选择的产品,以找出选择决策中的流失。目前我正在为单个站点执行此操作,结果在正确数量的记录下运行良好。现在我想更改在单个查询中获取 10 个站点的输出的代码。

create temporary view removes as
  select distinct
         asin,
         lagweek,
         fc,
         'removed' as state,
         demand_pp,
         instock_pp,
         source,
         filter_reason,
         is_missing_in_pp,
         is_missing_in_dc,
         is_missing_in_nmi,
         asin_nmi,
         asin_pre,
         asin_dc,
         filter_reason_old,
         asin_omi,
         asin_preo,
         asin_dco
  from sel_old so where asin not in (select asin from sel_new sn where sn.lagweek = so.lagweek);

由于这是针对单个站点,因此只执行 asin not in (select asin ...) 可以正常工作,但现在我想从相同的逻辑查看跨多个站点的 ASIN。我尝试了下面的方法,但它返回的记录数不正确。

create temporary view removes as
  select distinct
         so.asin,
         so.lagweek,
         so.fc,
         'removed' as state,
         so.demand_pp,
         so.instock_pp,
         so.source,
         so.filter_reason,
         so.is_missing_in_pp,
         so.is_missing_in_dc,
         so.is_missing_in_nmi,
         so.asin_nmi,
         so.asin_pre,
         so.asin_dc,
         so.filter_reason_old,
         so.asin_omi,
         so.asin_preo,
         so.asin_dco
  from sel_old so left join (select asin, fc, lagweek from sel_new) as sn
         on (so.asin <> sn.asin)
         and (so.fc = sn.fc)
         and (so.lagweek = sn.lagweek);

我应该如何处理这个问题。如果有的话,我找不到更简单的解决方案。

【问题讨论】:

    标签: sql join subquery in-subquery


    【解决方案1】:

    您可以使用EXISTS 谓词。它不会产生额外的记录,只是测试一些案例的存在并进行相应的过滤。

    select distinct
             so.asin,
             so.lagweek,
             so.fc,
             'removed' as state,
             so.demand_pp,
             so.instock_pp,
             so.source,
             so.filter_reason,
             so.is_missing_in_pp,
             so.is_missing_in_dc,
             so.is_missing_in_nmi,
             so.asin_nmi,
             so.asin_pre,
             so.asin_dc,
             so.filter_reason_old,
             so.asin_omi,
             so.asin_preo,
             so.asin_dco
    from sel_old so
    where not exists (
      select 1
      from sel_new sn
      where so.fc = sn.fc
        and so.lagweek = sn.lagweek
        and so.asin = sn.asin
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多