Using a WHERE clause in APEX Tree

Hi All -
I have an hierarchical SQL query that I'm displaying as an APEX tree.
My sample app is here:
https://apex.oracle.com/pls/apex/f?p=32581:29
login: guest
pw: app_1000
workspace: leppard
I am trying to add a WHERE clause so that only nodes with the lowest level children are displayed, i.e. something like 'WHERE connect_by_isleaf = 0 OR level = 5'
The tree query with the where clause works fine in the SQL commands window, but when I add the WHERE clause to my apex tree the page no longer displays anything. Is this an issue with APEX or is there some other way to filter my results?
Thanks in advance for any suggestions,
john

Connect by occurs first, the where clause then is applied to those results, effectively cutting away in the hierarchical structure. Since apex has to build a hierarchical structure from the query it relies on the level pseudocolumn, which is butchered by applying the where clause. It's a miracle you aren't even receiving errors because I'd almost expect an incorrect json array to have been built. With no top level to start from and only level 5 or leaf nodes there is no structure to present: both apex can not put out a correct representation and not jstree either. In your query you will see your "root nodes" but it is not representable.
I'm not sure why you want to present this in a tree? Both connect_by_is_leaf and level = 5 will simply give you a list of nodes without any hierarchical structure.
The better thing to do is to use a subquery to first limit your dataset and then use that to base the tree query on, that way you don't break any of those vital columns.
Eg if you only want leaf nodes and their immediate parent you could go for something like this (quick mockup on some testdata):
with dataset as (
select node_id, parent_id
   from treedata
  where connect_by_isleaf = 0
connect by prior node_id = parent_id
  start with parent_id = 0
dataset2 as (
select node_id, parent_id
  from dataset
union all
select node_id, null parent_id
  from treedata
where node_id in (select parent_id from dataset)
select level, node_id
   from dataset2
connect by prior node_id = parent_id
  start with parent_id is null

Similar Messages

  • How do you use 3 Where Clauses in a query

    Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
    Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
    Order the result by OrderID in ascending order.
    First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
    Second WHERE clause use equals and sub query.
    Third WHERE clause uses equal and company name.
    This is what i have so far but i am pretty confused on how to do this.
    My Code for NorthWind:
    Select OrderID
    From Orders o
    Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
    (Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
    The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
    Select VenderName, InvoiceNumber, InvoiceTotal
    FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
    WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
    ORDER BY VendorName;

    >Where Orders.OrderDate
    > ALL  (Select
    CompanyName
    The comparison operator (>) requires compatible data types.
    DATETIME is not compatible with VARCHAR string for comparison.
    Here is your homework:
    SELECT orderid
    FROM orders o
    WHERE o.orderdate > ALL (SELECT orderdate
    FROM orders
    WHERE shipvia = (SELECT Max(shipvia)
    FROM orders o
    INNER JOIN customers c
    ON c.customerid =
    o.customerid
    WHERE
    c.companyname = 'Bottom-Dollar Markets'));
    11064
    11065
    11066
    11067
    11068
    11069
    11070
    11071
    11072
    11073
    11074
    11075
    11076
    11077
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • What happens u0093Updateu0094 command is used without where clause ?

    Hi
    What happens “Update” command is used without where clause ? 
    thank you

    Hi subash,
    chk this help
    UPDATE  dbtab      SET f1 ... fn. or
    UPDATE (dbtabname) SET f1 ... fn.
    Extras:
    1. ... WHERE condition
    2. ... CLIENT SPECIFIED
    3. ... CONNECTION con
    Effect
    Updates values in a database table. <b>If there is no WHERE clause, all lines (in the current client) are updated.</b> If a WHERE condition is specified, only thoserecords which satisfy the WHERE condition are updated.

  • Trying to use a where clause from a table

    hi,
    I have a report and I need to use a where clause that is archived in a table, it is a different one depending on the row (colour=blue or number = 88..) I have tryied with ref cursor, dinamis sql, functions, packages and could not get it, any suggestion??
    thanks

    You may use Lexical Rerefences: "Lexical references are placeholders for text that you embed in a SELECT statement. You can use lexical references to replace the clauses appearing after SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, CONNECT BY, and START WITH....."

  • Tree issue using a where clause

    Tree created
    select "ID" id,
    "PARENT_ID" pid,
    "TITLE" name,
    "LINK" link,
    null a1,
    null a2
    from apexim.TREE_NAV
    I am attempting to alter this tree this way
    select "ID" id,
    "PARENT_ID" pid,
    "TITLE" name,
    "LINK" link,
    null a1,
    null a2
    from apexim.TREE_NAV
    WHERE INSTR(PAGE_ID_1,:app_page_id) > 0
    This returns rows in PL/SQL however does not return tree values to the page.
    this throw the error
    Warning: Tree root ID "1" not found.
    When I change the where clause to:
    where 1=1
    I get all the enitire tree as i would expect.
    We are creating a report delevery application that will allow people to see specific leaves passed on authority level.
    The Tree_nav table ha the url for the page to display and has the page_id_1 for the portal that launches specific content page.
    So ultimatly I will include the following into the where clause
    (and authority_level <= :p100_auth_level)
    I have tried several things and am at wits end.

    rbackmann wrote:
    And the page_id_1 is a field in the table (SORRY EXAMPLE BAD NAMING CONVENTIONS) that IS VARCHAR
    and houses a comma seperated list of page numbers ie 28,29, 30Okay, I was wrong about the field name :(
    Carefully examine the query, expecially the INSTR line. Get the values in both the column and the bind variable and make sure they are what you think they are.
    The example on the other post might have failed because you were equality comparing the column value to '28' when it might have been a csv with commas unless you changed it (something worth trying).
    On 4th look I may (or may not) have something. PAGE_ID_1 is a VARCHAR2, while the 28 you compared it to was a number. Its a long shot, but maybe SQL is trying to convert the csv field to a number, failing to raise an error message where you can find it, and failing for that reason. Try using '28' on the equality test.
    Edited by: riedelme on May 8, 2009 6:03 AM

  • Conditional WHERE clause for APEX

    Dear SQL gurus,
    I know that there's a APEX forum, but I think that there maybe an sql based solution for this...
    What I have is a query like this:
    SELECT COMMENT_ORIG, COMMENT_CHARGING
    FROM  CL_INVENTORY_OPEN_OUT
    WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FK where :P13_INCIDENTAL_FK is a field in the Web Based GUI...
    What I need is to change the WHERE clause to something like this
    WHERE
      DECODE(:P13_INCIDENTAL_FK, NULL, 1=1, INCIDENTAL_FK=:P13_INCIDENTAL_FK)so to say:
    if the : P13_INCIDENTAL_FK is null, then let the clause be 1=1, otherwise let it be NCIDENTAL_FK=:P13_INCIDENTAL_FK.
    Is there a way to do this inside a sql statement?
    Best Regards and THX
    Johann

    SELECT COMMENT_ORIG, COMMENT_CHARGING
    FROM  CL_INVENTORY_OPEN_OUT
    WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FKDo not know how you are using the query, but you might try something like this
    BEGIN
      CASE :P13_INCIDENTAL_FK
        WHEN NULL THEN
          SELECT COMMENT_ORIG, COMMENT_CHARGING
            FROM  CL_INVENTORY_OPEN_OUT
        ELSE
          SELECT COMMENT_ORIG, COMMENT_CHARGING
            FROM  CL_INVENTORY_OPEN_OUT
           WHERE INCIDENTAL_FK=:P13_INCIDENTAL_FK
      END CASE;
    END;Edited by: ajallen on May 7, 2010 6:44 AM

  • Function use in where clause

    hi,
    can we use output of a function in where clause directly ?
    i.e
    select node(condition1) clause from dual;
    clause
    occupation='SALARY'
    then i want to use this output i.e clause string directly in select stmt.
    select * from abc_table where clause
    more strictly
    select * from abc_table where (select node(condition1) clause from dual)
    which is interpreted as
    select * from abc_table where occupation='SALARY'
    Is there any way to use functions in clause area ???
    I tried CURSOR but not workd for this
    Thanks in advance,
    Rup

    I cant really understand your problem
    --fn1 is a function
    sql>
    select fn1
    from dual;
    FN1 
    CLERK 
    sql>
    select * from emp
    where job = fn1;
    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 
    7369  SMITH  CLERK  7902  17-DEC-80  800     20 
    7876  ADAMS  CLERK  7788  23-MAY-87  1100     20 
    7900  JAMES  CLERK  7698  03-DEC-81  950     30 
    7934  MILLER  CLERK  7782  23-JAN-82  1300     10
    Message was edited by:
            jeneesh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Case stament use in where clause  grive error

    i want to use this conditions but gives error. Please give me the solution of it
    WHERE
    CASE
    WHEN CANCELLED_DATE IS NULL THEN
    TO_DATE(INV_GL_DATE,'DD-MON-YY') <= TO_DATE (:P_DATE1,'DD-MON-YY')
    ELSE
    TO_DATE(INV_GL_DATE,'DD-MON-YY') >= TO_DATE(:P_DATE1,'DD-MON-YY')
    END) --- <= TO_DATE(:P_DATE1,'DD-MON-YY')

    Hi,
    Remember that a CASE expression always evaluates to a single value in one of the SQL datatypes, such as VARCHAR2, NUMBER or DATE. (There is no Boolean datatype in SQL.)
    You can't say
    WHERE   CASE ... END;for the same reason that you can't say
    WHERE   'Hello, world!';orWHERE   100;or
    WHERE   SYSDATE;CASE expressions are really valuable because they allow you do perform IF-THEN-ELSE logic in places where you normally can't, like the SELECT clause.
    The WHERE clause is a place where you can perform IF-THEN-ELSE logic anyway, so there's rarely a need for a CASE expression in a WHERE clause. It's just as efficient and just as clear (if not more so) to put all your conditions directly in the WHERE clause, like Max demonstrated.
    Edited by: Frank Kulash on Feb 26, 2010 10:13 AM
    user11995078 wrote:
    Thanks Dear But how can we use with case and decodeWhy do you want to?
    CASE does not help in this problem, any more than regular expressions or CONNECT BY or MODEL help. They are all great tools for particular jobs, but not for this job. Asking "How can I use CASE to do this?" Is like asking "How can I use a hammer to tighten a bolt?" Perhaps there is a way, but it's likely to be contrived and ridiculous.
    Here's the least ridiculous way I can think of:
    WHERE     ( TO_DATE (INV_GL_DATE, 'DD-MON-YY') 
         - TO_DATE (:P_DATE1,     'DD-MON-YY')
         ) * CASE
                 WHEN  cancelled_date  IS NULL  THEN  1
                    `                            ELSE -1
             END <= 0

  • Partition Pruning on Interval Range Partitioned Table not happening when SYSDATE used in Where Clause

    We have tables that are interval range partitioned on a DATE column, with a partition for each day - all very standard and straight out of Oracle doc.
    A 3rd party application queries the tables to find number of rows based on date range that is on the column used for the partition key.
    This application uses date range specified relative to current date - i.e. for last two days would be "..startdate > SYSDATE -2 " - but partition pruning does not take place and the explain plan shows that every partition is included.
    By presenting the query using the date in a variable partition pruning does table place, and query obviously performs much better.
    DB is 11.2.0.3 on RHEL6, and default parameters set - i.e. nothing changed that would influence optimizer behavior to something unusual.
    I can't work out why this would be so. It very easy to reproduce with simple test case below.
    I'd be very interested to hear any thoughts on why it is this way and whether anything can be done to permit the partition pruning to work with a query including SYSDATE as it would be difficult to get the application code changed.
    Furthermore to make a case to change the code I would need an explanation of why querying using SYSDATE is not good practice, and I don't know of any such information.
    1) Create simple partitioned table
    CREATETABLE part_test
       (id                      NUMBER NOT NULL,
        starttime               DATE NOT NULL,
        CONSTRAINT pk_part_test PRIMARY KEY (id))
    PARTITION BY RANGE (starttime) INTERVAL (NUMTODSINTERVAL(1,'day')) (PARTITION p0 VALUES LESS THAN (TO_DATE('01-01-2013','DD-MM-YYYY')));
    2) Populate table 1million rows spread between 10 partitions
    BEGIN
        FOR i IN 1..1000000
        LOOP
            INSERT INTO part_test (id, starttime) VALUES (i, SYSDATE - DBMS_RANDOM.value(low => 1, high => 10));
        END LOOP;
    END;
    EXEC dbms_stats.gather_table_stats('SUPER_CONF','PART_TEST');
    3) Query the Table for data from last 2 days using SYSDATE in clause
    EXPLAIN PLAN FOR
    SELECT  count(*)
    FROM    part_test
    WHERE   starttime >= SYSDATE - 2;
    | Id  | Operation                 | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT          |           |     1 |     8 |  7895  (1)| 00:00:01 |       |       |
    |   1 |  SORT AGGREGATE           |           |     1 |     8 |            |          |       |       |
    |   2 |   PARTITION RANGE ITERATOR|           |   111K|   867K|  7895   (1)| 00:00:01 |   KEY |1048575|
    |*  3 |    TABLE ACCESS FULL      | PART_TEST |   111K|   867K|  7895   (1)| 00:00:01 |   KEY |1048575|
    Predicate Information (identified by operation id):
       3 - filter("STARTTIME">=SYSDATE@!-2)
    4) Now do the same query but with SYSDATE - 2 presented as a literal value.
    This query returns the same answer but very different cost.
    EXPLAIN PLAN FOR
    SELECT count(*)
    FROM part_test
    WHERE starttime >= (to_date('23122013:0950','DDMMYYYY:HH24MI'))-2;
    | Id  | Operation                 | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT          |           |     1 |     8 |   131  (0)| 00:00:01 |       |       |
    |   1 |  SORT AGGREGATE           |           |     1 |     8 |            |          |       |       |
    |   2 |   PARTITION RANGE ITERATOR|           |   111K|   867K|   131   (0)| 00:00:01 |   356 |1048575|
    |*  3 |    TABLE ACCESS FULL      | PART_TEST |   111K|   867K|   131   (0)| 00:00:01 |   356 |1048575|
    Predicate Information (identified by operation id):
       3 - filter("STARTTIME">=TO_DATE(' 2013-12-21 09:50:00', 'syyyy-mm-dd hh24:mi:ss'))
    thanks in anticipation
    Jim

    As Jonathan has already pointed out there are situations where the CBO knows that partition pruning will occur but is unable to identify those partitions at parse time. The CBO will then use a dynamic pruning which means determine the partitions to eliminate dynamically at run time. This is why you see the KEY information instead of a known partition number. This is to occur mainly when you compare a function to your partition key i.e. where partition_key = function. And SYSDATE is a function. For the other bizarre PSTOP number (1048575) see this blog
    http://hourim.wordpress.com/2013/11/08/interval-partitioning-and-pstop-in-execution-plan/
    Best regards
    Mohamed Houri

  • Can pipelined functions' return values be used in WHERE clause?

    If I have function MY_FUNC that returns a REFCURSOR with columns COL1, COL2, COL3
    can I use the values returned in the output cursor in my WHERE clause as well as in the SELECT clause?
    e.g.
    SELECT COL1, COL2, COL3
    FROM TABLE(MY_FUNC(param1, param2))
    WHERE COL1 = 24 AND COL2=25
    Would that be proper SQL?

    Hi,
    SQL> Create OR Replace Package Pkg_Test_ Is
      2 
      3     Type my_typ Is Table Of Number;
      4 
      5     Function fnc_test Return my_typ Pipelined;
      6 
      7  End;
      8  /
    Package created
    SQL> Create OR Replace Package Body Pkg_Test_ Is
      2 
      3     Function fnc_test Return my_typ
      4        Pipelined Is
      5        va_typ my_typ := my_typ();
      6     Begin
      7        For i IN 1 .. 10 Loop
      8           va_typ.Extend;
      9           va_typ(va_typ.Count) := i;
    10           Pipe Row(va_typ(va_typ.Count));
    11        End Loop;
    12        Return;
    13     End;
    14 
    15  End;
    16  /
    Package body created
    SQL> SELECT *
      2    FROM Table(PKG_TEST_.FNC_TEST)
      3   WHERE COLUMN_VALUE > 5
      4  /
    COLUMN_VALUE
               6
               7
               8
               9
              10Regards,
    Christian Balz

  • REP 300 error when using dynamic where clause

    Hi,
    I am developing a report and I need a dynamic where clause.The code I have written in the where clause is as below
    function where_clauseFormula return VARCHAR2 is
    begin
    if :P_PO_REQD = 'No' then
    return ('and pha.segment1 is null');
    elsif :P_PO_REQD = 'Yes' then
         return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
    else return NULL;
    end if;
    end;
    Now if i directly use either of the conditions in my report query it works fine.But if the parameter is No then the where clause does not work.I have also tried different statements in the if clause of the 'No' part and they work.For example the following code works when :P_PO_REQD is 'No' :
    function where_clauseFormula return VARCHAR2 is
    begin
    if :P_PO_REQD = 'No' then
    return ('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
    elsif :P_PO_REQD = 'Yes' then
         return('and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)');
    else return NULL;
    end if;
    end;
    I don't know why this is happening.I really need a solution or a workaround urgently
    Thanks
    Mukund

    You may use Lexical Reference for the dynamic where. In After Parameter Form Trigger, code....
    if :P_PO_REQD = 'No' then
    :P_where := :P_where || 'and pha.segment1 is null';
    elsif :P_PO_REQD = 'Yes' then
    :P_where := :P_where || 'and pha.segment1 between NVL(:p_po_num_from,1) and NVL(:p_po_num_to,99999999999999999)';
    end if;
    ..........

  • How to use a where clause in an inner query

    String approv="pend"
     query = "Select Eno From EmpLeave Where  Status_approval='"+approv+"' and Eno in (Select Eno from EmpLeave  group by Eno having count(Eno)>1)";
    bt in the result even the rows without "pend" is also displayed...
    can sumone plzz tell me where should i put my where clause exactly in the above query

    i do have multiple records with status approval as "pend"....
    Try the below:
    query = "Select Eno From EmpLeave Where  Status_approval='"+approv+"' 
    and Eno in (Select Eno from EmpLeave  group by Eno having count(distinct status_approval)>1)";

  • How to write a java function for use in where clause in SQL statement

    Hi,
    Does anyone know a good tutorial on how to write and include a Java class/function into Oracle.
    I'd like to write mathematical function to use in my queries, but the resources available in PL/SQL are very limited.
    Many thanx

    Pim,
    I see you got an answer in the PL/SQL forum.
    But in case you haven't seen it, perhaps this Web page will help:
    http://www.oracle.com/technology/tech/java/jsp/index.html
    Good Luck,
    Avi.

  • Use of WHERE clause to get 2 columns from dynamically generated html page.

    I am using postgresql.I have to complete a java program , which is a forum like thing.User can login and submit question.....if anybody know the answer he can post the answer........Now the problem is that if more than one questions are entered it all should be displayed when we click on a "faq" button.........i created that one......these questions have the hyperlink.........When we click on a single question a text box will come........we have to write the answer there and answer should lie on the column corresponding the question clicked..............
    Here i need the querry to write in servlet so that the answer is stored in the corresponding column only.................
    Pleaseeeeeeeeeeeee help me as soon as possible..................
    Regards
    Shine..

    Sir,
    Other than you are using postgres and a servlet I have absolutley no idea what you are on about.
    Please try explaining in a less distressing manner.
    Sincerely,
    Slappy

  • Why tree query can't take where clause

    I build a tree struncture without where clause , it is fine, but When I tried to add a where clause, I get error, (Warning: Tree root ID "1" not found. ). Does anybody know why?
    Here is my SQL
    select "ID" id,
    "PID" pid,
    "NAME" name,
    "LINK" link
    from CAPACITY_TREE
    where branch='CAP'
    order by 3

    Hi,
    I'm having a similar problem in APEX 3.01. If you try to create a try using the following query you get the: Warning: Tree root ID "7839" not found error, where 7839 is the "KING" id empno
    WITH x AS
         (SELECT "EMPNO" ID,
                 "MGR" pid,
                 "ENAME" NAME,
                 NULL LINK,
                 NULL a1,
                 NULL a2
          FROM   "EMP"
          where ename = 'JONES')
    SELECT x.id, x.pid, x.name, x.link, x.a1, x.a2
    FROM   xAny ideas as to why we can't put a where clause in a tree?

Maybe you are looking for

  • How can I move my iMovie app to "purchased" in my appstore account???

    how can I move my iMovie app to "purchased" in my appstore account???

  • User Exit for MIGO (goods receipt and goods issue)

    Hi all , When i create a cross company goods receipt or goods issue through MIGO,I need to add another line item to the accounting documents posted with the sales tax calculated.For this i need to use a user exit which adds another line item. Can som

  • DC creation finished with some problems

    Hello, After finish the DC creation below message is getting DC Creation finished with some problems Reason: Some used DC's not avilable Locally, You Have to sync used DC's for the project. and when I open the view implementation many errors in the d

  • Problem with a small code

    Hello , Can anyone plz tell me how do i get this output from this sample code ? im new to java... public class Fact2 int factorial (int n) { int res; System.out.println(n); if (n == 1) return 1; res = factorial (n - 1) * n ; System.out.println("res "

  • Phone numbers not recognised (sms & incoming)

    After a momunmental mobile me sync disaster (it somehow trippled every contact), I had to wipe mobile me (and subsequently all conatcs on my phone) and restore it from a back up. Activating one device at a time and replacing info on device with new d