外连接选择

时间:2015-02-13 08:48:43

标签: sql oracle outer-join

我选择输出2列,区域和计数。在另一个表(site_to_site_address)中,我有完整的区域列表,我需要使用完整列表加入列,但是如果count<可以保存COUNT条件。 0,它应该是区域前面的空间,如果计数> 0,应该是一些数字。

Select Reg As Region, 
       Count (Reg) As Sleeping  
From 
     (Select DECODE_SITE_TO_REGION(sel1.BSC_NUMBER,sel1.segment_id) as reg
      from 
           (Select * 
            From Re.Bsc_Counter_2_Stat_Kpi
            where TRF_202>0.5 and period_real_start_time >= trunc(sysdate-1/24, 'HH') 
            order by PERIOD_REAL_START_TIME desc
           ) sel1,
           (Select * 
            From Re.Bsc_Counter_79_Stat_Kpi
            Where Period_Real_Start_Time >= Trunc(Sysdate-1/24, 'HH') 
            Order By Period_Real_Start_Time Desc
           ) Sel2
      where sel1.BSC_NUMBER=sel2.BSC_NUMBER(+)
        and sel1.SEGMENT_ID=sel2.SEGMENT_ID(+)
        and sel1.PERIOD_REAL_START_TIME=sel2.PERIOD_REAL_START_TIME(+)
        and (Sel2.Trf_215a=0 Or Sel2.Trf_215a Is Null)
      Order By Sel1.Period_Real_Start_Time Desc,
               Sel1.Bsc_Number,
               Sel1.Segment_Id
     )
Group By Reg 
order by sleeping desc;

我尝试按标记加入,​​但无法正常使用。我没有得到完整的地区名单。

Select s1.Reg As Region, 
       Count (Reg) As Sleeping  
From 
       (Select DECODE_SITE_TO_REGION(sel1.BSC_NUMBER,sel1.segment_id) as reg
        from
               (Select * 
                From Re.Bsc_Counter_2_Stat_Kpi
                where TRF_202>0.5 and period_real_start_time >= trunc(sysdate-1/24, 'HH') 
                order by PERIOD_REAL_START_TIME desc
               ) sel1,
               (Select * 
                From Re.Bsc_Counter_79_Stat_Kpi
                Where Period_Real_Start_Time >= Trunc(Sysdate-1/24, 'HH') 
                Order By Period_Real_Start_Time Desc
               ) Sel2
          where sel1.BSC_NUMBER=sel2.BSC_NUMBER(+)
            and sel1.SEGMENT_ID=sel2.SEGMENT_ID(+)
            and sel1.PERIOD_REAL_START_TIME=sel2.PERIOD_REAL_START_TIME(+)
            and (Sel2.Trf_215a=0 Or Sel2.Trf_215a Is Null)
       Order By Sel1.Period_Real_Start_Time Desc,
                Sel1.Bsc_Number,
                Sel1.Segment_Id
       ) S1
Left Outer Join (Select Distinct Site_Region 
                 From Site_To_Site_Address
                ) S2
             on s1.reg=s2.site_region
Group By Reg 
order by sleeping desc;

2 个答案:

答案 0 :(得分:0)

我不明白你的意思"如果计数< 0,它应该是区域前面的空间"。 Count()永远不会返回负数。

如果你想要所有地区,你的逻辑就是落后的。使用right outer join或将您想要保留的列表放在第一位:

Select s2.site_region As Region, 
       Count(s1.Reg) As Sleeping  
From (select Distinct Site_Region 
      From Site_To_Site_Address
     ) regions left join
     (Select DECODE_SITE_TO_REGION(sel1.BSC_NUMBER,sel1.segment_id) as reg
      from Re.Bsc_Counter_2_Stat_Kpi sel1 left join
           Re.Bsc_Counter_79_Stat_Kpi sel2
           on sel1.BSC_NUMBER = sel2.BSC_NUMBER and
              sel1.SEGMENT_ID=sel2.SEGMENT_ID and
              sel1.PERIOD_REAL_START_TIME=sel2.PERIOD_REAL_START_TIME and
              Sel2.Trf_215a = 0 and
              sel2.Period_Real_Start_Time >= Trunc(Sysdate-1/24, 'HH')
          where sel1.TRF_202 > 0.5 and
                sel1.period_real_start_time >= trunc(sysdate-1/24, 'HH') and
       ) S1
       on s1.reg = s2.site_region
Group By s2.Reg 
order by sleeping desc;

注意:

  • 如果您想要保留所有内容,请先将该表放在首位并使用left join
  • 不要混合旧式和新式的连接语法。我想我修正了子查询,但我并不是100%肯定。
  • order by子查询没用。
  • 您的查询可能比完成您想要做的事情更复杂。但如果没有样本数据和预期结果,很难再提出任何建议。考虑使用示例数据和所需结果询问另一个问题,看看是否有更简单的方法来执行此操作。

答案 1 :(得分:0)

Select s2.site_region As Region, 
       Count (Reg) As Sleeping  
From 
       (Select DECODE_SITE_TO_REGION(sel1.BSC_NUMBER,sel1.segment_id) as reg
        from
               (Select * 
                From Re.Bsc_Counter_2_Stat_Kpi
                where TRF_202>0.5 and period_real_start_time >= trunc(sysdate-1/24, 'HH') 
                order by PERIOD_REAL_START_TIME desc
               ) sel1,
               (Select * 
                From Re.Bsc_Counter_79_Stat_Kpi
                Where Period_Real_Start_Time >= Trunc(Sysdate-1/24, 'HH') 
                Order By Period_Real_Start_Time Desc
               ) Sel2
          where sel1.BSC_NUMBER=sel2.BSC_NUMBER(+)
            and sel1.SEGMENT_ID=sel2.SEGMENT_ID(+)
            and sel1.PERIOD_REAL_START_TIME=sel2.PERIOD_REAL_START_TIME(+)
            and (Sel2.Trf_215a=0 Or Sel2.Trf_215a Is Null)
       Order By Sel1.Period_Real_Start_Time Desc,
                Sel1.Bsc_Number,
                Sel1.Segment_Id
       ) S1
RIGHT Outer Join (Select Distinct Site_Region 
                 From Site_To_Site_Address
                ) S2
             on s1.reg=s2.site_region
Group By s2.site_region
order by sleeping desc;