Loop query with a variable in Oracle

Svperstar

I have a query based on a date with get me the data I need for a given day (lets say sysdate-1):

SELECT TO_CHAR(START_DATE, 'YYYY-MM-DD') "DAY",
  TO_CHAR(TRUNC(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),3600)/60),'FM00') || ':'
  || TO_CHAR(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),60),'FM00') "DURATION (mm:ss)"
FROM UI.UIS_T_DIFFUSION
WHERE APPID IN ('INT', 'OUT', 'XMD','ARPUX')
AND PSTATE = 'OK'
AND TO_CHAR(START_DATE, 'DD-MM-YYYY') = TO_CHAR( sysdate-1, 'DD-MM-YYYY')
AND ROWNUM <= 22
GROUP BY TO_CHAR(START_DATE, 'YYYY-MM-DD');

Gives me this (as expected):

╔════════════╦══════════╗
║    DAY     ║ DURATION ║
╠════════════╬══════════╣
║ 2016-02-28 ║       303║
╚════════════╩══════════╝

Now I'm trying to add a loop to get the results for each day since 10-10-2015. Somehting like this:

╔═══════════╦══════════╗
║    DAY    ║ DURATION ║
╠═══════════╬══════════╣
║ 2016-02-28║       303║
╠═══════════╬══════════╣
║ 2016-02-27║       294║
╠═══════════╬══════════╣
║        ...║       ...║
╠═══════════╬══════════╣
║ 2015-10-10║        99║
╚═══════════╩══════════╝

I've tried to put the query inside a loop:

DECLARE
  i NUMBER := 0;
BEGIN
  WHILE i <= 142
  LOOP
    i := i+1;
    SELECT TO_CHAR(START_DATE, 'YYYY-MM-DD') "DAY",
    TO_CHAR(TRUNC(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),3600)/60),'FM00') || ':'
    || TO_CHAR(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),60),'FM00') "DURATION (mm:ss)"
    FROM UI.UIS_T_DIFFUSION
    WHERE APPID IN ('INT', 'OUT', 'XMD','ARPUX')
    AND PSTATE = 'OK'
    AND TO_CHAR(START_DATE, 'DD-MM-YYYY') = TO_CHAR(sysdate - i, 'DD-MM-YYYY')
    AND ROWNUM <= 22
    GROUP BY TO_CHAR(START_DATE, 'YYYY-MM-DD');
  END LOOP;
END;

but I'm getting this error:

Error report -
ORA-06550: line 7, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Can anyone tell me how to accomplish this?

Michael Broughton

While bastihermann gave you the query to get all of those values in a single result set, if you want to understand the issue with your pl/sql block, the following should simplify it for you. The error relates to the fact that, in pl/sql you need to select INTO local variables to contain the data for reference within the code.

To correct (and simplify with a FOR LOOP) your block:

DECLARE
  l_day      varchar2(12);
  l_duration varchar2(30);;
BEGIN
  -- don't need to declare a variable for an integer counter in a for loop
  For i in 1..142
  LOOP
    SELECT TO_CHAR(START_DATE, 'YYYY-MM-DD'),
    TO_CHAR(TRUNC(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),3600)/60),'FM00') || ':'
    || TO_CHAR(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),60),'FM00') 
    INTO l_Day, l_duration
    FROM UI.UIS_T_DIFFUSION
    WHERE APPID IN ('INT', 'OUT', 'XMD','ARPUX')
    AND PSTATE = 'OK'
    AND TO_CHAR(START_DATE, 'DD-MM-YYYY') = TO_CHAR(sysdate - i, 'DD-MM-YYYY')
    AND ROWNUM <= 22
    GROUP BY TO_CHAR(START_DATE, 'YYYY-MM-DD');
    -- and here you would do something with those returned values, or there isn't much point to this loop. 
  END LOOP;
END;

Assuming you needed to do something with those values and want even more efficient, you could simplify even further with a cursor loop;

BEGIN
  -- don't need to declare a variable for an integer counter in a for loop
  For i_record IN
    (SELECT TO_CHAR(START_DATE, 'YYYY-MM-DD') the_Day,
    TO_CHAR(TRUNC(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),3600)/60),'FM00') || ':'
    || TO_CHAR(MOD(ROUND(AVG((END_DATE - START_DATE)*86400),0),60),'FM00') the_duration
    FROM UI.UIS_T_DIFFUSION
    WHERE APPID IN ('INT', 'OUT', 'XMD','ARPUX')
    AND PSTATE = 'OK'
    AND TO_CHAR(START_DATE, 'DD-MM-YYYY') <= TO_CHAR( sysdate, 'DD-MM-YYYY')
    AND TO_CHAR(START_DATE, 'DD-MM-YYYY') >= TO_CHAR( sysdate-142, 'DD-MM-YYYY')  
    AND ROWNUM <= 22
    GROUP BY TO_CHAR(START_DATE, 'YYYY-MM-DD')
    ORDER BY to_char(start_date,'dd-mm-yyyy')
    )
  LOOP
    -- and here you would do something with those returned values, but reference them by record_name.field_value. 
    -- For now I will put in the NULL; command to let this compile as a loop must have at least one command inside.
    NULL;
  END LOOP;
END;

Hope that helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

oracle: query timestamp with variable dates

From Dev

Oracle Recursive Query Connect By Loop in data

From Dev

Outputting variable result from Oracle loop

From Dev

Declare a variable in Oracle SQL to use in a query

From Dev

How to pass a variable to a query in Oracle stored procedure?

From Dev

Using variable in Oracle Connection Sql Query

From Dev

SQL query in Foreach loop using array variable

From Dev

SQL query in Foreach loop using array variable

From Dev

Oracle SQL - Reusing a bind variable on a query called through JDBC

From Dev

Oracle Trigger PLS-00103. Query with multiple rows in temporal variable

From Dev

Talend-Unable to use Context variable in oracle query

From Dev

How to use global variable in oracle stored procedure for query

From Dev

How to use loop variable inside pandas df.query()

From Dev

Generating multiple query results using Foreach Loop in one variable

From Dev

Generating multiple query results using Foreach Loop in one variable

From Dev

Oracle PL/SQL: How to write a loop in order to print all the components of a variable of Oracle customized type?

From Dev

How to check within query results for variable to change same query's while loop?

From Dev

Loop variable error in for loop

From Dev

Loop variable error in for loop

From Dev

Query with variable

From Dev

How to use Oracle to query row column A value as input on second row column B and loop all relation records

From Dev

Oracle - Anonymous Procedure to loop through multiple tables (dynamically) - Query returning multiple rows

From Dev

Variable inside a variable in a loop

From Dev

Dapper and Oracle parametrized query - ORA-01036: illegal variable name/number

From Dev

oracle dynamic query issue: how to get value into a variable which is coming as a part of string

From Dev

Oracle query under PHP isn't getting executed with parsing, may be due to date variable of PHP so how to convert date variable into string?

From Dev

Oracle Query error with debugged query

From Dev

PHP 1st while loop's output result as 2nd while loop's variable for query

From Dev

PHP Database Query: How to return the results from a while loop to a Javascript Variable?

Related Related

  1. 1

    oracle: query timestamp with variable dates

  2. 2

    Oracle Recursive Query Connect By Loop in data

  3. 3

    Outputting variable result from Oracle loop

  4. 4

    Declare a variable in Oracle SQL to use in a query

  5. 5

    How to pass a variable to a query in Oracle stored procedure?

  6. 6

    Using variable in Oracle Connection Sql Query

  7. 7

    SQL query in Foreach loop using array variable

  8. 8

    SQL query in Foreach loop using array variable

  9. 9

    Oracle SQL - Reusing a bind variable on a query called through JDBC

  10. 10

    Oracle Trigger PLS-00103. Query with multiple rows in temporal variable

  11. 11

    Talend-Unable to use Context variable in oracle query

  12. 12

    How to use global variable in oracle stored procedure for query

  13. 13

    How to use loop variable inside pandas df.query()

  14. 14

    Generating multiple query results using Foreach Loop in one variable

  15. 15

    Generating multiple query results using Foreach Loop in one variable

  16. 16

    Oracle PL/SQL: How to write a loop in order to print all the components of a variable of Oracle customized type?

  17. 17

    How to check within query results for variable to change same query's while loop?

  18. 18

    Loop variable error in for loop

  19. 19

    Loop variable error in for loop

  20. 20

    Query with variable

  21. 21

    How to use Oracle to query row column A value as input on second row column B and loop all relation records

  22. 22

    Oracle - Anonymous Procedure to loop through multiple tables (dynamically) - Query returning multiple rows

  23. 23

    Variable inside a variable in a loop

  24. 24

    Dapper and Oracle parametrized query - ORA-01036: illegal variable name/number

  25. 25

    oracle dynamic query issue: how to get value into a variable which is coming as a part of string

  26. 26

    Oracle query under PHP isn't getting executed with parsing, may be due to date variable of PHP so how to convert date variable into string?

  27. 27

    Oracle Query error with debugged query

  28. 28

    PHP 1st while loop's output result as 2nd while loop's variable for query

  29. 29

    PHP Database Query: How to return the results from a while loop to a Javascript Variable?

HotTag

Archive