1

我正在尝试更改我的代码以让 Oracle 完成所有繁重的工作。最初,我有一个执行以下操作的函数:

<!--- get common data --->
<cfquery name="getCommon">
    SELECT  x, NVL(SUM(y), 0) as y
    FROM    table_1
    WHERE   z between #this# and #that#
    GROUP BY x
</cfquery>

<!--- place common values into a struct --->
<cfset oCommon = StructNew() >
<cfloop query="getCommon">
    <cfset oCommon[x] = y >
</cfloop>

<!--- get records to loop through --->
<cfquery name="getSomething">
    SELECT a, b, c/d as e from (
        SELECT DISTINCT t2.a, 
                t2.b, 
                c,
                sum(d)as d
        FROM table_2 t2
        LEFT JOIN table_3 t3
        ON t2.a = t3.a
        AND t2.b = t3.b
        GROUP BY t2.a, t2.b, c
    )
</cfquery>

<!--- loop through the results --->
<cfloop query="getSomething">
    <!--- do an update to the target table if conditions are met --->       

    <!--- calculate my important new value --->
    <cfset new_value = #getSomething.e# * #oCommon[b]# >

    <!--- finally, insert my new values into the target table --->
    <cfquery name="insertTarget">
        INSERT INTO target_table(
                a, 
                b, 
                c
        )
        VALUES ( 
            #getSomething.a#,
            #getSomething.b#,
            #new_value# 
        )
    </cfquery>
</cfloop>

我已经想出了如何使用 Oracle 的 MERGE 来替换循环中的更新语句。我的问题是替换插入语句的 SQL。我希望能够计算 SQL 中的新值......一旦我得到它,我想我可以很容易地找出 MERGE/insert 部分。

所以我想做类似的事情:

    SELECT a, b, c/d*y as e from (
        SELECT DISTINCT t2.a, 
            t2.b, 
            c,
            sum(d)as d,
            (
                SELECT y FROM (
                    SELECT  x, NVL(SUM(y), 0) as y
                    FROM    table_1
                    WHERE   z between #this# and #that#
                            AND x = t2.a
                    GROUP BY x
                )
            )       
        FROM table_2 t2
        LEFT JOIN table_3 t3
        ON t2.a = t3.a
        AND t2.b = t3.b
        GROUP BY t2.a, t2.b, c
    )

这也许显然是行不通的。目前我得到一个 ORA-00904: "t2"."a": Invalid Identifier,这并不奇怪。

所以,我基本上需要弄清楚如何从 table_1 中获取 y 的总和,通过 x = table_2.a 相关,到原始的 getSomething 查询中。

编辑:我试过这个,但不太正确:

SELECT a, b, c/d*y as e from (
        SELECT DISTINCT t2.a, 
            t2.b, 
            c,
            sum(d)as d,
             NVL(SUM(y), 0) as y     
        FROM table_2 t2
        LEFT JOIN table_3 t3
        ON t2.a = t3.a
        AND t2.b = t3.b
        LEFT JOIN table_1 t1
        ON t1.x = t2.a
        AND t1.z between #this# and #that#
        GROUP BY t2.a, t2.b, c
    )
4

2 回答 2

3

你可以这样写你的查询:

SELECT a, b, c / d * y AS e
  FROM (SELECT DISTINCT t2.a,
                        t2.b,
                        c,
                        sum(d) AS d,
                        v.y
          FROM table_2 t2
               LEFT JOIN table_3 t3
                  ON t2.a = t3.a
                 AND t2.b = t3.b
               LEFT JOIN (SELECT x, nvl(sum(y), 0) AS y
                            FROM table_1
                           WHERE z BETWEEN #this# AND #that#
                          GROUP BY x) v
                  ON t2.a = v.x
        GROUP BY t2.a, t2.b, c, v.y)

如果方便的话,优化器应该能够将谓词推t2.a = v.x送到子查询中(例如,如果在 上有一个索引table_1.x)。

于 2012-10-15T09:26:28.393 回答
-1

对我有用的 sql 语句如下:

 Select p.id,p.name, ( select sum(quantity) from details d where d.idproduct=p.id) sum from  products p
于 2019-07-23T15:36:41.330 回答