根据其他列

时间:2017-06-14 13:28:47

标签: sql netezza

我在Netezza SQL数据库中工作。我有2列:

  • 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 个答案:

答案 0 :(得分: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;