tsql join没有拉出正确的行

时间:2015-09-16 00:16:11

标签: sql tsql join report

我一直在试图解决这个问题,试图解决这个问题。我正在路障,希望有人可以提供帮助。

我有两个表service_calls和service_code_description。

service_calls包含service_codes,对于每个服务调用,包括service_code_reported和service_code_actual。 这两者都与service_code_description表中的service_code和description相符。

这是我的问题所在,我只能提取一个描述,而不是两者。 下面是我在SQL Report Builder中使用的代码示例。

    SELECT SC.[system_code]
      ,SC.[node_id]
      ,SC.[service_call_num]
      ,SC.[subs_acct_num]
      ,SC.[drop_address]
      ,SC.[city_code]
      ,SC.[province_code]
      ,SC.[head_end]
      ,(cast(SC.[service_code_reported] as varchar(4)) +char(10)+ d.service_code_description) [service_code_reported]
      ,(cast(SC.[service_code_actual1] as varchar(4)) +char(10)+ d.service_code_description) [service_code_actual1]
      ,SC.[service_code_actual2]
      ,SC.[service_code_actual3]
      ,SC.[service_code_actual4]
      ,SC.[completed_date]
      ,SC.[startdate]
      ,SC.[enddate]
  FROM
    [SC_Data] SC
INNER JOIN [lkup_Service_Code_Definition] d
ON 
    sc.system_code = d.system_code
WHERE
    year(startdate)=@year
and
    month(startdate)=@month
and
    node_id=@node_id

输出以下内容。

sample_output

应该输出的是 服务代码已报告 004全面停电 - 所有服务

服务代码实际1 131取消卡车后叫车

如果有人在正确的方向上有任何提示,我会非常满意地提供反馈。

2 个答案:

答案 0 :(得分:2)

在没有看到任何其他内容的情况下,似乎您对报告的和实际的动态列使用了相同的service_code_description源(d.service_code_description)。您正在加入sc.system_code = d.system_code上的lkup_Service_Code_Definition设置,但是在您报告的/实际动态字段中,您引用的是service_code_reported和service_code_actual1值(而不是system_code(s))。

我想冒昧地猜测你需要加入lkup_Service_Code_Definition两次 - 一次用于报告的代码,一次用于实际(尽管这假设service_code_reported和service_code_actual1字段中的值应该映射)到该集合的system_code值),如:

select ...
        ,(cast(SC.[service_code_reported] as varchar(4)) +char(10)+ rd.service_code_description) [service_code_reported]
      ,(cast(SC.[service_code_actual1] as varchar(4)) +char(10)+ ad.service_code_description) [service_code_actual1]
       ....
from SC_Data sc
join lkup_Service_Code_Definition rd
on sc.service_code_reported = rd.system_code
join lkup_Service_Code_Definition ad
on sc.service_code_actual1 = ad.system_code
where  ...

答案 1 :(得分:1)

我认为这可行;

    SELECT SC.[system_code]
          ,SC.[node_id]
          ,SC.[service_call_num]
          ,SC.[subs_acct_num]
          ,SC.[drop_address]
          ,SC.[city_code]
          ,SC.[province_code]
          ,SC.[head_end]
          ,(cast(SC.[service_code_reported] as varchar(4)) +char(10)+ d.service_code_description) [service_code_reported]
          ,(cast(SC.[service_code_actual1] as varchar(4)) +char(10)+ e.service_code_description) [service_code_actual1]
          ,SC.[service_code_actual2]
          ,SC.[service_code_actual3]
          ,SC.[service_code_actual4]
          ,SC.[completed_date]
          ,SC.[startdate]
          ,SC.[enddate]
      FROM
        [SC_Data] SC, [lkup_Service_Code_Definition] d,[lkup_Service_Code_Definition] e    
  WHERE
    d.system_code = SC.service_code_reported
    and 
    e.system_code = SC.service_code_actual1
    and
        year(startdate)=@year
    and
        month(startdate)=@month
    and
        node_id=@node_id
相关问题