【问题标题】:Join help in SQL在 SQL 中加入帮助
【发布时间】:2019-01-07 15:32:07
【问题描述】:

我有 2 个非常简单的表要加入,但我错过了某个地方,因为我没有得到想要的输出:

表 #1:

表 #2:

期望的输出:

查询:

create table #temp1
(
     client_id int, 
     identifier int, 
     pp_id int,
     ch_id int,
     code varchar(20),
     program varchar(20),
     startdate date,
     enddate date, 
     ssc varchar(50)
)

insert into #temp1
values (9908,789654123,1567,1566,'OP','xASMT','1/1/2019','1/4/2019','A201901044F010134NNN01D               151 143 093 ')

create table #temp2
(
     client_id int, 
     identifier int, 
     pp_id int,
     ch_id int,
     code varchar(20), 
     program varchar(20),
     startdate date,
     enddate date, 
     ssc varchar(20)
)

insert into #temp2
values(9908,789654123,1574,1573,'OP','SU1','1/1/2019','1/4/2019',NULL)

--My query:
select 
    t1.client_id, t1.identifier, 
    concat(t1.code, t1.startdate, t1.enddate, t1.ssc),
    concat(t2.code, t2.startdate, t2.enddate, t2.ssc)
from 
    #temp1 t1
left join 
    #temp2 t2 on t1.client_id = t2.client_id and t1.identifier = t2.identifier

我仍然是一个学习者,如果这里有任何错误,请原谅我。有什么帮助吗?!

【问题讨论】:

  • 问题是什么,你得到了什么问题?
  • 注意:如果任何值为 null 并且您构建了一个字符串,它将使整个字符串为 null
  • 我的查询没有给我想要的输出。我需要 2 行记录
  • 我只是连接日期和 SSC。如果有任何内容为 null,则将报告为 null
  • @rick UNION 使结果集垂直增长,JOIN 使它们水平增长。你已经明确表示你想要垂直增长;你为什么要求加入?

标签: sql sql-server join union


【解决方案1】:

我认为你不需要加入,但你需要联合:

select t1.client_id, t1.identifier, CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc)
from #temp1 t1
union all
select t2.client_id, t2.identifier, CONCAT(t2.code,t2.startdate,t2.enddate,t2.ssc)
from #temp2 t2

也许您需要一个where 部分来限制某些行的结果。
demo

【讨论】:

    【解决方案2】:

    这是您不会做的事情,只是因为您询问了 JOIN 而发帖。这绝对是错误的方式,但是:

    select 
      COALESCE(t1.client_id, t2.client_id) client_id,
      COALESCE(t1.identifier, t2.identifier) identifier,
      COALESCE(
        CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc),  
        concat(t2.code,t2.startdate,t2.enddate,t2.ssc)
      )
    from 
      #temp1 t1
      full outer join 
      #temp2 t2 
      on 0 = 1
    

    在不可能的情况下,这些表之间的完全外连接意味着您最终会得到如下结果集:

    t1.client_id t2.client_id
    9908         NULL
    NULL         9908
    

    COALESCE 将它的分裂性重新组合在一起:

    client_id
    9908
    9908
    

    如前所述,不要这样做 - 与 union 相比,这会极大地浪费数据库的时间和资源;我纯粹将它作为一个示例来说明如何使用 JOIN 实现结果集的垂直增长,同时也有助于您理解 db 理论和操作:

    A UNION B (number is id)
    
    Results grow vertically:
    A1
    A2
    B1
    B2
    
    A JOIN B (number is id)
    
    Results grow horizontally:
    A1 B1
    A2 B2
    

    即使没有匹配,外连接也会保留表中的行:

    A OUTER JOIN B
    
    Results:
    A1   null
    null B2
    

    通过使连接变得不可能,完全外连接将导致结果集水平和垂直增长:

    A OUTER JOIN B ON 1 = 0
    
    Results:
    A1   null
    A2   null
    null B1
    null B2
    

    COALESCE 返回第一个非空参数,因此如果我们 COALESCE(a_column, b_column) 它将这两列(其中一个为空)折叠成一列:

    acol bcol  COALESCE(acol, bcol)  result
    ----|-----|--------------------|--------
    A1   null  COALESCE(A1, null)   -> A1
    null B2    COALESCE(null, B1)   -> B1
    

    【讨论】:

      【解决方案3】:

      您似乎在寻找的是 UNION 选择构造,而不是连接。

      UNION 从一个子查询返回行并从另一个子查询追加行

      你的例子

      select t1.client_id, t1.identifier, CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc
      from #temp1 t1 
      where ... some condition ...
      union all 
      select t2.client_id, t2.identifier, concat(t2.code,t2.startdate,t2.enddate,t2.ssc)
      from #temp2 t2 
      where ... some condition ...
      

      在此处查看文档:UNION

      【讨论】:

        【解决方案4】:

        关于您想要的结果,我知道您需要使用 UNION ALL 而不是加入表格。

        请检查脚本:

        SELECT
            t1.client_id
           ,t1.identifier
           ,CONCAT(t1.code, t1.startdate, t1.enddate, t1.ssc)
           --,CONCAT(t2.code, t2.startdate, t2.enddate, t2.ssc)
        FROM #temp1 t1
        UNION ALL
        SELECT 
            t2.client_id
           ,T2.identifier
           ,CONCAT(t2.code, t2.startdate, t2.enddate, t2.ssc)
        FROM  #temp2 t2
        

        【讨论】:

          【解决方案5】:

          这是我用来获得该输出的内容:

          select 
              t1.client_id, 
              t1.identifier, 
              CONCAT(t1.code,t1.startdate,t1.enddate,t1.ssc) ssc_concatenated
              --concat(t2.code,t2.startdate,t2.enddate,t2.ssc)
          
          from #temp1 t1
          
          union all
          
          select 
              t2.client_id, 
              t2.identifier, 
              concat(t2.code,t2.startdate,t2.enddate,t2.ssc) ssc_concatenated
          from #temp2 t2
          

          这是数据库小提琴demo

          【讨论】:

            猜你喜欢
            • 2011-11-06
            • 1970-01-01
            • 2020-08-22
            • 1970-01-01
            • 2023-04-09
            • 1970-01-01
            • 2011-07-21
            • 2011-10-12
            • 1970-01-01
            相关资源
            最近更新 更多