EXTRACT 函数在长查询中的位置

EXTRACT function position in long query

我想请教以下方面的一些专业知识。我对 SQL 很陌生,所以请原谅任何错误或不正确之处。我写了下面的SQL,它可以满足我现在的需要:

SELECT a.TICKET,
       f.CODE,
       f.SEQUENCE,
       f.CLEARSEQUENCE,
       f.TOTALMINUTES,
       a.SEV,
       a.ORIGSEV,
       a.CUSTOMER,
       a.WORKGROUP,
       a.NOC,
       d.COUNTRYA,
       d.COUNTRYZ,
       a.IDENTIFIER,
       f.ORIGDTTM,
FROM   SYSTEMACTV.T3SEVHEADER a
       LEFT OUTER JOIN SYSTEMACTV.T3SEVCUSTOMER d ON a.TICKET=d.TICKET 
       LEFT OUTER JOIN SYSTEMACTV.T3SEVCODEDATA f ON a.TICKET=f.TICKET
WHERE  a.CLOSEDDT >= to_timestamp('2021-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND    a.WORKGROUP IN ('SVCDSK.DES-INTL')
AND    a.SEV in (1,2,3,4)
AND    d.COUNTRYA IN ('CHN','JPN','HKG','GUM','IDN','PRK','KOR','MYS','MMR','NZL','PHL','SGP','TWN','THA','VNM')

我现在需要从 f.ORIGDTTM 字段中提取 HOURMONTHDAYDAY OF WEEK。我仅使用基本查询就设法使它按如下方式工作:

SELECT EXTRACT(hour FROM ORIGDTTM) "HOUR",
       EXTRACT(month FROM ORIGDTTM) "MONTH"
FROM   ETMSACTV.T3TKTNADDATA;

我的问题是如何将 EXTRACT 函数添加到长而复杂的查询中。我试过把它放在很多地方都没有结果。

将它们添加到长查询中,就像将它们添加到短查询中一样。

SELECT a.ticket,
       f.code,
       f.sequence,
       f.clearsequence,
       f.totalminutes,
       a.sev,
       a.origsev,
       a.customer,
       a.workgroup,
       a.noc,
       d.countrya,
       d.countryz,
       a.identifier,
       f.origdttm,
       --
       EXTRACT (HOUR FROM f.origdttm) "HOUR",         --> here
       EXTRACT (MONTH FROM f.origdttm) "MONTH"        --> here
  FROM systemactv.t3sevheader a
       LEFT OUTER JOIN systemactv.t3sevcustomer d ON a.ticket = d.ticket
       LEFT OUTER JOIN systemactv.t3sevcodedata f ON a.ticket = f.ticket
 WHERE     a.closeddt >=
           TO_TIMESTAMP ('2021-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
       AND a.workgroup IN ('SVCDSK.DES-INTL')
       AND a.sev IN (1,
                     2,
                     3,
                     4)
       AND d.countrya IN ('CHN',
                          'JPN',
                          'HKG',
                          'GUM',
                          'IDN',
                          'PRK',
                          'KOR',
                          'MYS',
                          'MMR',
                          'NZL',
                          'PHL',
                          'SGP',
                          'TWN',
                          'THA',
                          'VNM')

只需将它们添加到查询中即可:

SELECT a.TICKET,
       f.CODE,
       f.SEQUENCE,
       f.CLEARSEQUENCE,
       f.TOTALMINUTES,
       a.SEV,
       a.ORIGSEV,
       a.CUSTOMER,
       a.WORKGROUP,
       a.NOC,
       d.COUNTRYA,
       d.COUNTRYZ,
       a.IDENTIFIER,
       f.ORIGDTTM,
       EXTRACT(MONTH FROM f.ORIGDTTM) AS month,
       EXTRACT(DAY   FROM f.ORIGDTTM) AS day,
       EXTRACT(HOUR  FROM f.ORIGDTTM) AS hour,
       TRUNC(f.ORIGDTTM) - TRUNC(f.ORIGDTTM, 'IW') AS day_of_week,
         -- monday = 0, ..., sunday = 6
       TO_CHAR(f.ORIGDTTM, 'DY', 'NLS_DATE_LANGUAGE=English') AS day_of_week_alt
FROM   SYSTEMACTV.T3SEVHEADER a
       LEFT OUTER JOIN SYSTEMACTV.T3SEVCUSTOMER d ON a.TICKET=d.TICKET 
       LEFT OUTER JOIN SYSTEMACTV.T3SEVCODEDATA f ON a.TICKET=f.TICKET
WHERE  a.CLOSEDDT >= TIMESTAMP '2021-01-01 00:00:00'
AND    a.WORKGROUP IN ('SVCDSK.DES-INTL')
AND    a.SEV in (1,2,3,4)
AND    d.COUNTRYA IN ('CHN','JPN','HKG','GUM','IDN','PRK','KOR','MYS','MMR','NZL','PHL','SGP','TWN','THA','VNM')

db<>fiddle here