2

我正在尝试运行查询以删除具有特定 ID 且没有最新时间戳的所有行。我收到一条错误消息,告诉我表“c”不存在...

这是查询...

DELETE c
FROM 
    "sac"."vendor_item_cost" c
JOIN(
    SELECT
        c1.Vendor_id,
        c1.Item_Number,
        MAX(c1.Last_edit_timestamp) as [Date]
    FROM
        "sac"."vendor_item_cost" c1
    JOIN
        "sac"."vendor_item_cost" c2
    ON
        c1.Vendor_id = c2.Vendor_id AND c1.Item_Number = c2.Item_Number
    GROUP BY
        c1.Vendor_id, c1.Item_Number
) q
ON
    c.Vendor_id = q.vendor_id AND c.Item_Number = q.Item_Number AND c.Last_edit_timestamp <> q.[Date]

使用相同的代码运行 SELECT 语句会返回我尝试删除的正确行,而不会出现任何问题。见下文...

SELECT 
    c.Store_number, 
    c.Vendor_id, 
    c.Cost, 
    c.Item_Number, 
    c.Last_edit_timestamp 
FROM 
    "sac"."vendor_item_cost" as c
JOIN(
    SELECT
        c1.Vendor_id,
        c1.Item_Number,
        MAX(c1.Last_edit_timestamp) as [Date]
    FROM
        "sac"."vendor_item_cost" c1
    JOIN
        "sac"."vendor_item_cost" c2
    ON
        c1.Vendor_id = c2.Vendor_id AND c1.Item_Number = c2.Item_Number
    GROUP BY
        c1.Vendor_id, c1.Item_Number
) q
ON
    c.Vendor_id = q.vendor_id AND c.Item_Number = q.Item_Number AND c.Last_edit_timestamp <> q.[Date]

我以前从未使用过 sybase,我只是在删除语句中使用了错误的语法吗?

4

1 回答 1

2

试试这个方法:

DELETE "sac"."vendor_item_cost"
FROM "sac"."vendor_item_cost" as c,(SELECT c1.Vendor_id, c1.Item_Number, MAX(c1.Last_edit_timestamp) as [Date]
    FROM
        "sac"."vendor_item_cost" c1
    JOIN
        "sac"."vendor_item_cost" c2
    ON
        c1.Vendor_id = c2.Vendor_id AND c1.Item_Number = c2.Item_Number
    GROUP BY
        c1.Vendor_id, c1.Item_Number
) q
WHERE
    c.Vendor_id = q.vendor_id 
AND c.Item_Number = q.Item_Number 
AND c.Last_edit_timestamp <> q.[Date]
于 2013-08-22T20:30:51.247 回答