使用Oracle在单个查询中显示基于列的计算长度

u0sqgete  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(105)

我有2个查询,显示了2个不同的数据。
例如:一个查询具有IN条件,显示UG_LENGTH,另一个查询具有NOT IN条件,显示AR_LENGTH。我希望在单个查询中同时显示UG_LENGTHAR_LENGTH
下图是两者的查询
在查询

select RJ_FSA_ID, ROUND(SUM(NVL(CALCULATED_LENGTH,0)/1000), 4) AS UG_LENGTH
from NE.MV_SPAN@facid147
where SPAN_REF_NAME IN ('3x40-F-RG-ST-1L','2x40-F-G-ST-1L','1x40-F-G-ST-1L','2x40-F-RG-ST-1L','7x14MM-MICRO','1x14MM-MICRO',
    '3x40-F-G-ST-1L','2x14MM-MICRO','1x40-F-1L-RED','4x40-F-RG-ST-1L') 
and RJ_FSA_ID='MUMB1622'
group by RJ_FSA_ID;

字符串
不在查询中

select RJ_FSA_ID, ROUND(SUM(NVL(CALCULATED_LENGTH,0)/1000), 4) AS AR_LENGTH
from NE.MV_SPAN@facid147
where SPAN_REF_NAME NOT IN ('3x40-F-RG-ST-1L','2x40-F-G-ST-1L','1x40-F-G-ST-1L','2x40-F-RG-ST-1L','7x14MM-MICRO','1x14MM-MICRO',
    '3x40-F-G-ST-1L','2x14MM-MICRO','1x40-F-1L-RED','4x40-F-RG-ST-1L') 
and RJ_FSA_ID='MUMB1622'
group by RJ_FSA_ID;


如何合并这两个查询?

hvvq6cgz

hvvq6cgz1#

使用条件聚合:

select RJ_FSA_ID,
       ROUND(
         SUM(
           CASE
           WHEN SPAN_REF_NAME IN ('3x40-F-RG-ST-1L','2x40-F-G-ST-1L','1x40-F-G-ST-1L','2x40-F-RG-ST-1L','7x14MM-MICRO','1x14MM-MICRO','3x40-F-G-ST-1L','2x14MM-MICRO','1x40-F-1L-RED','4x40-F-RG-ST-1L')
           THEN NVL(CALCULATED_LENGTH,0)/1000
           END
         ),
         4
       ) AS UG_LENGTH,
       ROUND(
         SUM(
           CASE
           WHEN SPAN_REF_NAME NOT IN ('3x40-F-RG-ST-1L','2x40-F-G-ST-1L','1x40-F-G-ST-1L','2x40-F-RG-ST-1L','7x14MM-MICRO','1x14MM-MICRO','3x40-F-G-ST-1L','2x14MM-MICRO','1x40-F-1L-RED','4x40-F-RG-ST-1L')
           THEN NVL(CALCULATED_LENGTH,0)/1000
           END
         ),
         4
       ) AS AR_LENGTH
from   NE.MV_SPAN@facid147
where  RJ_FSA_ID='MUMB1622'
group by RJ_FSA_ID;

字符串

相关问题