LEFT join仅返回右侧的NULL

时间:2016-09-12 19:13:26

标签: sql sql-server tsql sql-server-2012 left-join

我遇到了animate查询有两个条件的问题。问题是查询只返回左侧站点,而右侧站点只包含join。我检查了数据的类型是否匹配。可能出现什么问题?

NULL

编辑:表格如何显示的简短示例。 STOCK_RIC列目前的类型为char(13)

SELECT t2.YEAR_NUMBER
    ,t2.WEEK_NUMBER
    ,t2.STOCK_RIC
    ,t1.SENT_POS_WEIGHTED
    ,t1.SENT_NEUT_WEIGHTED
    ,t1.SENT_NEG_WEIGHTED
    ,t1.OBSERVATIONS
    ,t1.AV_W_RELEVANCE
    ,t1.AV_W_LNKD_CNT1
    ,t1.AV_W_ITEM_CNT1
INTO res
FROM Structure2016 t2 left JOIN
     pre t1
     ON t1.WEEK_NUMBER = t2.WEEK_NUMBER AND t1.STOCK_RIC = t2.STOCK_RIC
ORDER BY STOCK_RIC, WEEK_NUMBER

2 个答案:

答案 0 :(得分:0)

它应该可以正常工作,我构建了你提供的样本,结果是

STOCK_RIC   WEEK_NUMBER SENT_POS_WEIGHTED
JPM.N         1               0.1
JPM.N         2               NULL
JPM.N         3               0.9

我使用的代码是

declare @table table(
     STOCK_RIC varchar(10),
     YEAR int,
     WEEK_NUMBER int
  )
 declare @pre table(
    STOCK_RIC varchar(10),
    WEEK_NUMBER int,
    SENT_POS_WEIGHTED decimal(3,1)
  )

  insert into @table(STOCK_RIC,YEAR,WEEK_NUMBER)
  select 'JPM.N',2016,1
  union
  select 'JPM.N',2016,2
  union
  select 'JPM.N',2016,3 

  insert into @pre(STOCK_RIC,WEEK_NUMBER,SENT_POS_WEIGHTED)
  select 'JPM.N',5,0.1
  union
  select 'JPM.N',3,0.9
  union
  select 'JPM.N',1,0.1
  union
  select 'JPM.N',10 ,0.1


  select t.STOCK_RIC,t.WEEK_NUMBER,p.SENT_POS_WEIGHTED
  from @table t
  left join @pre p on t.STOCK_RIC = p.STOCK_RIC and t.WEEK_NUMBER = p.WEEK_NUMBER

似乎问题不在您发布的代码中

答案 1 :(得分:0)

我能告诉它的唯一一件事就是你的bulk_create条款会引发错误。您需要指定order byt.STOCK_RIC, t.WEEK_NUMBER