【问题标题】:Join 2 tables with classification tag in one column在一列中加入 2 个带有分类标签的表
【发布时间】:2021-11-13 03:32:37
【问题描述】:

我在 Oracle 中有一个 SQL 表。我必须加入 2 个表(一个带有 ID 和其他数据,第二个带有 ID 和每行中的日期,第二个表显示在下面)但不想从第二个表中获取连接数据,而只需要标签(根据条件为 1 或 0搜索 ID 的日期在 21/08/01 到 21/08/31 之间)。

Table 2 

#    ID      Date            
#    1       21/08/01                           
#    2       21/07/02                           
#    3       21/08/24                        
#    4       21/08/02              
#    5       21/06/03                         
#    6       21/08/05        
#    7       21/08/26                           
#    8       21/08/05       

我想要的输出应该是这样的:

Table 1
#    ID      Date   Other data         
#    1       1      ....                     
#    2       0      ....                       
#    3       1      ....                    
#    4       1      ....          
#    5       0      ....                     
#    6       1      ....   
#    7       1      ....                       
#    8       1      ....       

有人可以帮忙吗?

【问题讨论】:

    标签: sql oracle join


    【解决方案1】:

    如果table2中只有一个id,可以使用left join

    select t`.*,
           (case when t2.date is null then 0 else 1 end) as flag
    from table1 t1 left join
         table2 t2
         on t2.id = t1.id and
            t2.date between date '2021-08-01' and date '2021-08-31;
    

    适用于table2 中多个匹配项的更通用解决方案是exists

    select t.*,
           (case when exists (select 1
                              from table2 t2
                              where t2.id = t1.id and
                                    t2.date between date '2021-08-01' and date '2021-08-31'
                             )
                 then 1 else 0
             end) as flag
    from table1 t1 
    

    【讨论】:

      【解决方案2】:

      在我看来像CASE

      with temp_2 as
        (select b.id,
                b.name,
                b.address,
                b.date_column,
                rank() over (partition by b.id order by b.date_column desc) rnk
         from table_2 b
        )  
      select 
        a.id,
        a.some_other_column,
        --
        case when b.date_column between date '2021-08-01' and date '2021-08-31' then 1
             else 0
        end as tag
      from table_1 a left join temp_2 b on a.id = b.id and b.rnk = 1
      

      【讨论】:

      • 我刚刚在上面的 cmets 中发布了我的代码。您是否发现任何错误?
      • 其中不少。第 3 行末尾缺少逗号,“LEFT OUTER JOIN”(不是“JOIN OUTER LEFT”),CASE 语法无效,DATE 文字的日期格式无效(必须是 yyyy-mm-dd),错误放置的 TAG 列(不是在“AND”中,但不是第 4 行中的 TAG,OTHER_DATA 很可能不是单列,而是多列。请参阅我编辑的答案。
      • 谢谢,这在我的帖子中可以正常工作,但我还有另一个问题,因为在表 2 中我有多个相同的 id,我只想在 Table1 中的每个 id 中放置具有最旧日期的行。你对这个问题有什么提示吗?
      • 一种选择是按 DATE_COLUMN 以降序排列 table_2 行(按 ID 分区),然后仅获取排名为“最高”(即等于 1)的行。查看编辑后的代码。
      • 感谢您的帮助。它有效,但仅当每个 ID 没有多个相同的日期时。我使用来自 Gordon 的示例,它帮助我为每个 id 选择一个值。谢谢你的代码,它对我也很有用。
      猜你喜欢
      • 2022-09-23
      • 2015-05-04
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多