外连接到 select

outer join to select

我有 select 输出 2 列、区域和计数。在另一个 table (site_to_site_address) 中,我有完整的区域列表,我需要用完整列表加入列,但保存 COUNT 条件,如果计数 < 0,它应该在前面 space地区的,如果计数> 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;

我不知道你所说的 "if count < 0, it should be space in front of region" 是什么意思。 Count() 永远不能 return 负数。

如果你想要所有地区,你的逻辑是落后的。使用 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;

注:

  • 如果你想保留所有的东西,那么把table放在第一位,然后使用left join
  • 不要混合使用新旧样式的连接语法。我想我修复了子查询,但我不是 100% 确定。
  • order by 是子查询没用。
  • 您的查询可能比完成您想要执行的操作所需的复杂。但如果没有样本数据和预期结果,就很难提出更多建议。考虑询问 另一个 带有示例数据和预期结果的问题,看看是否有更简单的方法来做到这一点。
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;