Skip to main content
Solved

How to use dynamic pivot on Quick Reports?


hhy38
Superhero (Customer)
Forum|alt.badge.img+16
  • Superhero (Customer)
  • 318 replies

Hi,

 

I need to create a Quick Report for a department. For this requirement, I should use pivot. However, the pivot columns must be dynamic. The columns show years and months.

To make the query dynamic, I wrote a function that returns a query text. However, when I query with Dual table. It has been returning query text, not the query result. 

 

The Function

CREATE OR REPLACE FUNCTION pivot_function RETURN VARCHAR2 AS
	dates       VARCHAR2(32000);
	pivot_query VARCHAR2(32000);
BEGIN

	SELECT listagg('''' || vade || ''' AS "' || vade || '"', ', ') within GROUP(ORDER BY vade)
	INTO   dates
	FROM   (

			SELECT DISTINCT to_char(t.due_date, 'YYYY') || '-' || to_char(t.due_date, 'MM') vade
			FROM   ifsapp.table_name t
			WHERE  t.odendi_bilgisi = '-'
				   AND t.is_advance_peyment_db = '+');

	pivot_query := 'SELECT *
                  FROM (
                    SELECT nvl(t.project_id, ''Projesiz'') "Proje No",
                         t.currency_rate,
                         to_char(t.due_date, ''YYYY'') || ''-'' || to_char(t.due_date, ''MM'') vade
                    FROM   ifsapp.table_name t
                    WHERE  t.odendi_bilgisi = ''-''
                         AND t.is_advance_peyment_db = ''+''
                  )
                  PIVOT (
                    SUM(currency_rate)
                    FOR vade IN (' || dates || ')
                  )';

	RETURN pivot_query;
END;

 

The Functions Result (Wrong - Queries itself)

 

The Actual Result (Must Be 😀)

 

Do you have any other suggestions?

Best answer by EntNadeeL

Hi @hhy38 

I can share it here. that is fine.

Sorry it took some time to find it. 

Let me know how it goes.

Cheers

View original
Did this topic help you find an answer to your question?

4 replies

EntNadeeL
Hero (Partner)
Forum|alt.badge.img+10
  • Hero (Partner)
  • 179 replies
  • October 4, 2023

Hi @hhy38 

I have this example with me. I will share the code with you. 

your approach is fine but i cant exactly fine the problem here. Let me get back to you.

 

 


hhy38
Superhero (Customer)
Forum|alt.badge.img+16
  • Author
  • Superhero (Customer)
  • 318 replies
  • October 4, 2023

@EntNadeeL Hi,

 

If you share here it's going to be perfect. Because someone can face the same problem.

Should I reach you by private message? Or Linkedin is good for you?


EntNadeeL
Hero (Partner)
Forum|alt.badge.img+10
  • Hero (Partner)
  • 179 replies
  • Answer
  • October 4, 2023

Hi @hhy38 

I can share it here. that is fine.

Sorry it took some time to find it. 

Let me know how it goes.

Cheers


hhy38
Superhero (Customer)
Forum|alt.badge.img+16
  • Author
  • Superhero (Customer)
  • 318 replies
  • October 4, 2023

@EntNadeeL  No worries it is okay. The solution is very smart.

Probably you schedule the procedure. So, the procedure refreshes the query inside the view.  

How about permission? Do we need to give permission for this view again?  

I don’t think it is needed. Because we replace the View. But, I just wanted to ask.

 

Thank you for the solution.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings