Master Dynamic Pivoting in Oracle

In Oracle, the PIVOT operation allows you to rotate rows into columns, effectively transforming data from a row-based representation to a columnar one. This is particularly useful when you want to aggregate data and display it in a more readable and concise format, such as in reporting or analytics applications.

Master Dynamic Pivoting in Oracle


Consider a 'sales_data' table with the columns 'product', 'region', and 'sales'. To find the total sales per product for each region, you can use the PIVOT operator as follows:




  (SELECT product, region, sales

   FROM sales_data)



   FOR region IN ('North' AS north, 'South' AS south, 'East' AS east, 'West' AS west)


ORDER BY product;


This query will return a result set with columns 'product', 'north', 'south', 'east', and 'west', displaying the total sales per product for each region in a clear and concise manner.

While the standard PIVOT operation works well with a known number of columns or categories, it may not be suitable for scenarios where the number of columns or categories is not fixed or known in advance. This is where dynamic pivot comes into play. Dynamic pivot is a technique that enables you to pivot data on the fly, generating the necessary columns based on the distinct values present in the data set.

Dynamic pivot is a powerful tool for handling various data scenarios where the structure of the data is not fixed or can change over time. By using dynamic pivot, you can create more flexible and adaptable queries that can handle a wide range of data inputs, making your applications more robust and maintainable. Additionally, dynamic pivot allows you to generate complex reports and visualizations without having to manually modify your queries every time the data structure changes.

Dynamic Pivot with XML Functions

Oracle provides a set of powerful XML functions that can be used to manipulate and transform XML data within SQL queries. Some of the key XML functions include XMLAGG, XMLELEMENT, and XMLFOREST, which allow you to aggregate, generate, and format XML elements, respectively. By leveraging these XML functions in combination with other SQL constructs, you can create dynamic pivot queries that can adapt to varying data structures and generate pivoted results without knowing the column names in advance.

In the context of dynamic pivoting, there isn't a single "common syntax" for XML functions in Oracle, as they can be combined in various ways to achieve the desired output. However, I can provide you with a general structure of how these XML functions are typically used in dynamic pivot scenarios:


This function is used to create an XML element with a specified name and content. The general syntax is:

XMLELEMENT(NAME element_name, value_expression)



This function is used to add attributes to an XML element. The general syntax is:

XMLATTRIBUTES(expression AS attribute_name [, ...])



This function is used to aggregate multiple XML elements into a single XML fragment. The general syntax is:



In a dynamic pivot scenario, these functions are usually combined in a nested fashion, like this:



  NAME "element_name",

  XMLATTRIBUTES(expression AS attribute_name),




And then aggregated using XMLAGG:




    NAME "element_name",

    XMLATTRIBUTES(expression AS attribute_name),





This combination of XML functions allows you to generate an XML representation of the pivoted data, which can then be processed further to extract the required values for the final result set.


Suppose you have a table called sales_data with the following structure and data:

To perform a dynamic pivot on the sales_data table to show the total quantity sold for each product on each date, you can use the following query:



  pivot_columns AS (








  xml_pivot AS (








            XMLATTRIBUTES(sale_date AS "date"),




      ) AS xml_data











  EXTRACTVALUE(xml_data, '/columns/column[@date="2023-01-01"]/text()') AS "2023-01-01",

  EXTRACTVALUE(xml_data, '/columns/column[@date="2023-01-02"]/text()') AS "2023-01-02"





This query generates the following dynamic pivoted output:

The example above demonstrates how XML functions can be used to create dynamic pivot queries in Oracle, enabling you to handle various data scenarios without having to know the column names in advance.

Dynamic Pivot using REF CURSOR

A REF CURSOR (Reference Cursor) is a data type in Oracle PL/SQL that enables you to create a pointer or a reference to a result set (query output) from a SELECT statement. REF CURSORs are useful in situations where you need to pass query results between PL/SQL blocks, functions, or procedures, and they provide flexibility in working with dynamic SQL statements, like dynamic pivoting.

There are two types of REF CURSORs:

  1. Strongly-typed REF CURSOR: A REF CURSOR type that is defined with a specific record structure (record type) that matches the structure of the result set it will reference.
  2. Weakly-typed REF CURSOR: A REF CURSOR type that is not restricted to any specific record structure, allowing it to reference result sets with different structures.

The difference lies in how the REF CURSORs are defined and used.

A strongly-typed REF CURSOR is explicitly associated with a specific RECORD type, which means that the structure of the data being fetched is known at compile time. This can provide some advantages, such as better error checking during compilation and potentially better performance.

A weakly-typed REF CURSOR, on the other hand, is not associated with a specific RECORD type. It can be used to fetch data with different structures at runtime. This provides more flexibility, but at the cost of weaker compile-time error checking.

In the context of dynamic pivoting, weakly-typed REF CURSORs are commonly used, as the structure of the result set can change based on the data being pivoted.

Example and use cases

Suppose we have a table named sales_data with columns product, region, and sales. We want to create a dynamic pivot that shows the total sales for each product by region.

First, let's create a weakly-typed REF CURSOR type:




  ref_cur RefCurTyp;

  pivot_query VARCHAR2(1000);

  product_name sales_data.product%TYPE;

  sales_total NUMBER;


  -- Generate the pivot query dynamically

  pivot_query := 'SELECT * FROM (

                    SELECT product, region, sales

                    FROM sales_data


                  PIVOT (


                    FOR region IN (' || /* Add the list of regions dynamically here */ || ')



  -- Execute the pivot query and open the REF CURSOR

  OPEN ref_cur FOR pivot_query;


  -- Process the result set


    FETCH ref_cur INTO product_name, sales_total;


    -- Display or process the pivoted data

    DBMS_OUTPUT.PUT_LINE('Product: ' || product_name || ', Sales: ' || sales_total);



  -- Close the REF CURSOR

  CLOSE ref_cur;



The dynamic pivot will produce the following result:

Next, example of using a strongly-typed REF CURSOR with the same sales_data table:

First, we need to define a RECORD type and a REF CURSOR type:



  TYPE sales_data_record IS RECORD (

    product VARCHAR2(20),

    region VARCHAR2(20),

    sales NUMBER



  TYPE sales_data_ref_cursor IS REF CURSOR RETURN sales_data_record;


Next, we create a procedure to fetch the data from the sales_data table and return it as a dynamic pivot using the REF CURSOR:


CREATE OR REPLACE PROCEDURE dynamic_pivot_sales_data(p_cursor OUT sales_data_ref_cursor) IS


  OPEN p_cursor FOR

    SELECT product, region, sales

    FROM sales_data;

END dynamic_pivot_sales_data;


Now we can call the procedure and fetch the data using the REF CURSOR:



  sales_data_cursor sales_data_ref_cursor;

  sales_data_row sales_data_record;





    FETCH sales_data_cursor INTO sales_data_row;

    EXIT WHEN sales_data_cursor%NOTFOUND;


    -- Process the fetched data row here

    DBMS_OUTPUT.PUT_LINE(sales_data_row.product || ' ' || sales_data_row.region || ' ' || sales_data_row.sales);



  CLOSE sales_data_cursor;




The above code declares a strongly-typed REF CURSOR and uses it in the dynamic_pivot_sales_data procedure to return the sales_data table content. Then, it calls the procedure, fetches the data using the REF CURSOR, and processes the fetched data rows.

In the given examples, both the strongly-typed and weakly-typed REF CURSORs are used to fetch data from the sales_data table and process it in the same way. The actual output will be the same, but the way the REF CURSORs are defined and used is different.

Optimizing Dynamic Pivot Queries

When working with dynamic pivot queries, it's essential to optimize the performance to ensure efficient processing of data. Here are some tips to improve the performance of dynamic pivot queries in Oracle:

  1. Limit the number of columns: Avoid pivoting on a large number of columns, as this can lead to a significant increase in the complexity and processing time of the query.
  2. Filter data early: Apply filters to the source data as early as possible in the query to reduce the amount of data being processed.
  3. Use appropriate indexing: Ensure that the source table has appropriate indexes on the columns used for pivoting, filtering, and ordering.
  4. Leverage materialized views: Consider using materialized views to store precomputed pivot results for complex and frequently executed queries.
  5. Optimize the pivot logic: Carefully analyze the pivot logic and find opportunities to simplify or optimize it for better performance.

When to use dynamic pivot vs static pivot

Deciding whether to use a dynamic pivot or a static pivot in your query depends on the requirements of your specific use case. Here are some guidelines to help you make this decision:

Use a dynamic pivot when:

  • The number of columns or values to pivot is unknown or variable at runtime.
  • The pivot values are subject to change, and you want to avoid hardcoding them in the query.

Use a static pivot when:

  • The number of columns or values to pivot is fixed and known at the time of query development.
  • The pivot values are unlikely to change, and hardcoding them in the query is acceptable.

(Pivot Table feature in dbForge Studio for Oracle)

In general, it's best to use a static pivot whenever possible because it typically provides better performance and simpler query development. However, dynamic pivoting is a valuable technique for handling cases where the pivot values are not known in advance or subject to change. If you need more information about static pivot and its usage, you can refer to the ‘Oracle PIVOT’ article.

Check the comment section below for additional information, share what you know, or ask a question about this article by leaving a comment below. And, to quickly find answers to your questions, use our search Search engine.

Note: Some of the information in samples on this website may have been impersonated or spoofed.

Bookmark articleSave

Was this article helpful?

Comments, Questions, Answers, or Reviews

There are no comments as yet, please leave one below or revisit.

To protect your privacy, please remove sensitive or identifiable information from your comments, questions, or reviews. We will use your IP address to display your approximate location to other users when you make a post. That location is not enough to find you.

Your post will be set as anonymous because you are not signed in. An anonymous post cannot be edited or deleted, therefore, review it carefully before posting. Sign-in.

Write Your Comment, Question, Answer, or Review

Online Threat Alerts Security Tips

Pay the safest way

Credit cards are the safest way to pay for online purchases because you can dispute the charges if you never get the goods or services or if the offer was misrepresented. Federal law limits your liability to $50 if someone makes unauthorized charges to your account, and most credit card issuers will remove them completely if you report the problem promptly.

Guard your personal information

In any transaction you conduct, make sure to check with your state or local consumer protection agency and the Better Business Bureau (BBB) to see if the seller, charity, company, or organization is credible. Be especially wary if the entity is unfamiliar to you. Always call the number found on a website’s contact information to make sure the number legitimately belongs to the entity you are dealing with.

Be careful of the information you share

Never give out your codes, passwords or personal information, unless you are sure of who you're dealing with

Know who you’re dealing with

Crooks pretending to be from companies you do business with may call or send an email, claiming they need to verify your personal information. Don’t provide your credit card or bank account number unless you are actually paying for something and know who you are sending payment to. Your social security number should not be necessary unless you are applying for credit. Be especially suspicious if someone claiming to be from a company with whom you have an account asks for information that the business already has.

Check your accounts

Regularly check your account transactions and report any suspicious or unauthorised transactions.

Don’t believe promises of easy money

If someone claims that you can earn money with little or no work, get a loan or credit card even if you have bad credit, or make money on an investment with little or no risk, it’s probably a scam. Oftentimes, offers that seem too good to be true, actually are too good to be true.

Do not open email from people you don’t know

If you are unsure whether an email you received is legitimate, try contacting the sender directly via other means. Do not click on any links in an email unless you are sure it is safe.

Think before you click

If an email or text message looks suspicious, don’t open any attachments or click on the links.

Verify urgent requests or unsolicited emails, messages or phone calls before you respond

If you receive a message or a phone call asking for immediate action and don't know the sender, it could be a phishing message.

Be careful with links and new website addresses

Malicious website addresses may appear almost identical to legitimate sites. Scammers often use a slight variation in spelling or logo to lure you. Malicious links can also come from friends whose email has unknowingly been compromised, so be careful.

Secure your personal information

Before providing any personal information, such as your date of birth, Social Security number, account numbers, and passwords, be sure the website is secure.

Stay informed on the latest cyber threats

Keep yourself up to date on current scams by visiting this website daily.

Use Strong Passwords

Strong passwords are critical to online security.

Keep your software up to date and maintain preventative software programs

Keep all of your software applications up to date on your computers and mobile devices. Install software that provides antivirus, firewall, and email filter services.

Update the operating systems on your electronic devices

Make sure your operating systems (OSs) and applications are up to date on all of your electronic devices. Older and unpatched versions of OSs and software are the target of many hacks. Read the CISA security tip on Understanding Patches and Software Updates for more information.

What if You Got Scammed?

Stop Contact With The Scammer

Hang up the phone. Do not reply to emails, messages, or letters that the scammer sends. Do not make any more payments to the scammer. Beware of additional scammers who may contact you claiming they can help you get your lost money back.

Secure Your Finances

  • Report potentially compromised bank account, credit or debit card information to your financial institution(s) immediately. They may be able to cancel or reverse fraudulent transactions.
  • Notify the three major credit bureaus. They can add a fraud alert to warn potential credit grantors that you may be a victim of identity theft. You may also want to consider placing a free security freeze on your credit report. Doing so prevents lenders and others from accessing your credit report entirely, which will prevent them from extending credit:

Check Your Computer

If your computer was accessed or otherwise affected by a scam, check to make sure that your anti-virus is up-to-date and running and that your system is free of malware and keylogging software. You may also need to seek the help of a computer repair company. Consider utilizing the Better Business Bureau’s website to find a reputable company.

Change Your Account Passwords

Update your bank, credit card, social media, and email account passwords to try to limit further unauthorized access. Make sure to choose strong passwords when changing account passwords.

Report The Scam

Reporting helps protect others. While agencies can’t always track down perpetrators of crimes against scammers, they can utilize the information gathered to record patterns of abuse which may lead to action being taken against a company or industry.

Report your issue to the following agencies based on the nature of the scam:

  • Local Law Enforcement: Consumers are encouraged to report scams to their local police department or sheriff’s office, especially if you lost money or property or had your identity compromised.
  • Federal Trade Commission: Contact the Federal Trade Commission (FTC) at 1-877-FTC-HELP (1-877-382-4357) or use the Online Complaint Assistant to report various types of fraud, including counterfeit checks, lottery or sweepstakes scams, and more.
  • If someone is using your personal information, like your Social Security, credit card, or bank account number, to open new accounts, make purchases, or get a tax refund, report it at This federal government site will also help you create your Identity Theft Report and a personal recovery plan based on your situation. Questions can be directed to 877-ID THEFT.

How To Recognize a Phishing Scam

Scammers use email or text messages to try to steal your passwords, account numbers, or Social Security numbers. If they get that information, they could get access to your email, bank, or other accounts. Or they could sell your information to other scammers. Scammers launch thousands of phishing attacks like these every day — and they’re often successful.

Scammers often update their tactics to keep up with the latest news or trends, but here are some common tactics used in phishing emails or text messages:

Phishing emails and text messages often tell a story to trick you into clicking on a link or opening an attachment. You might get an unexpected email or text message that looks like it’s from a company you know or trust, like a bank or a credit card or utility company. Or maybe it’s from an online payment website or app. The message could be from a scammer, who might

  • say they’ve noticed some suspicious activity or log-in attempts — they haven’t
  • claim there’s a problem with your account or your payment information — there isn’t
  • say you need to confirm some personal or financial information — you don’t
  • include an invoice you don’t recognize — it’s fake
  • want you to click on a link to make a payment — but the link has malware
  • say you’re eligible to register for a government refund — it’s a scam
  • offer a coupon for free stuff — it’s not real

About Online Threat Alerts (OTA)

Online Threat Alerts or OTA is an anti-cybercrime community that started in 2012. OTA alerts the public to cyber crimes and other web threats.

By alerting the public, we have prevented a lot of online users from getting scammed or becoming victims of cybercrimes.

With the ever-increasing number of people going online, it important to have a community like OTA that continuously alerts or protects those same people from cyber-criminals, scammers and hackers, who are every day finding new ways of carrying out their malicious activities.

Online users can help by reporting suspicious or malicious messages or websites to OTA. And, if they want to determine if a message or website is a threat or scam, they can use OTA's search engine to search for the website or parts of the message for information.

Help maintain Online Threat Alerts (OTA).

Master Dynamic Pivoting in Oracle