Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从相关子查询中选择总和-Oracle_Oracle_Coldfusion - Fatal编程技术网

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

从相关子查询中选择总和-Oracle,oracle,coldfusion,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> <!---

我正试图修改我的代码,让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>
 Select p.id,p.name, ( select sum(quantity) from details d where d.idproduct=p.id) sum from  products p
这很明显是行不通的。目前我得到了一个ORA-00904:“t2”“a”:无效的标识符,这并不奇怪

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

您可以这样编写查询:

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
推送到子查询中(例如,如果
表_1.x
上有索引)。

对我有效的sql语句如下:

<!--- 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>
 Select p.id,p.name, ( select sum(quantity) from details d where d.idproduct=p.id) sum from  products p

这看起来差不多完美了,谢谢!您唯一遗漏的是需要将v.y添加到最后一个GROUPBY子句中。我不得不说,SELECT语句的最后一个左连接对我的新手来说是错误的,不知怎的。。。关于子查询:一旦我意识到表是行的集合,我发现用子查询代替表是合乎逻辑的(因为子查询也是行的集合)。