• Query Calculation: Left-to-Right? (AXP)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Query Calculation: Left-to-Right? (AXP)

    Author
    Topic
    #366201

    Before I put together a test case…

    A query I am about to develop produces calculated values. Some of the calculated fields rely on results “in other columns”. e.g.,
    Field3 = Field1+Field2, Field4=2*Field3.

    What if I change the field order, and write it like this:
    Field3=2*Field4, Field4 = Field1+Field2 ?

    Will either of those work?

    Or do I need to write two queries, one to calc (the original) Field3, and a second query to calc Field4?

    Viewing 1 reply thread
    Author
    Replies
    • #567175

      In general, as long as the field that depends on another calculated value is in a column to the right of the one it depends on, it will work. It seems to me there are some exceptions, but I don’t recall what they were – I think it may involve using functions in the first calculated value. Maybe someone else can dredge up what the issue was.

    • #567178

      Why not use Field4=2*(Field1+Field2) and avoid any possibility of a problem? I don’t know about 2000 or XP, but in some older versions you could not use a calculated field as a parameter in the same query that calculated it.

      • #567214

        If my situation was as simple as I stated it, I would do what you suggest. However, many of the values that are calculated are returned from functions. The fields and functions are scavenged from a spreadsheet, where the calculation order is “automatic”: cells are recalculated depending on whatever has changed.

        Perhaps another question is pertinent here: my approach to a “complicated expression” is to encapsulate that calculation in a function, verify it outside of the query, and then expose the function interface to the world. Is this the recommended database practice?

        • #567535

          ——-
          Perhaps another question is pertinent here: my approach to a “complicated expression” is to encapsulate that calculation in a function, verify it outside of the query, and then expose the function interface to the world. Is this the recommended database practice
          ——

          For complicated expressions, that’s probably a good approach, though it may take a bit more resources during execution. For simple expressions using existing functions and simple IIF statements, we usually embed them in the query or the control.

    Viewing 1 reply thread
    Reply To: Query Calculation: Left-to-Right? (AXP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: