使用连接删除sybase中的行

时间:2013-08-22 20:23:34

标签: sql join sybase sql-delete

我正在尝试运行查询以删除具有特定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,我只是使用了错误的语法来删除语句吗?

1 个答案:

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