Home Discussions Questions & Answers Dynamic SQL Pivot Table Column Generation

Dynamic SQL Pivot Table Column Generation

Avatar photoCustomer February 16, 2025 at 2:31 am

I need to build a financial report that pivots GL balances, but the columns (periods) must be dynamically generated based on a list of periods selected by the user. Which type of Blitz Report anchor should I use for this dynamic column injection?

Viewing 8 reply threads
  • Author
    Replies
    • Support February 17, 2025 at 12:25 am  

      For dynamically changing the structure of your SQL query, such as generating columns for a pivot table, you must use the `&lexical` anchor. This type of anchor is highly versatile because it allows you to inject arbitrary SQL snippets, including dynamic tables, columns, `ORDER BY` clauses, or even complete subselects, which goes far beyond simple `WHERE` clause restrictions. In your specific case involving pivoting, the lexical anchor, such as `&gl_period_pivot`, would be placed inside the `PIVOT` clause of your main report SQL.

    • Avatar photoCustomer February 17, 2025 at 12:38 pm  

      So, if the user selects a range of periods, how does the parameter definition generate the required list of quoted and aliased period names that the `PIVOT` clause expects?

    • Support February 19, 2025 at 8:12 am  

      The parameter definition associated with the `&lexical` anchor must contain SQL text that, when executed, returns the precise comma-separated list of quoted period names and their corresponding aliases expected by the `PIVOT` clause. For example, if the parameter logic is designed to derive periods up to a specified month, the resulting SQL text inserted at runtime might look like ` ‘Jan-20’ jan_2020 , ‘Feb-20’ feb_2020 , … , ‘total’ total `. This dynamic substitution is what allows the pivot table structure to change based on the user’s input without altering the core report SQL definition.

    • Avatar photoCustomer February 20, 2025 at 7:51 am  

      This report also needs a simple WHERE clause restriction on the Ledger Name. Should I use the `&lexical` anchor again, or is there a simpler method for basic filtering?

    • Support February 22, 2025 at 2:22 am  

      For simple filtering like restricting the Ledger Name, the `n=n` anchor is often preferred, assuming your main query includes `where 1=1`. When the user provides a value for the Ledger Name parameter, the parameter’s corresponding SQL text (e.g., `gl.name = :ledger_name`) is inserted in place of or adjacent to the ‘1=1’ anchor at runtime, resulting in clean, parameterized SQL for standard filtering. Using `n=n` for standard `WHERE` clauses is simpler than setting up a lexical anchor.

    • Avatar photoCustomer February 22, 2025 at 11:20 am  

      Can lexical anchors also be used to dynamically change the SELECT list, for example, to include DFF columns based on user selection?

    • Support February 23, 2025 at 6:10 am  

      Yes, the `&lexical` anchor is perfect for dynamically changing the columns in the `SELECT` list. You can define a parameter that uses the utility function `XXEN_UTIL.DFF_COLUMNS` to retrieve the SQL text corresponding to enabled Descriptive Flexfield columns for a specified table, such as `mtl_system_items_b`. This utility dynamically generates the necessary column aliases (e.g., `msiv.attribute1 “Late Demands Penalty”`) which are then injected into the main report SQL at the position of the `&flexfield_columns` lexical anchor.

    • Avatar photoCustomer February 23, 2025 at 9:23 am  

      If I need to write a massive SQL query that exceeds the 32767 character limit imposed by Oracle Forms, how can I upload it?

    • Support February 23, 2025 at 8:27 pm  

      If your extraction SQL exceeds the standard Oracle Forms limit of 32767 characters, you must use the specialized ‘Upload Large SQL’ functionality found under the Tools menu in Blitz Report Setup. This feature opens a browser window where you can select and upload a SQL file, which must be uploaded in UTF-8 encoding if it contains non-ANSI characters. This ensures your comprehensive report logic can be stored and executed correctly, bypassing the Forms character limitation.

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

Login with: