Home Discussions Questions & Answers Dynamically Modifying GROUP BY Clauses

Dynamically Modifying GROUP BY Clauses

Avatar photoCustomer January 21, 2022 at 6:20 am

I need a Purchase Order Summary report where users can optionally choose to group the results by ‘Supplier ID’ in addition to the mandatory ‘Currency Code’. If they choose not to group by Supplier, the query should perform simple currency aggregation. How can I dynamically insert the `pv.vendor_id` column into the `GROUP BY` clause?

Viewing 6 reply threads
  • Author
    Replies
    • Support January 22, 2022 at 4:02 pm  

      For modifying structural SQL elements like the `GROUP BY` clause, the `&lexical` anchor is the appropriate tool, as it allows insertion of arbitrary SQL snippets . You should place the anchor, perhaps named `&group_by_vendor`, within your `GROUP BY` clause immediately preceding the mandatory columns, like `group by &group_by_vendor pha.currency_code` . The parameter associated with this anchor would have a SQL text containing the optional column list, such as `pv.vendor_id,` .

    • Avatar photoCustomer January 24, 2022 at 6:04 pm  

      If the user runs the report but leaves the optional parameter blank, will the resulting SQL still be valid?

    • Support January 25, 2022 at 8:48 pm  

      Yes, if the user leaves the parameter value blank, the core functionality of the `&lexical` anchor dictates that the corresponding reference is completely removed before the SQL is executed . Therefore, the runtime SQL would simply revert to `group by pha.currency_code`, which is perfectly valid SQL and ensures the intended aggregation occurs without the vendor dimension . This prevents syntax errors and optimizes performance.

    • Avatar photoCustomer January 26, 2022 at 10:30 am  

      Can I use the same lexical anchor multiple times in the same report? For instance, once in the `SELECT` list and once in the `GROUP BY` clause, if both need the `vendor_id` column?

    • Support January 27, 2022 at 6:16 am  

      Yes, you can use the same anchor multiple times within a single report SQL definition . For every occurrence of the anchor (e.g., `&group_by_vendor` in both `SELECT` and `GROUP BY`), Blitz Report will insert the corresponding parameter SQL text during runtime . This allows you to maintain consistency across different clauses based on a single parameter input.

    • Avatar photoCustomer January 27, 2022 at 6:37 pm  

      If I need the dynamic value to appear as literal text in the final SQL (e.g., to force Oracle to re-parse the query execution plan), rather than as a bind variable, how is that configured?

    • Support January 29, 2022 at 12:18 am  

      To force the parameter value to be inserted as literal text (lexical text) instead of a bind variable, you must use the special placeholder “ within the parameter’s SQL text field . This method ensures that the final SQL executed includes the literal value, which can be useful when you purposefully want to enforce reparsing for different parameter values, as illustrated in the Discoverer EUL access example .

Viewing 6 reply threads
  • You must be logged in to reply to this post.

Login with: