按月分区两个日期

时间:2018-11-08 18:22:53

标签: sql oracle

当结束日期的月份与开始日期的月份不同时,我需要使用Oracle 11gXE中的SQL在两个日期之间划分范围。 例如:


kubectl logs --namespace=kube-system $(kubectl get pods --namespace=kube-system -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep core)

我想看看:

kubectl logs --namespace=kube-system $(kubectl get pods -o=name --all-namespaces | grep core)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试以下操作,将静态值替换为列名和表名。

      WITH TEMP_CTE(ATIVOID, OSID, DT_INI,DIF_M, LVL, DT_FIM)
AS
(SELECT '12345TC' AS , '1710201409' AS OSID, TO_DATE('20/10/17 14:09:00', 'DD/MM/YYYY HH24:MI:SS') AS DT_INI,
       ABS(EXTRACT(MONTH FROM TO_DATE('20/10/17 14:09:58', 'DD/MM/YYYY HH24:MI:SS'))-  EXTRACT(MONTH FROM TO_DATE('01/12/17 09:03:52', 'DD/MM/YYYY HH24:MI:SS'))) AS DIF_M,
        1,
        TO_DATE('01/12/17 09:03:52', 'DD-MM-YYYY HH24:MI:SS' ) AS DT_FIM
  FROM DUAL
  UNION ALL 
  SELECT ATIVOID, OSID, ADD_MONTHS(DT_INI, 1) AS DT_INI,
  DIF_M, LVL + 1, DT_FIM FROM TEMP_CTE WHERE LVL <= DIF_M
  )
SELECT ATIVOID, OSID,
CASE WHEN LVL = 1 THEN TO_CHAR(DT_INI, 'DD-MM-YYYY HH24:MI:SS') ELSE TO_CHAR(TRUNC(DT_INI, 'MM'), 'DD-MM-YYYY HH24:MI:SS') END AS DT_INI,
CASE WHEN LVL <= DIF_M THEN TO_CHAR(TRUNC(LAST_DAY(DT_INI) + 1) - 1/(24*60*60), 'DD-MM-YYYY HH24:MI:SS') ELSE TO_CHAR(DT_FIM, 'DD-MM-YYYY HH24:MI:SS') END AS DT_FIM FROM TEMP_CTE