从相关子查询中选择一个总和 - Oracle

时间:2012-10-14 22:49:17

标签: oracle coldfusion

我正在尝试更改我的代码以让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来替换循环中的update语句。我的问题是SQL替换insert语句。我希望能够计算SQL中的新值...一旦我得到它,我想我可以很容易地找出MERGE /插入部分。

所以我想做点什么:

    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”:无效的标识符,这并不奇怪。

所以,我基本上需要弄清楚如何从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
    )

2 个答案:

答案 0 :(得分: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推送到子查询中。

答案 1 :(得分:-1)

对我有用的sql语句如下:

 Select * from ( select id from table) r