Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic selection of data in query (oracle 11g)
Dynamic selection of data in query [message #650581] Thu, 28 April 2016 00:33 Go to next message
rocky_007
Messages: 36
Registered: March 2012
Location: India
Member
Dear Gurus,

I want to run one of my query as per the report type condition which is:
1. Display self data
2. Display SUB ORDINATE data (MGR)
3. Display ALL SUBORDINATE (connect by prior)

The structure is below:
CREATE TABLE EMPLOYEES
(
  EMPNO   NUMBER(4),
  ENAME   VARCHAR2(8 BYTE),
  INIT    VARCHAR2(5 BYTE),
  JOB     VARCHAR2(8 BYTE),
  MGR     NUMBER(4),
  BDATE   DATE,
  MSAL    NUMBER(6,2),
  COMM    NUMBER(6,2),
  DEPTNO  NUMBER(2)
)

Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (1, 'Jason', 'N', 'TRAINER', 2, 
    TO_DATE('12/18/1965 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 800, NULL, 10);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (2, 'Jerry', 'J', 'SALESREP', 3, 
    TO_DATE('11/19/1966 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1600, 300, 10);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (3, 'Jord', 'T', 'SALESREP', 5, 
    TO_DATE('10/21/1967 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1700, 500, 20);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (4, 'Mary', 'J', 'MANAGER', 5, 
    TO_DATE('09/22/1968 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1800, NULL, 20);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (5, 'Joe', 'P', 'SALESREP', 10, 
    TO_DATE('08/23/1969 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1900, 1400, 30);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (6, 'Black', 'R', 'MANAGER', 2, 
    TO_DATE('07/24/1970 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2000, NULL, 30);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (7, 'Red', 'A', 'MANAGER', 4, 
    TO_DATE('06/25/1971 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2100, NULL, 40);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (8, 'White', 'S', 'TRAINER', 4, 
    TO_DATE('05/26/1972 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2200, NULL, 40);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (9, 'Yellow', 'C', 'DIRECTOR', 10, 
    TO_DATE('04/27/1973 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2300, NULL, 20);
Insert into EMPLOYEES
   (EMPNO, ENAME, INIT, JOB, MGR, 
    BDATE, MSAL, COMM, DEPTNO)
 Values
   (10, 'Pink', 'J', 'SALESREP', NULL, 
    TO_DATE('03/28/1974 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2400, 0, 30);
COMMIT;


I tried this:

SELECT  ename, empno
  FROM employees
  WHERE (:REPORT_TYPE =1 AND empno=:Var_EMP_NO) OR 
      (:REPORT_TYPE =2 AND mgr=:Var_EMP_NO) OR 
      (:REPORT_TYPE =3 ) START WITH mgr = :Var_EMP_NO
                       CONNECT BY PRIOR empno = mgr



When the report_type is 1 it is not giving the right result. Is that true that we cannot get the result from one query?
Re: Dynamic selection of data in query [message #650583 is a reply to message #650581] Thu, 28 April 2016 00:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Use UNION ALL of 3 queries.

Re: Dynamic selection of data in query [message #650586 is a reply to message #650583] Thu, 28 April 2016 01:04 Go to previous messageGo to next message
rocky_007
Messages: 36
Registered: March 2012
Location: India
Member
Yes Union is working fine, I have tried that but I cannot fit union all in my main query as it will slow down the execution time.
Re: Dynamic selection of data in query [message #650587 is a reply to message #650586] Thu, 28 April 2016 01:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Wrong!

Re: Dynamic selection of data in query [message #650591 is a reply to message #650586] Thu, 28 April 2016 02:50 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
rocky_007 wrote on Thu, 28 April 2016 07:04
Yes Union is working fine, I have tried that but I cannot fit union all in my main query as it will slow down the execution time.


That's only a meaningful statement if there is a faster alternative, which I doubt in this case.
Otherwise that statement ends up meaning - it takes longer to display the data I want than to not display it.
Re: Dynamic selection of data in query [message #650607 is a reply to message #650591] Thu, 28 April 2016 05:41 Go to previous messageGo to next message
rocky_007
Messages: 36
Registered: March 2012
Location: India
Member
Found a solution:


SELECT ename, empno
  FROM employees
 WHERE (:report_type = 1 AND empno = :var_emp_no)
    OR (:report_type = 2 AND mgr = :var_emp_no)
    OR ((:report_type = 3) AND (empno IN (SELECT     empno
                                                FROM employees
                                          START WITH mgr = :var_emp_no
                                          CONNECT BY PRIOR empno = mgr)))

Re: Dynamic selection of data in query [message #650611 is a reply to message #650607] Thu, 28 April 2016 05:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Which is a bad one.

Re: Dynamic selection of data in query [message #650642 is a reply to message #650611] Thu, 28 April 2016 10:24 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
A union all is not slower. When 2 of the unioned selects will never run because of the options passed, it will run just fine.
Previous Topic: Index on composite foreign key
Next Topic: Oracle Lock
Goto Forum:
  


Current Time: Wed Apr 24 00:06:27 CDT 2024