2017-06-14 56 views
1

我正在Netezza SQL數據庫中工作。我有2列:在Netezza基於其他列添加小時到Netezza的時間戳

  • DATETIME1:具有交易的UTC時間的時間戳
  • TIME_OFFSET:是一個整數,表示從UTC用戶的時區的偏移量。例如,在UTC + 1的用戶纔有價值1

我想這樣做是TIME_OFFSET列添加到DATETIME1列一個小時。

我創建了以下再現的例子:

SELECT * 
FROM ADMIN.INTEGRAL_ADSCIENCE_1845 

WITH my_table AS (
SELECT TIMESTAMP('2017-06-01 08:01:45') AS datetime1, 
    1 AS time_offset 
UNION 
SELECT TIMESTAMP('2017-06-01 08:03:45') AS datetime1, 
    2 AS time_offset 
) 
SELECT 
    DATETIME1, 
    TIME_OFFSET, 
    DATETIME1 + TIME_OFFSET AS simple_add, 
    DATETIME1 + INTERVAL '1 hour' AS add_one_hour 
FROM my_table; 

這產生以下輸出:

+---------------------+-------------+------------+---------------------+ 
|  DATETIME1  | TIME_OFFSET | SIMPLE_ADD | ADD_ONE_HOUR  | 
+---------------------+-------------+------------+---------------------+ 
| 2017-06-01 08:01:45 |   1 | 2017-06-02 | 2017-06-01 09:01:45 | 
| 2017-06-01 08:03:45 |   2 | 2017-06-03 | 2017-06-01 09:03:45 | 
+---------------------+-------------+------------+---------------------+ 

但我想有是添加1小時,以第一行和2小時,以第二行。

我知道mysql的date_add()函數,但我僅限於Netezza不幸的。

回答

1

這應該適合你。

WITH my_table AS (SELECT TIMESTAMP('2017-06-01 08:01:45') AS dttm 
          , 1 AS tm_offset 
         UNION 
        SELECT TIMESTAMP('2017-06-01 08:03:45') AS dttm 
          , 2 AS tm_offset) 
SELECT dttm 
     , tm_offset 
     , dttm + tm_offset AS simple_add 
     , dttm + CAST(tm_offset || ' hour' AS INTERVAL) 
    FROM my_table; 
+0

謝謝。我沒有想到投中間隔。我試過'+ INTERVAL tm_offset || '小時',但沒有工作。 – niczky12

+0

是的,如果我猜測它與Netezza評估查詢的順序有關。 – joeb