PL SQL 'IF or CASE' using variable date - oracle

I am stuck with this and could use advice/help:
Basically, trying to set the date as a variable and then run select statements, using that date variable in the 'WHERE' section of the query. Not sure if I should be using IF or CASE, or neither? If its monday, i want to run 1 set of dates (prev thur and fri) any other day (just sysdate-2 and sysdate-1) Any help is much appreciated!
Code is below:
DECLARE
today_date number;
start_date date;
end_date date;
BEGIN
today_date := to_char(sysdate, 'D');
start_date := case when today_date ='2' then 'sysdate-4' else 'sysdate-2'
end;
end_date := case when today_date ='2' then 'sysdate-3' else 'sysdate-1' end;
SELECT COLUMN A, COLUMN B, COLUMN C, COLUMN D
FROM /*csv*/REPORT_NAME
WHERE COLUMN B between trunc(start_date)+21/24 and trunc(end_date)+21/24 and
BOOK_NAME = 'xxxxxx' and SERVER = 'xxxxxx' and EX_ACTION = 'xxxxx';
end;

You're mixing variables/functions and strings. This should work.
DECLARE
today_date number;
start_date date;
end_date date;
BEGIN
today_date := to_char(sysdate, 'D');
start_date := case when today_date ='2' then sysdate-4 else sysdate-2
end;
end_date := case when today_date ='2' then sysdate-3 else sysdate-1 end;
/* this won't work without declaring a cursor, and returning it to the client
SELECT COLUMN A, COLUMN B, COLUMN C, COLUMN D
FROM REPORT_NAME
WHERE COLUMN B between trunc(start_date)+21/24 and trunc(end_date)+21/24 and
BOOK_NAME = 'xxxxxx' and SERVER = 'xxxxxx' and EX_ACTION = 'xxxxx';
*/
end;
Note you also have some implicit type conversion happening. today_date should probably be char(1) instead.

You don't really need variables for this, and you don't need PL/SQL; you can calculate the dates as part of the where clause using case expressions in that instead:
SELECT /*csv*/ COLUMN_A, COLUMN_B, COLUMN_C, COLUMN_D
FROM REPORT_NAME
WHERE COLUMN_B >= trunc(sysdate) - case to_char(sysdate, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH')
when 'Mon' then 4 else 2 end + 21/24
AND COLUMN_B < trunc(sysdate) - case to_char(sysdate, 'Dy', 'NLS_DATE_LANGUAGE=ENGLISH')
when 'Mon' then 3 else 1 end + 21/24
AND BOOK_NAME = 'xxxxxx'
AND SERVER = 'xxxxxx'
AND EX_ACTION = 'xxxxx';
I've taken the liberty of changing the logic from being based on the day number being 2 to it having a specific day abbreviation, because the value D returns is based on your NLS settings so it could vary (and give unexpected results) for someone else running your code. As day names and abbreviations are also NLS-dependent I've specified the language to use. (You can't specify how D is used in the same way, unfortunately).
I've changed the time window slightly, so it goes from 9pm on one day to up to but not including 9pm the next day. If you use between then it's inclusive at both ends, so runs on consecutive days you both pick up any data at exactly 21:00:00 from the overlapping day. That probably isn't what you want (but if it really is, just change < to <=, or revert to between if you prefer...).
If you want a PL/SQL wrapper you can do the same thing, but you either have to select the results into something like a collection, or use a ref cursor to return the result set to the caller. It isn't clear if you actually need or want to do that though.

have you tried selecting into your variables?
DECLARE
TODAY_DATE NUMBER;
START_DATE DATE;
END_DATE DATE;
BEGIN
TODAY_DATE := TO_CHAR (SYSDATE, 'D');
SELECT CASE WHEN TODAY_DATE = '2' THEN SYSDATE - 4 ELSE SYSDATE - 2 END,
CASE WHEN TODAY_DATE = '2' THEN SYSDATE - 3 ELSE SYSDATE - 1 END
INTO START_DATE, END_DATE
FROM DUAL;
SELECT COLUMN_A, COLUMN_B, COLUMN_C, COLUMN_D
FROM REPORT_NAME /*csv*/
WHERE COLUMN_B BETWEEN TRUNC (START_DATE) + 21 / 24
AND TRUNC (END_DATE) + 21 / 24
AND BOOK_NAME = 'xxxxxx'
AND SERVER = 'xxxxxx'
AND EX_ACTION = 'xxxxx';
END;

Related

PL SQL iterate loop through date range

I have a date range, I am trying to take one date every week through a loop
DECLARE
start_date DATE := TO_DATE('06.01.2021', 'dd.MM.yyyy');
end_date DATE := TO_DATE('26.05.2021', 'dd.mm.yyyy');
active_date DATE;
start_number NUMBER;
end_number NUMBER;
BEGIN
start_number := TO_NUMBER(TO_CHAR(start_date, 'j'));
end_number := TO_NUMBER(TO_CHAR(end_date, 'j'));
active_date := start_date;
FOR cur_r IN start_number..end_number
LOOP
INSERT INTO test_tbl
SELECT snap_date FROM s_act
WHERE
snap_date = active_date;
active_date := TRUNC(active_date) + 7;
COMMIT;
END LOOP;
END;
When I execute this script, only one date 06.01.2021 is written to the table through all iterations.
Where am I making a mistake? How can this be fixed?
You do not need PL/SQL for this and can just use a recursive sub-query:
INSERT INTO test_tbl
WITH date_range ( start_date, end_date ) AS (
SELECT DATE '2021-01-06', DATE '2021-05-26' FROM DUAL
UNION ALL
SELECT start_date + INTERVAL '7' DAY,
end_date
FROM date_range
WHERE start_date + INTERVAL '7' DAY <= end_date
)
SELECT snap_date
FROM s_act s
WHERE EXISTS(
SELECT 1
FROM date_range r
WHERE r.start_date = s.snap_date
);
or a hierarchical query:
INSERT INTO test_tbl
SELECT snap_date
FROM s_act s
WHERE EXISTS(
WITH date_range ( start_date, end_date ) AS (
SELECT DATE '2021-01-06', DATE '2021-05-26' FROM DUAL
)
SELECT 1
FROM date_range r
WHERE r.start_date + ( LEVEL - 1 ) * INTERVAL '7' DAY = s.snap_date
CONNECT BY r.start_date + ( LEVEL - 1 ) * INTERVAL '7' DAY <= r.end_date
);
If you really want to use PL/SQL then you can make it much simpler and iterate by weeks rather than days (however, this will be much less efficient as you will have one INSERT per week and the associated context switch from PL/SQL to SQL compared to the SQL solution which is only a single INSERT for the entire operation and no context switches):
DECLARE
start_date DATE := DATE '2021-01-06';
end_date DATE := DATE '2021-05-26';
active_date DATE := start_date;
BEGIN
LOOP
EXIT WHEN active_date > end_date;
INSERT INTO test_tbl
SELECT snap_date FROM s_act
WHERE snap_date = active_date;
active_date := active_date + INTERVAL '7' DAY;
END LOOP;
END;
/
db<>fiddle here
To me, it looks as if everything is, actually, OK with code you wrote, because active_date gets its new value:
SQL> set serveroutput on;
SQL> declare
2 start_date date := to_date('06.01.2021', 'dd.MM.yyyy');
3 end_date date := to_date('26.05.2021', 'dd.mm.yyyy');
4 active_date date;
5 start_number number;
6 end_number number;
7 begin
8 start_number := to_number(to_char(start_date, 'j'));
9 end_number := to_number(to_char(end_date, 'j'));
10 active_date := start_date;
11
12 for cur_r in start_number..end_number
13 loop
14 dbms_output.put_line('Active_date = ' || to_char(active_date, 'dd.mm.yyyy'));
15 /* Commented, as I don't have your tables nor data
16 INSERT INTO test_tbl
17 SELECT snap_date
18 FROM s_act
19 WHERE snap_date = active_date;
20 */
21 active_date := trunc(active_date) + 7;
22 end loop;
23 -- move COMMIT out of the loop!
24 commit;
25 end;
26 /
Active_date = 06.01.2021
Active_date = 13.01.2021
Active_date = 20.01.2021
<snip>
Active_date = 06.09.2023
Active_date = 13.09.2023
PL/SQL procedure successfully completed.
SQL>
You said
When I execute this script, only one date 06.01.2021 is written to the table through all iterations.
This is piece of code responsible for that:
INSERT INTO test_tbl
SELECT snap_date
FROM s_act
WHERE snap_date = active_date;
I interpret it as:
s_act table contains rows only with snap_date equal to 06.01.2021, or
if it contains rows with other dates, maybe they contain a time component (hours, minutes, seconds) and where condition prevents them to be inserted. If that's so, try with
where trunc(snap_date) = active_date
and see what happens.

Need help creating/using variables in Oracle PL/SQL

I just have a simple code and I hope a simple solution. I just want to create a variable in oracle and use it later on. I'm getting errors about needing a into clause or asking me to enter binds. I've been messing with this for 2 days now. Can someone please explain.
DECLARE
BEGIN_DATE VARCHAR2 (20) := '12/31/2017';
END_DATE VARCHAR2 (20) := '01/01/2019';
BEGIN
SELECT
STATUS
, EQUIPMENT_ID "Eq_ID"
FROM CYNFLEET.V_EQUIPMENT_INFO
WHERE
1=1
AND STATUS Not In ('T','N')
AND SOLD_DATE BETWEEN TO_DATE(:BEGIN_DATE, 'MM/DD/YYYY') AND TO_DATE(:END_DATE, 'MM/DD/YYYY')
AND IN_SERVICE_DATE < TO_DATE(:BEGIN_DATE, 'MM/DD/YYYY') OR STATUS Not In ('T','N')
AND SOLD_DATE Is Null
AND IN_SERVICE_DATE < TO_DATE(:END_DATE, 'MM/DD/YYYY');
END;
Error starting at line : 1 in command -
Error report -
ORA-06550: line 7, column 1:
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:
You need stock the result of your selection in a variable with INTO clause.
DECLARE
BEGIN_DATE VARCHAR2 (20) := '12/31/2017';
END_DATE VARCHAR2 (20) := '01/01/2019';
VAR1 varchar2(100);
VAR2 VARCHAR2(100);
BEGIN
SELECT
STATUS
, EQUIPMENT_ID "Eq_ID"
INTO VAR1, VAR2
FROM CYNFLEET.V_EQUIPMENT_INFO
WHERE
1=1
AND STATUS Not In ('T','N')
AND SOLD_DATE BETWEEN TO_DATE(:BEGIN_DATE, 'MM/DD/YYYY') AND TO_DATE(:END_DATE,
'MM/DD/YYYY')
AND IN_SERVICE_DATE < TO_DATE(:BEGIN_DATE, 'MM/DD/YYYY') OR STATUS Not In ('T','N')
AND SOLD_DATE Is Null
AND IN_SERVICE_DATE < TO_DATE(:END_DATE, 'MM/DD/YYYY') AND ROWNUM <= 1;
END;
But this works when the selection returns one column, if you need more columns, your variable must be a table type.
The error you have shown is fairly select-explanatory; as shown in the documentation, you need to select into something - which is what the error says. You will need to declare additional variables to accept the query results; but only if your query returns a single row. If it returns multiple rows you will need to select into a collection, or use a cursor and iterate over those results.
You will get messages about bind variables because... you are using bind variables in your query. You should not prefix local PL/SQL variables with a colon. Bind variables are set by the client, not within the PL/SQL block.
DECLARE
BEGIN_DATE VARCHAR2 (20) := '12/31/2017';
END_DATE VARCHAR2 (20) := '01/01/2019';
BEGIN
FOR rec IN (
SELECT
STATUS
, EQUIPMENT_ID "Eq_ID"
FROM CYNFLEET.V_EQUIPMENT_INFO
WHERE
1=1
AND STATUS Not In ('T','N')
AND SOLD_DATE BETWEEN TO_DATE(BEGIN_DATE, 'MM/DD/YYYY') AND TO_DATE(END_DATE, 'MM/DD/YYYY')
AND IN_SERVICE_DATE < TO_DATE(BEGIN_DATE, 'MM/DD/YYYY') OR STATUS Not In ('T','N')
AND SOLD_DATE Is Null
AND IN_SERVICE_DATE < TO_DATE(END_DATE, 'MM/DD/YYYY')
)
LOOP
-- do some thing with each row's rec.status and rec."Eq_ID"
dbms_output.put_line('Got status ' || rec.status || ' for ' || rec."Eq_ID");
END LOOP;
END;
/
Or with date-type variables and literal values:
DECLARE
BEGIN_DATE DATE := DATE '2017-12-31';
END_DATE DATE := DATE '2019-01-01';
BEGIN
FOR rec IN (
SELECT
STATUS
, EQUIPMENT_ID "Eq_ID"
FROM CYNFLEET.V_EQUIPMENT_INFO
WHERE
1=1
AND STATUS Not In ('T','N')
AND SOLD_DATE BETWEEN BEGIN_DATE AND END_DATE
AND IN_SERVICE_DATE < BEGIN_DATE OR STATUS Not In ('T','N')
AND SOLD_DATE Is Null
AND IN_SERVICE_DATE < END_DATE
)
LOOP
-- do some thing with each row's rec.status and rec."Eq_ID"
dbms_output.put_line('Got status ' || rec.status || ' for ' || rec."Eq_ID");
END LOOP;
END;
/
You need to be careful as between is inclusive; given your values you might actually want:
AND SOLD_DATE > BEGIN_DATE
AND SOLD_DATE < END_DATE
though it's hard to tell. As dates include times it would be more common on set your begin date to the first date you want information from (not, as it appears, the day before) and then use SOLD_DATE >= BEGIN_DATE.
You also need to be careful about operator precedence and evaluation order, as you are mixing and and or. It's better to be explicit about which bits of logic are tied together; it looks like you might want:
AND STATUS Not In ('T','N')
AND (
(
SOLD_DATE > BEGIN_DATE
AND SOLD_DATE < END_DATE
AND IN_SERVICE_DATE < BEGIN_DATE
)
OR
(
SOLD_DATE Is Null
AND IN_SERVICE_DATE < END_DATE
)
)
If you did actually want to use bind variables and just display the output then you would set those in your client and use plain SQL; e.g. in SQL*Plus, SQL Developer, SQLcl and possibly others:
-- client variables
var BEGIN_DATE VARCHAR2(10);
var END_DATE VARCHAR2(10);
-- set variables via a PL/SQL block
BEGIN
BEGIN_DATE := '2017-12-31';
END_DATE := '2019-01-01';
END;
/
-- plain SQL query
SELECT
STATUS
, EQUIPMENT_ID "Eq_ID"
FROM CYNFLEET.V_EQUIPMENT_INFO
WHERE
1=1
AND STATUS Not In ('T','N')
AND STATUS Not In ('T','N')
AND (
(
SOLD_DATE > TO_DATE(:BEGIN_DATE, 'YYYY-MM-DD')
AND SOLD_DATE < TO_DATE(:END_DATE, 'YYYY-MM-DD')
AND IN_SERVICE_DATE < TO_DATE(:BEGIN_DATE, 'YYYY-MM-DD')
)
OR
(
SOLD_DATE Is Null
AND IN_SERVICE_DATE < TO_DATE(:END_DATE, 'YYYY-MM-DD')
)
);
Now those variable references in the query are actually bind variables, so they do have the colon prefix.
You declare and use variables like this:
declare
someVar varchar2(20) := 'Hello, world!';
begin
dbms_output.put_line(someVar);
end;
You populate variables from a query using
select a, b, c into x, y, z
from ...
where x, y and z are variables.

finding working days in sql

I have a table 'country_holiday' which has two columns country_id and holiday_dt , this table doesnt have entries for weekends.
I need to write a procedure that takes 3 inputs start_dt , end_dt and country_id and then iterate over all working dates between the two given dates for given country_id
I tried writing something like this which doesnt work ( i get a blank cursor)
create or replace procedure check_data(p_start_date in date, p_end_date in date, p_country_id in number)
IS
curr_date date;
CURSOR v_buss_days is select p_start_date + rownum -1
from all_objects where rownum <= p_end_date - p_start_date +1
and to_char(p_start_date+rownum-1,'DY') not in ('SAT','SUN')
and p_start_date + rownum -1 not in (select holiday_dt from country_holiday where country_id = p_country_id)
BEGIN
for curr_date in v_buss_days
LOOP
dbms_output.put_line(curr_date)
END LOOP;
END
I tried running the query
select p_start_date + rownum -1
from all_objects where rownum <= p_end_date - p_start_date +1
and to_char(p_start_date+rownum-1,'DY') not in ('SAT','SUN')
this gives me 0 rows with p_start_date='01 dec 2013' and p_end_date='31 dec 2013' , seems like my query to populate cursor is incorrect.
After populating the cursor correctly i face issue
thanks for your help , indeed it works .... but facing issue when i try to use in procedure ....
create or replace procedure check_data(p_start_date in date, p_end_date in date, p_cntry_id in number)
IS
curr_num_of_empoyee number;
curr_date date;
CURSOR v_buss_days is select work_date from
( with dates as
( select p_start_date dt_start, p_end_date dt_end from dual )
select dt_start + (level-1) work_date from dates
connect by level <= (dt_end - dt_start + 1 )
) wdates
where work_date not in ( select HOLIDAY_DATE
from country_holiday
where country_id = p_cntry_id)
and to_char(work_date,'DY') not in ('SAT','SUN')
order by work_date;
BEGIN
for curr_date in v_buss_days
LOOP
select count(*) into curr_num_of_empoyee from employee_details where country_id = p_cntry_id and data_dt = curr_date;
END LOOP;
END;
Error is
19/101 PLS-00382: expression is of wrong type
seems like issue is in part "data_dt = curr_date"
Here is a query I put on SQLFiddle. Remove the WITH clause and replace with your procedure date parameters. You can use the combination of CONNECT BY and LEVEL to generate a set of rows with increasing numeric values. Then, add that to your start date, and filter out from your holiday table and weekends.
select work_date from
(
with dates as
( select to_date('01/01/2014','MM/DD/YYYY') dt_start,
to_date('01/10/2014','MM/DD/YYYY') dt_end
from dual
)
select dt_start + (level-1) work_date
from dates
connect by level <= (dt_end - dt_start + 1 )
) wdates
where work_date not in ( select holiday_dt
from country_holiday
where country_id = 1)
and to_char(work_date,'DY') not in ('SAT','SUN')
order by work_date

Arithmetic operations on Date values in Oracle

I am struggling with some case...
there is table, in which I have employee attendance records, for example:
for empID=1;
empID time Type date
-------------------------------
1 9:22 in sameday
1 11:23 out sameday
1 14:35 in sameday
1 16:21 out sameday
particularly, I want some fn/procedure that will take an EmpID and DATE parameters, and then based on this data if I'll write: select proc(EmployeeID, Date) from dual(or maybe some other table?) it should do such a work:
take first couples in table (table is ordered be ascending as default order), then calculate FROM first OUT (11:23) to first IN(9:22) time, save that time somewhere (int tempResult) and then calculate second couple, and calculate second tempResult and in finalResult, it should count the total time, like finalResult+=finalResult+tempResult (if it has been an iterations in loop);
I think it would be done someway like, in foreach (or whatever it is in pl/sql oracle) take first select with top result, then, second.. and so forth... and on each iteration calculate desire goal.
so.. logics is ok with me I think :), but the problem is that I'm not that familiar with PL/SQL, if it had been written in Java it should have come easy to me.
I will pay lots of Thanks to some one who will help me...
its crucial for me to day.
thanks in advance.
I have Date and Time is separate columns, like:
date time
----------------------
11-09-2013 12:34
so, I made little change like this
FOR rec IN
( SELECT t.EID, to_char(t.devent_date, 'DD.MM.YY') ||' '|| t.RegTime, t.acttype from turnicate_ie t WHERE t.EID = i_emp_id ORDER BY t.EID
)
LOOP
but it states that package or function is in incorrect state...
(t.devent_date is 11.09.2013, t.RegTime is 16:23)
The below will give you some idea of using plsql:
you need to use many logic of calculating total working hours, like multiple inputs within same time, multiple empId etc.
create table my_test ( empId number, log_time date, type varchar2(3));
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 09:22:00 am', 'dd/mon/yyyy hh:mi:ss am'), 'in');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 11:23:00 am', 'dd/mon/yyyy hh:mi:ss am'), 'out');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 02:35:00 pm', 'dd/mon/yyyy hh:mi:ss pm'), 'in');
INSERT INTO my_test VALUES( 1, to_date('11/sep/2013 04:21:00 pm', 'dd/mon/yyyy hh:mi:ss pm'), 'out');
CREATE OR REPLACE
FUNCTION total_hours(
i_emp_id IN NUMBER)
RETURN VARCHAR2
IS
l_total_seconds NUMBER := 0;
in_time DATE;
l_total_time VARCHAR2(20);
BEGIN
FOR rec IN
( SELECT log_time, type FROM my_test WHERE empid = i_emp_id ORDER BY log_time
)
LOOP
IF rec.TYPE = 'in' AND in_time IS NULL THEN
in_time := rec.log_time;
END IF;
IF rec.TYPE = 'out' AND in_time IS NOT NULL THEN
l_total_seconds := (rec.log_time - in_time)*24*60*60 + l_total_seconds;
in_time := NULL;
END IF;
END LOOP;
SELECT TO_CHAR(TRUNC(l_total_seconds/3600), 'FM999999990')
|| 'hh '
|| TO_CHAR(TRUNC(mod(l_total_seconds,3600)/60), 'FM00')
|| 'mm '
|| TO_CHAR(mod(l_total_seconds,60), 'FM00')
||'ss'
INTO l_total_time
FROM dual;
RETURN l_total_time;
END;
/
SELECT total_hours(1) from dual;

SQL Developer - Using start and end date variables in where clause of my query

I have a query that I run everyday that requires a StartDate and EndDate value. The StartDate and EndDate used to be a manual input but I am trying to get away from that and calculate the StartDate and EndDate to be used in the query. I've developed code to capture the StartDate and EndDate in variables:
DECLARE
c_DateMask VARCHAR2(20) := 'DD-Mon-YYYY';
c_TimeMask VARCHAR2(20) := 'HH24:MI';
v_Month char(4) := 'Prev';
v_StartDate date;
v_EndDate date;
v_Environment char(7) := 'Prod';
BEGIN
if v_MONTH = 'Prev'
THEN
select TO_DATE ('01-' || TO_CHAR (ADD_MONTHS (SYSDATE, -1),'mon-yyyy')) into v_StartDate from dual;
select Last_day(TO_DATE('01-' || TO_CHAR (ADD_MONTHS (SYSDATE, -1),'mon-yyyy'))) into v_EndDate from dual;
ELSE
select TO_DATE ('01-' || TO_CHAR (ADD_MONTHS (SYSDATE, 0),'mon-yyyy')) into v_StartDate from dual;
CASE
WHEN v_Environment = 'Prod'
THEN
-- Production Environment --
select
to_char(sysdate, 'dd-Mon-yyyy ') ||
case
when to_char(sysdate, 'mi') between 00 and 20
then to_char(sysdate, 'hh24')-1||':58'||':00'
when to_char(sysdate, 'mi') between 21 and 40
then to_char(sysdate, ' hh24')||':18'||':00'
when to_char(sysdate, 'mi') between 41 and 60
then to_char(sysdate, ' hh24')||':38'||':00'
END
into v_EndDate from dual;
WHEN v_Environment = 'OldTest'
THEN
-- Test Environment --
select
to_char(sysdate, 'dd-Mon-yyyy ') ||
case
when to_char(sysdate, 'mi') between 10 and 30
then to_char(sysdate, 'hh24')||':08'||':00'
when to_char(sysdate, 'mi') between 31 and 50
then to_char(sysdate, ' hh24')||':28'||':00'
when to_char(sysdate, 'mi') between 51 and 60
then to_char(sysdate, ' hh24')||':48'||':00'
END
into v_EndDate from dual;
end case;
end if;
I then want to use the variables in my select statement below:
-----------------
/* KPI Figures */
-----------------
SELECT
SysDate as RunTime
, v_StartDate
, v_EndDate
, TTM_OFF_CONTRIBUTOR
, SUM(NVL(TTM_PER_OFF_FEE,0)) Fees
FROM [Table]
Where
TTM_PROCESSED_DATE >= v_StartDate
AND TTM_PROCESSED_DATE <= v_EndDate
group by SysDate, v_StartDate, v_EndDate, TTM_OFF_CONTRIBUTOR
END;
It all works up until when I try to use the variable values in the KPI Figures query. What am I missing?
Update:
Regarding Phil's answer: I tried but it didn't work and I get the following error:
PLS-00428: an INTO clause is expected in this SELECT statement.
I am sure I saw another response yesterday which is now gone relating to being able to assign multiple values to variables or something.
Is that what I need and how would I do that?
The variables v_StartDate and v_EndDate are only in scope within the PL/SQL block where they are declared. It looks like you are then trying to use them outside the block in a separate query. To do that you will need to create SQL Developer bind variables outside the PL/SQL block like this:
var v_start_date varchar2(11)
var v_end_date varchar2(11)
Then reference these as bind variables in both the PL/SQL block and the SQL query:
declare
...
begin
....
:v_start_date := '01-' || TO_CHAR (ADD_MONTHS (SYSDATE, -1), 'mon-yyyy');
:v_end_date := TO_CHAR(Last_day(TO_DATE('01-'
|| TO_CHAR (ADD_MONTHS (SYSDATE, -1),'mon-yyyy'))));
-- (NB No need to select from dual)
...
end;
SQL:
...
Where
TTM_PROCESSED_DATE >= TO_DATE(:v_StartDate)
AND TTM_PROCESSED_DATE <= TO_DATE(:v_EndDate)
Note that these variables cannot be declared with a type of DATE, so they need to be converted back to dates in the query (using the correct format mask).
I suggest you wrap the startdate/enddate in two functions (stored procedures)
create or replace function get_startdate(p_month in varchar2, p_env in varchar2)
return date
is
l_return date;
begin
... logic goes here ...
return l_return;
end;
Similar for enddate.
Then use those functions in your query:
SELECT
SysDate as RunTime
, get_startdate('Prev', 'Prod')
, get_enddate('Prev', 'Prod')
, TTM_OFF_CONTRIBUTOR
, SUM(NVL(TTM_PER_OFF_FEE,0)) Fees
FROM [Table]
Where
TTM_PROCESSED_DATE >= get_startdate('Prev', 'Prod')
AND TTM_PROCESSED_DATE <= get_enddate('Prev', 'Prod')
group by SysDate, get_startdate('Prev', 'Prod'), get_enddate('Prev', 'Prod'), TTM_OFF_CONTRIBUTOR
Sidenote: please consider a more concise version to determine first and last day of a month. You also can just assign a value to a variable without using SELECT INTO, e.g.
startdate := trunc(sysdate, 'MM'); -- first day of current month
enddate := last_day(trunc(sysdate, 'MM')); -- last day of current month
To calculate with hours, you could add/subtract a number of minutes, that's more readable than all the to_date/to_char conversions you do, e.g.
enddate := trunc(sysdate, 'HH24');
case
when to_char(sysdate, 'mi') between 00 and 20 then enddate := enddate - 2/(24*60);
when to_char(sysdate, 'mi') between 21 and 40 then enddate := enddate + 18/(24*60);
when to_char(sysdate, 'mi') between 41 and 60 then enddate := enddate + 38/(24*60);
end;
Good luck, Martin
I think the issue is with your query and aliases. Try
SELECT
SysDate as RunTime
, v_StartDate AS StartDate
, v_EndDate AS EndDate
, TTM_OFF_CONTRIBUTOR
, SUM(NVL(TTM_PER_OFF_FEE,0)) Fees
FROM [Table]
Where
TTM_PROCESSED_DATE >= v_StartDate
AND TTM_PROCESSED_DATE <= v_EndDate
group by RunTime, StartDate, EndDate, TTM_OFF_CONTRIBUTOR

Resources