Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I continually add a pl/sql return value to the last recorded value in a table?

3988598Jul 28 2019 — edited Jul 29 2019

So I am quite new to SQL so please be gentle.

I am using Oracle Apex to generate a bar chart showing the amount of storage used in a particular database over the last 12 months. In this chart I am trying to add an additional feature where the chart will show (via a line) how much storage will likely be used on a fortnightly basis over the course of the next three months.

I have created a PL/SQL function (GET_PREDICTED_VALUE) which generates the anticipated value I require, but I am having trouble using that value to show the predicted trend. If the generated anticipated value is positive, that’s how much more storage is needed. If the anticipated value is negative, that’s how much less storage is needed (the arguments sent into this function are how many days ago do I want to draw the ‘storage used’ values. 30 will be 30 days ago, 150 is 150 days ago etc).

What I would like to do is use this anticipated value and add it to the last ‘current storage used’ value recorded in the database. I would like the anticipated value to be continually added so that a future trend can be observed.

For example, if the last ‘current storage used’ value saved in the database is 50, and the anticipated value is 3, then the value for the immediate future date should be 53. And the next future value should be 56, and the next should be 59, and so on up to three months ahead.

This is the code I have so far, and it works insofar that the chart I need does get generated and the trend line runs across the previous year and then the future three months. But for the future three months, I am only able to get the trend line to represent the anticipated value, not the value continually added E.g again, if the anticipated value is 3, my trend line shows 3 across all future dates.

WITH src AS ( SELECT GET_PREDICTED_VALUE(1, 30, 60, 90, 120, 150, 180) AS predictedValue FROM DUAL),mydays as ( select level mylevel, sysdate + level as futureDate from dual connect by level <= 90 ),future_values as ( SELECT MAX(src.predictedValue) AS "ASM Used", TO_CHAR(TRUNC(futureDate,'IW') ,'DD/MM/YY') AS "Capture Date", TRUNC(futureDate,'IW') AS capture_date_order FROM mydays CROSS JOIN src GROUP BY TRUNC(futureDate,'IW') ORDER BY TRUNC(futureDate,'IW') ),SRC_back AS ( SELECT TRUNC(capture_date,'IW') AS week, database_name, MAX((os_usable_total_mb-os_usable_free_mb)) AS ASMUsed FROM tablespace_used WHERE capture_date >= (SYSDATE - 360) GROUP BY TRUNC(capture_date,'IW'), database_name, tablespace_name),back_values as ( SELECT /*+ parallel(a, 8) */ TO_CHAR(week,'DD/MM/YY') AS "Capture Date", week AS capture_date_order, ROUND( MAX(ASMUsed/1024/1024 ) ) AS "ASM Used" FROM src_back a WHERE week >= (SYSDATE - 360) GROUP BY TO_CHAR(week,'DD/MM/YY'), week ORDER BY 2 ) select "Capture Date", capture_date_order, "ASM Used" from back_valuesunion select "Capture Date", capture_date_order, "ASM Used" from future_valuesorder by 2

I hope my explanation is clear, and if anyone could let me know what I have to do to get my trend line running the way I need?

(This is an image of how my chart looks so far):

chart.png

Comments

i don't see that behavior, also on 4.1.3

monitor_session_query.jpg

Buntoro

Hi Jeff,

It seems to be program specific problem.

We have an old ETL Tools called Sagent and it seems all queries fired from that tool are coming in this one-line format.

Notice the horizontal scroll bar at the bottom.

Please don't mind the Master layout of the Session Monitor, which I adjust to be more informational with Gauge Bar and so on.

pastedImage_2.png

For the time being, I need to open another worksheet, copy and paste that line and use Ctrl + F7 to re-format.

pastedImage_4.png

Thank you for clarifying.

Regards,

Buntoro

Buntoro

Hi Jeff,

On second thought.

I need to unmark as correct.

Probably this problem happened on DML command (insert / update / delete) only.

Here is another example, query coming from user tool (PL/SQL Developer).

pastedImage_0.png

I try to dig into the query for "Active SQL" and try to execute it myself on worksheet.

Notice the SID = 2024 which I get from the above.

I thought that it will return one line, which justify why SQL Dev show single line output.

However, as you can see it is not.

pastedImage_1.png

The question still hold.

Regards,

Buntoro

you customized the report...does it do this on the original Tools > Monitor Session screen?

Buntoro

I copy the Report from the Template (All Reports - Data Dictionary Reports - Database Administration - Sessions - Sessions).

And paste it under User Defined Reports, and name it Session Browser instead.

Every time I open Tools - Monitor Sessions, it seems to be pointing out to the User Defined Reports - Session Browser (the one that I modified).

Regards,

Buntoro

thatJeffSmith-Oracle

what's the sql behind your report? compare it to mine in the screenshot up top

tools > monitor sessions going to your custom report...that would be weird. maybe something else is happening

Buntoro

Hi Jeff,

Mine is :

select replace(q.SQL_TEXT, chr(0), chr(10) || chr(13)) sql_text

  from gv$session s, v$sqltext_with_newlines q

where  s.sql_address = q.address

  and  s.sql_hash_value = q.hash_value

  and  s.sid = :SID

order by q.piece

I also tried yours too. But on detail tab produces the same result.

select replace(q.SQL_FULLTEXT,chr(0)) sql_text

  from gv$session s, gv$sql q

where  s.sql_address = q.address

  and  s.sql_hash_value = q.hash_value

  and  s.sid = :SID

FYI, if executed on Worksheet

My query will produce multiple rows result

Your query will produce single line result

Nevertheless, both produce the same single line output on Detail tab.

Regards,

Buntoro

1 - 7

Post Details

Added on Jul 28 2019
2 comments
291 views