Dynamic SQL query
Hi allI have a procedure that takes as an input parameter v_emp_id which is of type varchar.
emp_id are sent separated by commas to v_emp_id. Ex: v_emp_id = 7,2,1
In my code I use a cursor for loop recover data from the table emp like this
I'm in (select * from emp where emp_id in v_emp_id) loop
-----
end loop;
It does not work. How do I use dynamic sql statements to retrieve data by using the CURSOR LOOP FOR only?
Thanks in advance
MR Marie wrote:
How do I use dynamic sql statements to retrieve data by using the CURSOR LOOP FOR only?
Not with dynamic sql statements. I suggest you use collections:
SQL> create or replace
2 procedure p1(
3 p_empno sys.OdciNumberList
4 )
5 is
6 begin
7 for v_rec in (select ename from emp where empno in (select * from table(p_empno))) loop
8 dbms_output.put_line(v_rec.ename);
9 end loop;
10 end;
11 /
Procedure created.
SQL> set serveroutput on
SQL> exec p1(sys.OdciNumberList(7566,7844,7900));
JONES
TURNER
JAMES
PL/SQL procedure successfully completed.
SQL>
SY.
Tags: Database
Similar Questions
-
Dynamic recovery for the dynamic sql query
Hi all
I want to create a query dynamically and to fetch it in a dynamic cursor record... The structure of the record should be the same as that of the query...
Can you suggest any method to do this.
for example
OPEN < dynamic news > TO < dynamic stmt >;
LOOP
FETCH < dynamic news > < DYNAMIC rec >;
---
END LOOP;
CLOSE < dynamic news >;
How to declare the rec here dynamic? I wouldn't have the columns in the query in advance either.
Thank you
Merz
Published by: merz Sep 12, 2011 17:02
Published by: merz Sep 12, 2011 17:03Merz says:
How to declare the rec here dynamic? I wouldn't have the columns in the query in advance either.
Is not possible. What you describe is called four dynamic sql type. For this type of synamic SQL, you use the DBMS_SQL package.
SY.
-
A difficult dynamic SQL query problem
Hi all
I have a very interesting problem to work:
We have this special table defined as follows:
CREATE TABLE sales_data)
sales_id NUMBER,
NUMBER of sales_m01
NUMBER of sales_m02
NUMBER of sales_m03
NUMBER of sales_m04
NUMBER of sales_m05
NUMBER of sales_m06
NUMBER of sales_m07
NUMBER of sales_m08
NUMBER of sales_m09
NUMBER of sales_m10
NUMBER of sales_m11
NUMBER of sales_m12
sales_prior_yr NUMBER);
/
Columns ' sales_m01... sales_m12' represents aggregated monthly sales, what "sales_m01" is translated by "sales for the month of January, January is the first month,"sales_m02"in sales for the month of February and so on.»
The problem I encounter is that we have a project that requires that a parameter is passed to a stored procedure that represents the number of months which is then used to create a SQL query with aggregations of next mandatory field, which depends on the parameter passed:
Example 1: entry of parameter: 4
Should be built dynamically to SQL query:
SELECT
Sum (sales_m04) as CURRENT_SALES,
Sum (sales_m01 + sales_m02 + sales_m03 + sales_m04) SALES_YTD
Of
sales_data
WHERE
sales_id = '0599768';
Example 2: input parameter: 8
Should be built dynamically to SQL query:
SELECT
Sum (sales_m08) as CURRENT_SALES,
SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04 +)
sales_m05 + sales_m06 + sales_m07 + sales_m08) SALES_YTD
Of
sales_data
WHERE
sales_id = '0599768';
So in a sense, the contents of SUM(sales_m01...n) would vary according to the parameter, which must be a number between 1... 12 what is a month, which in turn corresponds to a range of real field on the table itself. The resulting dynamic query should include only those columns/fields in the table that is within the range given by the input parameter and does not account for all the other columns/fields.
Any solution is greatly appreciated.
Thank you.SQL> declare cols long; param integer := 6; sales_id integer := 0599768; begin for i in 1..param loop cols := cols || 'sales_m' || to_char(i, 'fm00') || '+'; end loop; dbms_output.put_line('select sum(sales_m'||to_char(param,'fm00')||') current_sales, sum(' || rtrim(cols,'+') || ') sales_ytd from sales_data WHERE sales_id = :1'); end; / select sum(sales_m06) current_sales, sum(sales_m01+sales_m02+sales_m03+sales_m04+sales_m05+sales_m06) sales_ytd from sales_data WHERE sales_id = :1 PL/SQL procedure successfully completed.
Now it should be obvious:
Instead of dbms_output OPEN a refcursor:SQL> declare cols long; param integer := 6; sales_id integer := 0599768; cur sys_refcursor; begin for i in 1..param loop cols := cols || 'sales_m' || to_char(i, 'fm00') || ','; end loop; open cur for 'select sum(sales_m'||to_char(param,'fm00')||') current_sales, sum(' || rtrim(cols,',') || ') sales_ytd from sales_data WHERE sales_id = :1' using sales_id; ..... end; /
Published by: michaels2 on July 26, 2009 09:49
replaced ',' with ' + '.
-
Dynamic SQL query in the DB adapter
Hello
I want to use the following custom SQL query in the DB adapter:
SELECT EMP_ID, EMP_NAME, EMP_LOCATION FROM EMP WHERE EMP_ID IN (#iNPUT_PARAMETER)
the problem I'm facing is that I don't know at the time of the development that EMP_ID how much will be passed to the process so that I can not use the input parameter specific number. I cannot use it IN the query within which statement I concatenate all the EMP_ID separated by comma and passed under INPUT_PARAMETER, but when running, he read the full concatenated string as a single string rather than identify them as a list of different: with comma separation. could someone help me in this case
Thank you
SunilUse the following query with two parameters. Build the string with delimiter when running and pass this string and delimiter to query... (in this example, the input value would be the same for both Delimiter1 and Delimiter2.)
Select * from EMP WHERE deptno IN (SELECT SUBSTR (STRING_TO_TOKENIZE, DECODE (LEVEL, 1, 1, INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL-1) + 1), INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL)-DECODE (LEVEL, 1, 1, INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL-1) + 1)) FROM (SELECT #StringToTokenizer | #Delimiter1 AS STRING_TO_TOKENIZE, #Delimiter2 AS DELIMITER FROM DUAL) CONNECT BY INSTR (STRING_TO_TOKENIZE SEPARATOR) ((, 1, LEVEL) > 0)
Thank you
-Sreeny -
Difference between the static SQL query and dynamic SQL query.
Hello
Please explain the fundamental difference between static and dynamic sql queries. Please explain for example.Static: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10472/static.htm
Dynamics: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10472/dynamic.htm -
Using CASE in a dynamic sql query
Hello
I want to create a dynamic query using the CASE statement to replace the below 2 SQL statements:
+++++
Select "create or replace the synonym ' |" SYNONYM_NAME | 'for'. TABLE_OWNER |'. ' || TABLE_NAME | » @'|| SUBSTR (Db_link, 1, 30) |';' OF USER_SYNONYMS where db_link is not null;
Select "create or replace the synonym ' |" SYNONYM_NAME | 'for'. TABLE_OWNER |'. ' || TABLE_NAME |';' OF USER_SYNONYMS where the db_link is null;
+++++
While I have a unique select query which will give her create synonym statements for all with or without links db
Using the example of CASE statement as below:
SELECT
FirstName, LastName,
Salary, date of birth,
ABOUT sex
WHEN'm ' THEN 'male '.
WHEN 'F' THEN 'woman '.
END
Employees
I don't get how to write this CASE statement. Can someone guide me here?
Thank you
Malika"" The thread has been moved to Forum Home "database" SQL and PL/SQL+.
Nicolas.
-
Dynamic SQL query to get the unique value of list in a column
I have two tables: tblWorkers and tblSkills. tblWorkers has a column, skills, which is populated by a field of multiple-checkbox with one or more skill_IDs of tblSkills, so each tblWorkers.Skills consists of a list of one or more comma-delimited values. For any Skill_ID, I need to generate a list of all workers with the appropriate skills, so I tried to do something in the direction of SELECT WorkerName FROM tblWorkers WHERE IN of skills (skills, #FORM. Skill_ID #)... or WHERE skills (ListFind (skills, #FORM. Skill_ID #))... etc.? My results (once I have had data type mismatches of the road) return all workers, not just those with the desired skills. There must be an easy way to do... How people with a little more experience CF/SQL I do this?> each tblWorkers.Skills consists of a list of one or more comma-delimited values
This is your problem. You store a list of values that you want to access power/query separately in a single column. You will have to burst in a separate table.
TblWorkersTblSkills
TblWorkerSkills
mapping table of many workers with skills.If you do not change your data model now, you will constantly be butting your head against that.
-
Dynamic SQL query returning (problem with list of value)
Hi, I have problems with my request. I want to do where statement based on my selectlist, but the problem is that I could not write the correct string in my where condition.
: P61_STATUS has this following display, return value
Bewerber Bewerber
PRA_Kandidat PRA_Kandidat
abgelehnt abgelehnt
angenommen angenommen
Thema-Thema
join online
Staaten Staaten
Sky sky
our our
resultDECLARE q varchar2(4000); list_betreuer htmldb_application_global.vc_arr2; list_semester htmldb_application_global.vc_arr2; list_status htmldb_application_global.vc_arr2; BEGIN -- variable to store the list list_betreuer := HTMLDB_UTIL.STRING_TO_TABLE(:P61_BETREUER); list_semester := HTMLDB_UTIL.STRING_TO_TABLE(:P61_SEMESTER); list_status := HTMLDB_UTIL.STRING_TO_TABLE(:P61_STATUS); -- Query begins q:= 'select p1.name, p1.vorname , a1.tel, a2.tel, '; q:= q||'ab.thema, ab.status, ab.typ, s.bezeichnung, p2.name '; q:= q||'from person p1, person p2, adresse a1, adresse a2, '; q:= q||'zuordnungp_a zpa1,zuordnungp_a zpa2, '; q:= q||'abschlussarbeit ab, semester s '; q:= q||'WHERE ab.SEMESTER = s.OBJECTID (+) '; q:= q||'AND ab.STUDENT = p1.OBJECTID (+) '; q:= q||'AND ab.BETREUER = p2.OBJECTID (+) '; q:= q||'and p1.objectid = zpa1.person (+) '; q:= q||'and zpa1.adresse = a1.objectid (+) '; q:= q||'and zpa1.art (+)= ''Privat'' '; q:= q||'and p1.objectid = zpa2.person (+) '; q:= q||'and zpa2.adresse = a2.objectid (+) '; q:= q||'and zpa2.art (+)= ''Geschäft'' '; -- Loop for betreuer list FOR i in 1..list_betreuer.count LOOP IF i = 1 THEN q:= q||'AND (ab.betreuer = '||list_betreuer(i); ELSE q:= q||' OR ab.betreuer = '||list_betreuer(i); END IF; END LOOP; if (list_betreuer.count>0)THEN q:= q||')'; END IF; -- Loop for semester list FOR i in 1..list_semester.count LOOP IF i = 1 THEN q:= q||'AND (ab.semester = '||list_semester(i); ELSE q:= q||'OR ab.semester = '||list_semester(i); END IF; END LOOP; if (list_semester.count>0)THEN q:= q||')'; END IF; -- Loop for status list FOR i in 1..list_status.count LOOP IF i = 1 THEN q:= q||'AND (ab.status = '||list_status(i)||''; ELSE q:= q||'OR ab.status = '||list_status(i)||''; END IF; END LOOP; if (list_status.count>0)THEN q:= q||')'; END IF; htp.p(q); return q; END;
the problem is in this summaryselect p1.name, p1.vorname , a1.tel, a2.tel, ab.thema, ab.status, ab.typ, s.bezeichnung, p2.name from person p1, person p2, adresse a1, adresse a2, zuordnungp_a zpa1,zuordnungp_a zpa2, abschlussarbeit ab, semester s WHERE ab.SEMESTER = s.OBJECTID (+) AND ab.STUDENT = p1.OBJECTID (+) AND ab.BETREUER = p2.OBJECTID (+) and p1.objectid = zpa1.person (+) and zpa1.adresse = a1.objectid (+) and zpa1.art (+)= 'Privat' and p1.objectid = zpa2.person (+) and zpa2.adresse = a2.objectid (+) and zpa2.art (+)= 'Geschäft' AND (ab.status = abgegeben)
This statement produce this following statementq:= q||'AND (ab.status = '||list_status(i)||'';
But what I need, is this statementab.status = abgegeben
How can I get this statement?ab.status = 'abgegeben';
Thank you very muchTo use double quotes:
q:= q||'AND (ab.status = '''||list_status(i)||'''';
-
Hello
I want to run and store the results of a dynamic sql query in a strored procedure.
I get the following variables of the user running: v_column_names, v_table_name, v_col, v_value
The query will be like:
v_query: = 'SELECT'. v_column_names | ' A ' | v_table_name | ' WHERE ' | v_col |' = ' | v_value;
for example v_column_names: = 'ACCOUNT_NUM, SYSTEM_ID, ENTITY_ID ';
v_table_name: = 'ACCOUNT '.
v_col: = 'COUNTRY_CODE ';
v_value: = "USA";
Here is what I tried
DECLARE
v_column_names VARCHAR (200): = 'ENTITY_ID, SYSTEM_ID, ACCOUNT_NUM;
v_table_name VARCHAR (200): = 'ACCOUNT '.
v_col VARCHAR (200): = 'COUNTRY_CODE ';
v_value VARCHAR (200): = "'USA"';
TYPE column_record () IS RENDERING
VARCHAR2 (200) C1: = null;
C2 VARCHAR2 (200): = null;
C3 VARCHAR2 (200): = null
);
TYPE st_table IS TABLE OF column_record INDEX DIRECTORY.
pk_table st_table;
NUMBER of v1: = 1;
BEGIN
v_select: = 'SELECT'. v_column_names | ' A ' | v_table_name | ' WHERE ' | v_col |' = ' | v_value;
EXECUTE IMMEDIATE v_select COLLECT LOOSE pk_table.
I'm IN 1.pk_table.count LOOP
DBMS_OUTPUT. Put_line (pk_table (v1). (C1);
DBMS_OUTPUT. Put_line (pk_table (v1). (C2);
DBMS_OUTPUT. Put_line (pk_table (v1). (C3);
v1: = v1 + 1;
END LOOP;
END;
The number of column names in v_column_names is dynamic, so I need a way to store the results and then manipulate them.
I look forward to your reply.
EDIT: The real question:
1. in the settings I have pass the table name and the value of the primary key of the row I want to delete.
2. He then checks if this line is referenced anywhere, removes the reference, and then deletes the current line. This can go, until all the rows that are referenced are removed.
The above code, I have provided a small part of the largest of stored procedure.
Thank you all, I found the tips very helpful. I found a solution to my problem:
It generates all removal requests to remove a specific line. Not what I wanted, but this does not solve my problem.
The reason why is that I discovered that we do not use the ON DELETE CASCADE that we don't remove a record accidentally, I know we have an option to restore, but it is just in case something goes wrong.
-
Rewrite the static query in dynamic SQL
Can someone please help me write a dynamic query in pl/sql to SQL next?
It's the game ('25', ' 04 "). the number of list might be n.
The column_type varies from(1..6) and
the from(1..6) of column_amt1 lines
Thanks in advanceSELECT t.claim_no, t.cert_no, t.rec_code, (NVL(decode(t.column_type1,'25',NVL (t.column_amt1, 0)),0)+ NVL(decode(t.column_type1,'04',NVL(t.column_amt1, 0)),0 ) ) + (NVL(decode(t.column_type2,'25',NVL (t.column_amt2, 0)),0)+ NVL(decode(t.column_type2,'04',NVL(t.column_amt2, 0)),0 ) ) + (NVL(decode(t.column_type3,'25',NVL (t.column_amt3, 0)),0)+ NVL(decode(t.column_type3,'04',NVL(t.column_amt3, 0)),0 ) ) + (NVL(decode(t.column_type4,'25',NVL (t.column_amt4, 0)),0)+ NVL(decode(t.column_type4,'04',NVL(t.column_amt4, 0)),0 ) ) + (NVL(decode(t.column_type5,'25',NVL (t.column_amt5, 0)),0)+ NVL(decode(t.column_type5,'04',NVL(t.column_amt5, 0)),0 ) ) + (NVL(decode(t.column_type6,'25',NVL (t.column_amt6, 0)),0)+ NVL(decode(t.column_type6,'04',NVL(t.column_amt6, 0)),0 )) amt from test_detail t;
HenaBut in fact, you should not dynamic SQL at all. What you could do is create a SQL type and use MEMBER OF:
SQL> CREATE OR REPLACE 2 TYPE test_detail_type_tbl 3 AS 4 TABLE OF varchar2(2) 5 / Type created. SQL> select case_id, 2 item, 3 code, 4 case 5 when type1 member of test_detail_type_tbl('25','04') then nvl(amt1,0) 6 else 0 7 end + 8 case 9 when type2 member of test_detail_type_tbl('25','04') then nvl(amt2,0) 10 else 0 11 end + 12 case 13 when type3 member of test_detail_type_tbl('25','04') then nvl(amt3,0) 14 else 0 15 end + 16 case 17 when type4 member of test_detail_type_tbl('25','04') then nvl(amt4,0) 18 else 0 19 end + 20 case 21 when type5 member of test_detail_type_tbl('25','04') then nvl(amt5,0) 22 else 0 23 end + 24 case 25 when type6 member of test_detail_type_tbl('25','04') then nvl(amt6,0) 26 else 0 27 end copay 28 from test_detail 29 / CASE_ID ITEM CO COPAY ---------- --------- -- ---------- EML3371015 133761570 10 355 EML3371015 133761570 10 20 EML3371015 133761570 10 5 EMC6369600 140328551 10 54 EMH6353995 140328551 11 26.04 SQL> select case_id, 2 item, 3 code, 4 case 5 when type1 member of test_detail_type_tbl('25') then nvl(amt1,0) 6 else 0 7 end + 8 case 9 when type2 member of test_detail_type_tbl('25') then nvl(amt2,0) 10 else 0 11 end + 12 case 13 when type3 member of test_detail_type_tbl('25') then nvl(amt3,0) 14 else 0 15 end + 16 case 17 when type4 member of test_detail_type_tbl('25') then nvl(amt4,0) 18 else 0 19 end + 20 case 21 when type5 member of test_detail_type_tbl('25') then nvl(amt5,0) 22 else 0 23 end + 24 case 25 when type6 member of test_detail_type_tbl('25') then nvl(amt6,0) 26 else 0 27 end copay 28 from test_detail 29 / CASE_ID ITEM CO COPAY ---------- --------- -- ---------- EML3371015 133761570 10 300 EML3371015 133761570 10 20 EML3371015 133761570 10 5 EMC6369600 140328551 10 0 EMH6353995 140328551 11 5.52 SQL>
SY.
-
Difference between static &; dynamic sql
Hello
It is my first question in OTN.
I have a pl/sql procedure with no insert statements.
Insert into table values...
If I turn all these insert statements in dynamic sql, my procedure will give more effective? (execution)
Please help me.
Thanks in adv
Friend
Hello
8b00c42d-7716-467e-BCEF-c756fd9599e2 wrote:
Hello
It is my first question in OTN.
Welcome to the Forum!
To get the most out of this Forum, see the FAQ in the Forum: Re: 2. How can I ask a question on the forums?
For all performance issues, see also this Forum FAQ page: Re: 3. how to improve the performance of my query? / my query is slow.
I have a pl/sql procedure with no insert statements.
Insert into table values...
If I turn all these insert statements in dynamic sql, my procedure will give more effective? (execution)
Probably not. Dynamic SQL is generally less effective than static SQL, as well as being more difficult to code, debug and maintain.
Is there a reason why you think that the dynamic SQL would be preferable in this case? If so, what is it?
-
DA on updatable report items Sql query using the class
Request Express 4.2.5.00.08
10-11 g Oracle
I have a page, so it has a link/button when he clicks then modal region will appear. Modal region has a as a table (Type SQL Query (updateable report)). All work very well so far.
However, I've now added some complex things.
for example. My region is based on the query of SQL Type (editable report).
SELECT id, name of family, Traghetti, area, d_date, start_time, End_time, displacement, role of table where id =: P10_ID;
I can also add a new line of course.
My problem is, (point) Shift display based on the value of Start_Time point) and so I made sure the class for two items.
for example, Start_Time (' class = "st_tm" ') and Maj ('class to = "Shift_time" '). Start_Time is based on (LOV) and shift is off the point in the sql query, but change the Start_Time.
And now, I created the DA.
1. event: change
Selection type: jQuery Selector
jQuery Selector: .st_tm
Condition: No.
1. action: set value
Set type: Expression Javascript
The JavaScript Expression: $(this.triggeringElement) .val ();
The element affected: P10_X1 (it is a hidden item)
2. action: Plsql Code
Code: start to null; end;
Page items to submit: P10_X1
3. action: Plsql Code
Code: start
If: P10_X1 between '03' AND '11' then
: P10_X1: = 'EARLIES ';
elsif: P10_X1 then '12' and '18'
: P10_X1: = 'LATES ';
on the other
: P10_X1: = 'NIGHTS';
end if;
end;
Page items to submit: P10_X1
NOW another DA
Event: change
Article (s) P10_X1
Condition (in list)
value (HASTY, LATES, NIGHTS)
Action:
Set type: Expression Javascript
The JavaScript Expression: $(this.triggeringElement) .val ();
Affected item:
Selection type: jQuery Selector
jQuery Selector:. Shift_time
Well, I look forward all the above work well, but I'm having a problem.
If I have more than 1 lines on a modal region and if I change the Start_Time value for a row then MAJ (point) is changing but he effect on all lines.
I want, if I make the changes on the line line # 2 while only 2 # MAJ (point) must be not performed any other lines?
Help, please!
Kind regards
RI
I found the solution me thank you.
I removed all the DA mentioned above and created a new.
1. event: change
Selection type: jQuery Selector
jQuery Selector: .st_tm
Condition: No.
Scope of the event: dynamic and light on page load.
Action (Javascript code)
ROW_ID = $(this.triggeringElement).attr('id').substr (4);
If ($(this.triggeringElement). val() > = 03 &. val() $(this.triggeringElement))<= 11)="">=>
.Val ('EARLIES') $(«#f11_» + row_id);
}
ElseIf ($(this.triggeringElement). > 11 val() & $(this.triggeringElement). val())<= 18)="">=>
.Val ('LATES') $(«#f11_» + row_id);
}
ElseIf (. val() > $19 (this.triggeringElement)) {}
.Val ('NIGHTS') $(«#f11_» + row_id);
}
also created another DA because I have a disabled report items.
Event: before the page is sent.
$('_:_disabled').removeAttr ("disabled");
and finally created manual process of DML for tabular and everything works fine. (Insert, Update, and delete).
Kind regards.
-
Hi all
I'm trying to find a solution to this problem
SELECT b.person,
tbl1.program_code,
TO_CHAR (to_date (tbl1.start_date, 'YYYYMMDD'), ' MM/DD/YYYY'),
TO_CHAR (to_date (tbl1.end_date, 'YYYYMMDD'), ' MM/DD/YYYY'),
TBL2.program_code,
TO_CHAR (to_date (TBL2.start_date, 'YYYYMMDD'), ' MM/DD/YYYY')
Person_table p,
(SELECT e.person,
e.program_code,
e.start_date,
e.end_date,
OF e program_table,.
a_program_table has,
WHERE the e.end_date BETWEEN: first_day_year AND: last_day_year
AND a.system_date > to_date (: last_run_date, 'YYYYMMDD')) tbl1.
(SELECT e.person,
e.program_code,
e.start_date,
e.end_date,
OF e program_table,.
a_program_table has,
WHERE the e.start_date BETWEEN: first_day_year AND: last_day_year
AND a.system_date > to_date (: last_run_date, 'YYYYMMDD')) tbl2
WHERE = tbl1.person p.person
AND tbl1.person = tbl2.person
AND to_date (tbl1.end_date, 'YYYYMMDD') < to_date (tbl2.start_date, 'YYYYMMDD')
ORDER BY tbl2.start_date asc, asc tbl1.end_date;
The tables look like this
Program_table
PERSON PROGRAM_CODE START_DATE END_DATE
------ ------------ ---------- ---------
20150115 20150101 17 A1
20150125 20150116 13 A1
20150210 20150126 3 A1
20150220 20150211 16 A1
22991231 20150221 13 A1
A_program_table
PERSON PROGRAM_CODE START_DATE END_DATE SYSTEM_DATE
------ ------------ ---------- --------- -----------
17 20150101 20150115 20150101 A1
13 20150116 20150125 20150116 A1
3 20150126 20150210 20150126 A1
16 20150211 20150220 20150211 A1
13 20150221 22001231 20150221 A1
OUTPUT_RESULT
PERSON PROGRAM_CODE START_DATE END_DATE PROGRAM_CODE START_DATE
------ ------------ ---------- -------- ------------ ----------
A1 3 26/01/2015 10/02/2015 13 21/02/2015 -the unwanted span
A1 3 26/01/2015 10/02/2015 16 02/11/2015
16/02/11/2015-02/20/2015 A1 13 21/02/2015
As you can see here, I'm recording a person of conversion from one program to the other, given that the query has been run since last month. The results I'm capture is for the month of February.
I wanted the transition of the program 3-16 and 16-13, but not from 3 to 13. I need to eliminate the transition from 3 to 13. The reason why is this makes its appearance is due to the line
AND to_date (tbl1.end_date, 'YYYYMMDD') < to_date (tbl2.start_date, 'YYYYMMDD').
I use this because I want the result spans compared dynamically.
I'm looking for change of this request (for example: we can have 10 entries on the table for a month, but have only 9 transitions listed in the final - either).
Note: I would like to have a single query while as noted above, no procedure or query.
Thanks in advance
Hello
Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.
Also post the exact results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: Re: 2. How can I ask a question on the forums?
You said that this problem was "creation of the pl/sql query", but you have probably not all PL/SQL; just SQL.
2886750 wrote:
...
AND to_date (tbl1.end_date, 'YYYYMMDD')< to_date="" (tbl2.start_date,="">
Do not store information on dates in the string columns. Use the DATE columns. Then you won't need TO_DATE in conditions like this.
You do a join to match each row in each previous row for the same person. If I understand the problem, you don't want to compare with each previous line, you only want to compare with the last front row. That sound like a job for the analytic LAG function.
-
Hey guys,.
I'm using version 4.2.6 apex. The theme is 26.
IM also using Listener Oracle APEX.
I can print my classic report in PDF or CSV format.
But I need to export it as a .xls extension.
Report type: SQL (body of function from PL/SQL returning the SQL) query
I need is like this cause my 'where' clause type is dynamic.
Look at using this method: Tom's Blog: a non-standard export excel 2010 (.xlsx)
I do something similar for downloads to excel in my projects...
Thank you
Tony Miller
Software LuvMuffin
Ruckersville, WILL -
How to extract the names of columns in dynamic SQL
Hi all
Is it possible to extract all the names of columns in a dynamic query?
In my case according to the user selections that my query will get changed (number of the column, column name and tables that everything can vary).
So now, is it possible to retrieve all the column names of the dynamic query generated?
I am using Oracle 11g (11.2.0.4)
Thank you
Shaz
Maybe you are looking for
-
Verification of failure AppleID
Whenever I try to connect to my AppleID by typing my password on my iPhone 6, I get a pop-up response that says "failed check your Apple ID or password is incorrect." However, I can use the same password to connect successfully to my Apple ID on my c
-
Someone answered my question, and wanted a page-ecran-- how to make he him?
his name is wayne
-
Pavilion dv6 wireless network card drivers?
I just finished reinstalling a new windows 7 64 bit on my laptop of uncles engaged. I reinstall the drivers and the only one missing is wireless network card driver. I run the scan on the HP site and it came not with all the drivers for the wireless
-
Satellite P200 - failed to connect to the Windows Services
After starting Windows Vista on my Satellite P200, I received a message: failed to connect to any Windows Services.The event notification system file are not available.Use restricted to the mode of administration. No network supports either (wifi, Et
-
Programmatically formatting graphic mixed signals
I am writing an FPGA application where I am acquiring data from a unit under test (USE). The PXI-7842R digitizes 3 groups of signals: 1. an analog voltage monitor 2. 5 digital signals connected to the analog inputs (limitation of the pinout of the co