ORACLE EST GMT日期问题

时间:2015-08-11 12:27:17

标签: oracle date

我需要按日期分组清点。我使用了以下查询。我的问题是找到数据的应用程序是EST,数据库是在GMT时区运行的。因此,当我在应用程序中选择特定日期时,需要获取相应的GMT日期并获得其计数。目前使用的查询是

SELECT tble.dte, COUNT(DISTINCT INVENTORY_ID)As cmt 
  FROM ATL_GROUNDING_INFO groundinginfo 
       right join (SELECT 
                   TO_DATE('03/11/2015 04:00:00','MM/DD/YYYY HH24:MI:SS') - 1 + rownum dte 
                     FROM DUAL 
                  CONNECT BY ROWNUM < 366 
                      AND 
         TO_DATE('03/11/2015 04:00:00','MM/DD/YYYY HH24:MI:SS') - 1 + rownum <= 
                         TO_DATE('04/02/2015 03:59:59','MM/DD/YYYY HH24:MI:SS')
                  ) tble 
              ON tble.dte = (groundinginfo.DATE_TURNED_IN) 
 group by tble.dte
 order by tble.dte desc`

但是在这种情况下,计数是错误的,因为11月3日EST = 11March 04:00:00 AM到3月12日03:59:59 GMT。因此,当我为11月3日搜索时,我需要从DB获取上述日期范围之间的数据。请协助

1 个答案:

答案 0 :(得分:0)

假设EST是您的SESSIONTIMEZONE,您可以这样做:

WITH t AS
    (SELECT TIMESTAMP '2015-04-02 04:00:00' + ROWNUM * INTERVAL '1' DAY AS DT
    FROM dual
    CONNECT BY TIMESTAMP '2015-04-02 04:00:00' + ROWNUM * INTERVAL '1' DAY <= TIMESTAMP '2015-11-03 04:00:00')
SELECT tble.dte, COUNT(DISTINCT INVENTORY_ID)As cmt 
FROM ATL_GROUNDING_INFO groundinginfo 
   right join t ON FROM_TZ(CAST(dte AS TIMESTAMP), 'UTC') = DT

或更明确地说:

WITH t AS
    (SELECT TIMESTAMP '2015-04-02 04:00:00 EST' + ROWNUM * INTERVAL '1' DAY AS DT
    FROM dual
    CONNECT BY TIMESTAMP '2015-04-02 04:00:00 EST' + ROWNUM * INTERVAL '1' DAY <= TIMESTAMP '2015-11-03 04:00:00 EST')
SELECT tble.dte, COUNT(DISTINCT INVENTORY_ID)As cmt 
FROM ATL_GROUNDING_INFO groundinginfo 
   right join t ON FROM_TZ(CAST(dte AS TIMESTAMP), 'UTC') = DT