当前位置: 首页 > 面试题库 >

在SQL中加入帮助

郝永思
2023-03-14
问题内容

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

Query:

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

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


问题答案:

这是您不希望做的,只是发布,因为您询问了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

A full outer join between these tables on an impossible condition means you
end up with a resultset like:

t1.client_id t2.client_id
9908         NULL
NULL         9908

The COALESCE brings the split-ness of it back together:

client_id
9908
9908

如前所述,不这样做-与联合相比,这浪费了数据库的时间和资源。我纯粹是作为一个示例来说明如何
使用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

Outer joins preserve the row from the table even if there is no match:

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返回第一个非null参数,因此,如果我们使用COALESCE(a_column,b_column),它将这两列(其中一个为null)折叠为一列:

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


 类似资料:
  • 问题内容: 我正在尝试从文件中加载数据,并且想将CREATED_DATE和UPDATED_DATE设置为SYSDATE并将CREATE_BY和UPDATED_BY设置为USER 这是我正在使用的表: 这是数据文件: 和我的控制文件: 当我尝试使用SQL Loader加载它时…所有记录均被拒绝: 有什么想法我做错了吗?提前致谢。 问题答案: 就像Oracle所说的那样:添加到您的控制文件中: 这是必

  • 问题内容: 怀疑在VBA ADO和Sql查询中… 我有2张纸,即adodc1,adodc2(在一本工作簿中) 在adodc1中具有“名称”,“部门”列,有时其具有“ Sect”列 在adodc2中具有“名称”,“部门”,“宗派”列 我想要的是当我运行Query..Vba时需要检查adodc1是否具有Sect列。 要返回为空值.. 下面的代码取自“”,根据我的需要进行了更改 它将执行的工作是来自两张

  • 请帮帮我,对于连接,学习SQL和连接是痛苦的:( 我有表:类别,列名称:name,ID 我有列名称为ID的table:sub_类别 我有列名称为category\u id的表:provider\u services 当我在laravel视图中执行此操作时,刀片

  • 我正在尝试将文件插入到现有表中。现有的表有3列,这些列是ID(在表中分配)、学生号和组号。 在我的中,我有下面的格式,但是每次插入它时,我都会得到一个错误

  • 我有3张桌子 许可证中的CREATED_AT是一个日期(非空)字段。 表根据同名主键/外键进行关联;客户可以拥有0个或多个许可证,每个许可证都有一个版本。 我想从这些表格中得到: 客户的名字、姓氏和创建的最后一个许可证的release\u id(根据许可证中的created\u AT字段查找最后一个),如果有。 对于这个问题,我使用了以下查询: 这似乎有效,但我问是否有人可以证实我这一点,或者是否

  • 问题内容: 好的,可以说我要插入100行,每行大约有150列(我知道听起来很多列,但是我需要将此数据存储在一个表中)。插入将随机发生(即,每当一组用户决定上载包含数据的文件时),每月大约20次。但是,数据库将在连续负载下处理大型企业应用程序的其他功能。这些列是varchars,int以及其他各种类型。 将这些插入物包装在一个事务中(而不是一次运行一次)的性能增益会变大,变小还是介于两者之间? 为什