2014-09-25 50 views
0

我發現了一個例子SQL建立時間漁政船,我從這個鏈接https://gist.github.com/johngrimes/408559如何在netezza中創建時間維度?

一切都在mysql的完美搞定,

但是,當我試圖在Netezza公司,代碼無法正常工作,有些錯誤是出現諸如 「期待USING' or ')' '或'', ''」 等

我曾試圖修改

CREATE TABLE numbers_small (number INT); 
INSERT INTO numbers_small VALUES (0); 
INSERT INTO numbers_small VALUES (1); 
INSERT INTO numbers_small VALUES (2); 
INSERT INTO numbers_small VALUES (3); 
INSERT INTO numbers_small VALUES (4); 
INSERT INTO numbers_small VALUES (5); 
INSERT INTO numbers_small VALUES (6); 
INSERT INTO numbers_small VALUES (7); 
INSERT INTO numbers_small VALUES (8); 
INSERT INTO numbers_small VALUES (9); 

CREATE TABLE numbers (number BIGINT); 
INSERT INTO numbers 
SELECT thousands.number * 1000 + hundreds.number * 100 + tens.number * 10 + ones.number 
    FROM numbers_small thousands, numbers_small hundreds, numbers_small tens, numbers_small ones 
LIMIT 1000000; 



CREATE TABLE D_TIME_DAILY_TES (
    TIME_ID   BIGINT PRIMARY KEY, 
    TANGGAL    DATE NOT NULL, 
    TAHUN    INT NOT NULL, 
    BULAN   CHAR(10) NOT NULL, 
    HARI  CHAR(10) NOT NULL, 
    LAST_UPDATE timestamp NOT NULL, 
    constraint D_TIME_DAILY_TES UNIQUE KEY (TANGGAL) 
); 


INSERT INTO D_TIME_DAILY_TES (TIME_ID, TANGGAL, LAST_UPDATE) 
SELECT number, DATE_ADD('2010-01-01', INTERVAL number DAY), CURRENT_TIMESTAMP 
    FROM numbers 
    WHERE DATE_ADD('2010-01-01', INTERVAL number DAY) BETWEEN '2014-01-01' AND '2014-12-31' 
    ORDER BY number; 


UPDATE D_TIME_DAILY_TES SET 
    HARI = DATE_FORMAT(TANGGAL, "%W"), 
    BULAN =  DATE_FORMAT(TANGGAL, "%M"), 
    TAHUN =  DATE_FORMAT(TANGGAL, "%Y"), 
    LAST_UPDATE = CURRENT_TIMESTAMP; 

,但它仍然沒有工作:(

我該如何解決它?

thx :)

回答

0

從語法角度來看,我的代碼有兩個問題。

第一個是創建表D_TIME_DAILY_TES。

刪除TANGGAL上的唯一鍵約束,它會正常運行。

CREATE TABLE D_TIME_DAILY_TES 
    (
     TIME_ID BIGINT PRIMARY KEY, 
     TANGGAL DATE NOT NULL, 
     TAHUN INT NOT NULL, 
     BULAN CHAR(10) NOT NULL, 
     HARI CHAR(10) NOT NULL, 
     LAST_UPDATE TIMESTAMP NOT NULL 
    ); 

第二個是在INSERT中填充同一個表。 DATE_ADD不需要將整數添加到日期,並且實際上不存在。您可以改用此表單。請注意,這個插入實際上會失敗,因爲我們只插入三列,而您爲D_TIME_DAILY_TES定義的所有列都不是NULL,所以您還需要修復它。

INSERT INTO D_TIME_DAILY_TES 
    (
     TIME_ID, 
     TANGGAL, 
     LAST_UPDATE 
    ) 
    SELECT   NUMBER, 
    '2010-01-01'::DATE + NUMBER, 
    CURRENT_TIMESTAMP 
    FROM numbers 
    WHERE '2010-01-01'::DATE + NUMBER BETWEEN '2014-01-01'::DATE AND '2014-12-31'::DATE 
    ORDER BY NUMBER;