【问题标题】:Get Data Of Third Column Based on Two other Columns根据其他两列获取第三列的数据
【发布时间】:2013-03-22 02:21:14
【问题描述】:

这是一个示例表

ID  STOREA  STOREB  STOREC   AB   BC   CA  ABC
--- ------- ------  -------  --   --  ---  ---
10    1       0       0
10    0       1       0
10    0       1       0
29    0       1       0 
29    0       0       1
29    1       0       0      

每一行对应于在商店 A 或 B 或 C 进行的购买。客户 10 在 A 和 B 商店但不是 c。所以我想要AB=1 BC=0 CA=0 ABC=0 用于所有 ID=10 的行,而对于 ID=29,他在所有 3 处购物,所以我需要AB=1 BC=1 CA=1 ABC=1 用于 ID=29 的所有行(使用 ORACLE SQL)

我想更新表格中的列。

【问题讨论】:

  • 你能规范化这张表吗?
  • 如果您不理解@PolishPrince 的评论,我听说过《为凡人设计数据库》这本书的好消息。

标签: sql oracle oracle-sqldeveloper


【解决方案1】:

这是您可以做到这一点的一种方法。我认为您不能在 Oracle 中将 JOINsUPDATE 语句一起使用——但是,您可以通过使用 MERGE 来完成同样的事情:

MERGE
INTO    yourtable
USING   (
          select id as idnew, 
            case when a + b = 2 then 1 else 0 end abnew,
            case when b + c = 2 then 1 else 0 end bcnew,
            case when a + c = 2 then 1 else 0 end acnew,
            case when a + b + c = 3 then 1 else 0 end abcnew
          from (
            select 
              id,
              max(case storea when 1 then 1 else 0 end)  A,
              max(case storeb when 1 then 1 else 0 end)  B,
              max(case storec when 1 then 1 else 0 end)  C
            from yourtable
            group by id
          ) a
        )
ON      (id = idnew)
WHEN MATCHED THEN
UPDATE
SET     ab = abnew, 
  bc = bcnew,
  ac = acnew,
  abc = abcnew

SQL Fiddle Demo

【讨论】:

  • 谢谢,它成功了。你能告诉我什么是 A,B,C 和 a,b,c 以及按 id 分组后的另一个 'a'。
  • @user2133404 -- 所有这些都是别名。 group by 后面的小 a 是表别名(使用子查询时需要)。其他只是列别名,其中 A 与 a 相同 - 应该更加一致。很高兴我们能提供帮助!
  • 再次非常感谢 :)
【解决方案2】:

以下是您作为select 执行此操作的方法:

update (select id, storea, storeb, storec, AB as new_AB, BC as new_BC, AC as new_AC, ABC as new_ABC
        from t join
             (select id,
                     (case when max(storeA) = 1 and max(storeB) = 1 then 1 else 0 end) as AB,
                     (case when max(storeB) = 1 and max(storeC) = 1 then 1 else 0 end) as BC,
                     (case when max(storeA) = 1 and max(storeC) = 1 then 1 else 0 end) as AC,
                     (case when max(storeA) = 1 and max(storeB) = 1 and max(storeC) = 1 then 1 else 0 end) as ABC
              from t
              group by id
             ) tsum
             on t.id = tsum.id
            )
     set AB = new_AB, AC = new_AC, BC = new_BC, ABC = new_ABC;

我认为这可能有效:

select id, storea, storeb, storec, AB, BC, AC, ABC
from t join
     (select id,
             (case when max(storeA) = 1 and max(storeB) = 1 then 1 else 0 end) as AB,
             (case when max(storeB) = 1 and max(storeC) = 1 then 1 else 0 end) as BC,
             (case when max(storeA) = 1 and max(storeC) = 1 then 1 else 0 end) as AC,
             (case when max(storeA) = 1 and max(storeB) = 1 and max(storeC) = 1 then 1 else 0 end) as ABC
      from t
      group by id
     ) tsum
     on t.id = tsum.id
            )
     set AB = new_AB, AC = new_AC, BC = new_BC, ABC = new_ABC;

【讨论】:

  • 可以编写更新查询。但是,最好在尝试之前知道您要做什么。你没有把那部分解释得很清楚。
  • 我正在尝试根据客户 ID 和他的商店访问更新表中的列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2021-09-27
  • 2020-02-08
  • 1970-01-01
相关资源
最近更新 更多