删除&将新数据插入表中

时间:2017-06-01 16:15:56

标签: sql teradata sql-insert sql-delete execution

我需要删除旧记录并在项目事件发生变化时每天插入新记录。我正在尝试按位置获取每个项目的清单,而不是清算,并且能够每天更新它,所以我创建了一个表格来删除&插入:

Items_minus_liquidation(表名)

(存储INTEGER NULL,

存储B INTEGER NULL)

表A:

LOCATION      EVENT         ITEM
STORE A    LIQUIDATION     APPLE
STORE A      SALE          BANANA
STORE A      SALE          ORANGE
STORE A      SALE          PEAR
STORE A    LIQUIDATION     PEACH
STORE A      SALE          SPINACH
STORE A      SALE          TOMATO
STORE A    LIQUIDATION     CARROT
STORE A      SALE         BROCCOLI
STORE A      SALE         MUSHROOM
STORE B    LIQUIDATION     APPLE
STORE B      SALE          BANANA
STORE B      SALE          ORANGE
STORE B      SALE          PEAR
STORE B    LIQUIDATION     PEACH
STORE B      SALE          PLUM
STORE B      SALE         TOMATO
STORE B    LIQUIDATION    CARROT
STORE B      SALE         BROCCOLI
STORE B      SALE         MUSHROOM

以下是我要找的结果:

STORE_A    STORE_B
BANANA     BANANA
ORANGE     ORANGE
PEAR       PEAR
SPINACH    NULL
NULL       PLUM
TOMATO     TOMATO
BROCCOLI   BROCCOLI
MUSHROOM   MUSHROOM

这是我目前的查询:

DELETE FROM DB.ITEMS_MINUS_LIQUIDATION;
INSERT INTO DB.ITEM_MINUS_LIQUIDATION
SELECT
    A.PRODUCT AS STORE_A,
    B.PRODUCT AS STORE_B
FROM
(
SELECT
    ITEM AS PRODUCT
FROM TABLE_A
WHERE EVENT NOT IN  ('LIQUIDATION')
AND LOCATION='STORE_A'
) A

FULL OUTER JOIN

(
SELECT
    ITEM AS PRODUCT
FROM TABLE_A
WHERE EVENT NOT IN  ('LIQUIDATION')
AND LOCATION='STORE_B'
) B

ON A.ITEM=B.ITEM

问题是INSERT命令运行并运行并运行而不提供任何结果。完整的外连接查询产生了我上面正在寻找的内容,因此我希望删除旧记录并使用完整外连接查询插入新记录。为什么insert命令无法正常工作?

0 个答案:

没有答案