无法获取“从子查询更新”在 Oracle SQL 中工作的语句

发布于 2024-11-03 23:12:58 字数 672 浏览 0 评论 0原文

我已经查看了所有内容,但无法弄清楚在匹配客户 ID 时如何根据子查询数据更新表中的列。下面是一些语法,可以让您了解我想要做什么:

UPDATE TableName
   SET TableName.Revenue = Z.Revenue
FROM
(
   SELECT
      CustomerID,
      sum(Revenue) as Revenue
   FROM
   (
      SELECT
         CustomerID,
         Revenue
      FROM
         TableA
      WHERE
         CustomerID in TableF
      UNION ALL
         SELECT
            CustomerID,
            Revenue
         FROM
            TableB
         WHERE
            CustomerID in TableF
   )
   GROUP BY
      CustomerID
) Z
WHERE
   TableName.CustomerID = Z.CustomerID

本质上,如果同一个表下的另一个 ID 列与子查询中的 ID 匹配,我希望更新表列。我的目标是避免从子查询创建一个全新的表。任何帮助将不胜感激。谢谢。

I've looked all over, but can't figure out how to update a column in a table based on subquery data when matching on a customer ID. Here's some syntax to give an idea of what I'm trying to do:

UPDATE TableName
   SET TableName.Revenue = Z.Revenue
FROM
(
   SELECT
      CustomerID,
      sum(Revenue) as Revenue
   FROM
   (
      SELECT
         CustomerID,
         Revenue
      FROM
         TableA
      WHERE
         CustomerID in TableF
      UNION ALL
         SELECT
            CustomerID,
            Revenue
         FROM
            TableB
         WHERE
            CustomerID in TableF
   )
   GROUP BY
      CustomerID
) Z
WHERE
   TableName.CustomerID = Z.CustomerID

In essence, I'm looking to update a table column if another ID column under the same table matches an ID from a subquery. My goal is to avoid creating a whole new table from the subquery. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-11-10 23:12:58

Oracle 不支持 UPDATE 的 FROM 子句。这可行:

UPDATE TableName
   SET TableName.Revenue =
(
   SELECT
      sum(Revenue) as Revenue
   FROM
   (
      SELECT
         CustomerID,
         Revenue
      FROM
         TableA
      WHERE
         CustomerID in (select CustomerID from TableF)
      UNION ALL
         SELECT
            CustomerID,
            Revenue
         FROM
            TableB
         WHERE
            CustomerID in (select CustomerID from TableF)
   )
   WHERE
      CustomerID = TableName.CustomerID
   GROUP BY
      CustomerID
);

另一个选择是使用 dml_expression_table 语法,它基本上类似于 update (select ax, by from a join b on aa = bb) set x = y。但这有点奇怪,需要独特的约束才能发挥作用。

或者您可以仅使用 MERGE 和 UPDATE 部分。仅使用 MERGE 进行 UPDATE 的情况并不常见,但由于 Oracle 支持 ANSI 标准,因此它可以帮助您使用熟悉的语法。

Oracle does not support a FROM clause for an UPDATE. This will work:

UPDATE TableName
   SET TableName.Revenue =
(
   SELECT
      sum(Revenue) as Revenue
   FROM
   (
      SELECT
         CustomerID,
         Revenue
      FROM
         TableA
      WHERE
         CustomerID in (select CustomerID from TableF)
      UNION ALL
         SELECT
            CustomerID,
            Revenue
         FROM
            TableB
         WHERE
            CustomerID in (select CustomerID from TableF)
   )
   WHERE
      CustomerID = TableName.CustomerID
   GROUP BY
      CustomerID
);

Another option is to use the dml_expression_table syntax, which basically looks something like update (select a.x, b.y from a join b on a.a = b.b) set x = y. But that's kinda weird and requires unique constraints to work.

Or you could use MERGE with only an UPDATE section. It's unusual to use a MERGE for only an UPDATE, but since Oracle supports the ANSI standard it may help you use familiar syntax.

挽梦忆笙歌 2024-11-10 23:12:58

看起来您在这里打破了规范化(在数据库中多次重复相同的数据),这可能会在尝试保持此列正确更新时导致很多问题。假设您了解这一点并且仍然想继续,这应该可行:

UPDATE
    Table_Name
SET
    revenue = SUM(Z.revenue)
FROM
    Table_Name
INNER JOIN
(
    SELECT
        customer_id,
        SUM(revenue) AS revenue
    FROM
    (
        SELECT
            customer_id,
            revenue
        FROM
            Table_A
        WHERE
            customer_id IN (SELECT customer_id FROM Table_F)
        UNION ALL
        SELECT
            customer_id,
            revenue
        FROM
            Table_B
        WHERE
            customer_id IN (SELECT customer_id FROM Table_F)
    )
) Z ON
    Z.customer_id = Table_Name.customer_id

我认为问题可能是您的 FROM 子句中没有主表或专门加入到查询中。我没有做很多 Oracle 工作,所以我不太肯定,但如果你用 MS SQL Server 运行它,那将是一个问题。

It looks like you're breaking normalization here (repeating the same data more than once in your database), which is likely to cause a LOT of problems down the road with trying to keep this column properly updated. Assuming that you understand that and still want to proceed, this should work:

UPDATE
    Table_Name
SET
    revenue = SUM(Z.revenue)
FROM
    Table_Name
INNER JOIN
(
    SELECT
        customer_id,
        SUM(revenue) AS revenue
    FROM
    (
        SELECT
            customer_id,
            revenue
        FROM
            Table_A
        WHERE
            customer_id IN (SELECT customer_id FROM Table_F)
        UNION ALL
        SELECT
            customer_id,
            revenue
        FROM
            Table_B
        WHERE
            customer_id IN (SELECT customer_id FROM Table_F)
    )
) Z ON
    Z.customer_id = Table_Name.customer_id

I think the problem may be that you don't have the main table in your FROM clause or specifically JOINed into the query. I don't do a lot of Oracle work, so I'm not positive, but that would be a problem if you ran this with MS SQL Server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文