Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:20:52+00:00 2026-06-13T01:20:52+00:00

I’m trying to change my code to have Oracle do all the heavy lifting.

  • 0

I’m trying to change my code to have Oracle do all the heavy lifting. Originally, I had a function which did the following:

<!--- 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>

I’ve already figured out how to use Oracle’s MERGE to replace the update statement in the loop. My problem is the SQL to replace the insert statement. I’d like to be able to calculate the new value inside the SQL… once I get that, I think I can figure out the MERGE/insert part fairly easily.

So I want to do something like:

    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
    )

which, perhaps obviously, doesn’t work. Currently I get an ORA-00904: “t2″.”a”: Invalid Identifier, which isn’t surprising.

So, I basically need to figure out how to get the sum of y from table_1, correlated by x = table_2.a, into the original getSomething query.

Edit: I tried this, but it’s not quite right:

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
    )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:20:53+00:00Added an answer on June 13, 2026 at 1:20 am

    You could write your query like this:

    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)
    

    The optimizer should be able to push the predicate t2.a = v.x into the subquery if it is convenient (for example if there is an index on table_1.x).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.