我有这样简单的INSERT:
INSERT INTO table_1
(FIX_DATA, VAR_DATA, DATE)
SELECT 'row1', t.value, g.value
FROM temp t, table_2 g
WHERE t.condition = g.condition
在table_1中,我根据上述条件插入了数千个值。但是现在我需要在这个条件下在table_1中执行INSERT:
结论,日总是-1,如果日,则月是-1。 4和当前月份如果天是=> 4.格式为dd.mm.yyyy。
任何想法如何做到这一点?
最好的问候。
答案 0 :(得分:1)
您需要使用CASE
。
INSERT INTO table_1(FIX_DATA, VAR_DATA, DATE)
SELECT 'row1', t.value,
CASE WHEN EXTRACT(day FROM g.value) < 4 THEN -- create the first type of date from g.value
ELSE -- create the second type of date from g.value
END
FROM temp t
JOIN table_2 g on t.condition = g.condition
或者您也可以使用UNION ALL
INSERT INTO table_1(FIX_DATA, VAR_DATA, DATE)
SELECT 'row1', t.value,
-- create the first type of date from g.value
FROM temp t
JOIN table_2 g on t.condition = g.condition
WHERE EXTRACT(day FROM g.value) < 4
UNION ALL
SELECT 'row1', t.value,
-- create the second type of date from g.value
FROM temp t
JOIN table_2 g on t.condition = g.condition
WHERE EXTRACT(day FROM g.value) >= 4
答案 1 :(得分:0)
此解决方案适用于我:
INSERT INTO table_1
(FIX_DATA, VAR_DATA, DATE)
SELECT 'row1', t.value,
CASE WHEN EXTRACT(day FROM g.value) < 4 THEN EXTRACT (day FROM g.value-1) || '.' || EXTRACT (month FROM sysdate) || '.' || EXTRACT (year FROM sysdate)
ELSE EXTRACT (day FROM g.value-1) || '.' || EXTRACT (month FROM sysdate-1) || '.' || EXTRACT (year FROM sysdate) END
FROM temp t, table_2 g
WHERE t.condition = g.condition
谢谢大家,br。