简体   繁体   English

Oracle-需要对查询进行一些更改

[英]Oracle- Need some changes to the Query

I have the below Query.我有以下查询。 My Expected output would be as below.我的预期 output 如下所示。 Please help me make changes to the Query请帮我更改查询

select 
ID,TERM,
case
  when TERM  like '____1_' then
     function_toget_hrs(ID, TERM,sysdate) else null
    end fall_hours,
 case 
   when TERM  like '____2_' then
      function_toget_hrs(ID, TERM,sysdate) else null
    end winter_hours
  
    from TABLE_TERM
    where ID='12087762'
    

实际结果

Expecting one row for each ID.期望每个 ID 一行。 Please help me the ways请帮助我的方式

Pivoting is what you need:旋转是你需要的:

WITH TABLE_TERM AS
(
  SELECT 12087762 AS ID, '202110____1_' AS term, 12 AS func FROM dual UNION ALL
  SELECT 12087762 AS ID, '202120____2_' AS term, 16 FROM dual UNION ALL
  SELECT 12087762 AS ID, '202140____1_' AS term, 0  FROM dual 
)
SELECT *
  FROM (SELECT ID
             , DECODE(SUBSTR(term,-6),'____1_','fall_hours','winter_hours') AS hrs
             , func --function_toget_hrs(ID, TERM,sysdate) for test purposes
          FROM TABLE_TERM
         WHERE ID =  '12087762'
        )
  PIVOT (SUM(func) FOR hrs IN ('fall_hours','winter_hours'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM