1

I need to create a list of objects in PL/SQL - postgres and return it as table to user.

Here is the scenario. I have two table called

create table ProcessDetails(
    processName varchar,
    processstartdate timestamp,
    processenddate timestamp);

create table processSLA(
    processName varchar,
    sla numeric);

Now I need to loop over all the records in processDetails table and check which records for each activity type has breached sla, within sla and those that are more 80% of sla.

I would need help in understanding how to loop over records and create a collection in which for each processtype I have details required.

sample data from processdetails table

    ProcessName  processstartdate             processenddate   
----------------------------------------------------- 
    "Create"    "2018-12-24 13:11:05.122694"    null
    "Delete"    "2018-12-24 12:12:24.269266"    null
    "Delete"    "2018-12-23 13:12:31.89164"     null
    "Create"    "2018-12-22 13:12:37.505486"    null

    processSLA
       ProcessName  sla(in hrs) 
    ---------------------------------
       Create       1
       Delete       10

And the output will look something like this

ProcessName  WithinSLA(Count)  BreachedSLA(Count) Exceeded80%SLA(Count)
---------------------------------------------------------------------
Create                 1                1              3
Delete                 1                2              1   
2
  • Are you using PostgreSQL or Oracle?
    – Andomar
    Dec 24, 2018 at 10:28
  • I am using postgresql. Dec 24, 2018 at 10:39

2 Answers 2

0

For each SLA, you can look up all corresponding process details with a join. The link between two joined tables specified in a join condition. For your example, using (processName) would work.

To find processes that have exceeded the SLA, say that the allowed end date is smaller than the actual end date:

select  processName
,       count(case when det.processstartdate + interval '1 hour' * sla.hours >=
            coalesce(det.processenddate, now()) then 1 end) as InSLA
,       count(case when det.processstartdate + interval '1 hour' * sla.hours <
            coalesce(det.processenddate, now()) then 1 end) as BreachedSLA
,       count(case when det.processstartdate + interval '1 hour' * 0.8 * sla.hours <
            coalesce(det.processenddate, now()) then 1 end) as 80PercentSLA
from    processSLA sla
left join
        ProcessDetails det
using   (processName)
group by
        processName
2
  • I need data something like this.. ProcessName WithinSLA(Count) BreachedSLA(Count) Exceeded80%SLA(Count) Create 1 1 3 Dec 24, 2018 at 10:30
  • You can group by process name, and then count the number of rows within or without the SLA
    – Andomar
    Dec 24, 2018 at 10:50
0

You can join both tables and use conditional aggregation based on the calculation of the difference between the timestamps.

Something like that:

SELECT count(CASE
               WHEN extract(EPOCH FROM pd.processenddate - pd.processstartdate) / 3600 < ps.sla * .8 THEN
                1
             END) "less than 80%",
       count(CASE
               WHEN extract(EPOCH FROM pd.processenddate - pd.processstartdate) / 3600 >= ps.sla * .8
                    AND extract(EPOCH FROM pd.processenddate - pd.processstartdate) / 3600 <= ps.sla THEN
                1
             END) "80% to 100%",
       count(CASE
               WHEN extract(EPOCH FROM pd.processenddate - pd.processstartdate) / 3600 > ps.sla THEN
                1
             END) "more than 100%"                
       FROM processdetails pd
            INNER JOIN processsla ps
                       ON ps.processname = pd.processname;
5
  • Thanks, but i need other two columns in the answer. I need those within SLA, exceeded SLA too along with 80% of the sla.. how to I get those.. I need the count of records, not the records it self. Dec 24, 2018 at 10:36
  • @PradeepSreeram: If you want them all, simply remove the WHERE clause.
    – sticky bit
    Dec 24, 2018 at 10:38
  • I will not get the count. I will get records. What I need to count of each type of records... those within sla , breaded and nearing 80% for each process type. The above query will return multiple rows. Dec 24, 2018 at 10:49
  • Thanks for the answer. processend will be null sometimes. if processenddate is null it means the process is still pending.. how to handle that? Dec 24, 2018 at 12:02
  • Thanks for the answer! Dec 24, 2018 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.