Skip to main content
Solved

How to use dynamic pivot on Quick Reports?

  • October 4, 2023
  • 4 replies
  • 197 views

hhy38
Superhero (Customer)
Forum|alt.badge.img+16
  • Superhero (Customer)
  • 326 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

4 replies

EntNadeeL
Hero (Partner)
Forum|alt.badge.img+10
  • Hero (Partner)
  • 182 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)
  • 326 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)
  • 182 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)
  • 326 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.