Issue in select query with where clause

Hi guys,
I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
Here is my query which causing the issue,
select * from mytable where myfield = 'myvalue'
But if I use the following query it returns the result correctly.
select * from mytable
Also the myfield value 'myvalue' exists in the table.
I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
Thanks,
Ram.

Hi Ram,
I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
select * from employees
where email = '[email protected]'The best way I found to detect this was to use
select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
Jeff

Similar Messages

  • Hierarchical query with where clause

    Hi,
    How can I query hierarchically a query with WHERE clause? I have a table with three fields session_id,id and root_id.
    When I try with the following query,
    select id, level from relation
    where session_id = 79977
    connect by prior id = root_id start with id = 5042;
    It gets duplicate values.
    I want the query to show in the hierarchical manner with a filter condition using WHERE clause. Please help me how can I achieve this. If you know any link that describes more about this, please send it.
    Thanks in Advance.
    Regards,
    -Parmy

    Hi Sridhar Murthy an others,
    Thanks a lot for your/the answer. It's working for me. It saved a lot of other work around without the proper knowledge of hierarchical query. Please send me any link that describes these issues in detail and also I hope as I have mentioned in the other message, same cannot be achieved on views or ( on two different tables ???)
    Any way thanks for your reply,
    It's working for me.
    With happiness,
    -Parmy

  • Populate list by executing query with where clause

    Hi,
    I m populating my list from database table using following command.
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("learning", new HashMap());
    EntityManager em = emf.createEntityManager();
    String query = "select d from Dept d";
    List list = em.createQuery(query).getResultList();
    My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string.
    String query = "select d from Dept d where d.dept_name='ANYNAME'";
    Moreover I cant select even indivisual columns by using following line
    String query = "select d.dept_code,d.dept_name from Dept d";
    It looks like that my query is getting all rows from table as Object.
    What to do to
    1. put some where clause
    2. get some particular colums instead of all columns
    3. apply some table joins in query
    plz help.

    Hi Alex,
    I misunderstood your question. You had mentioned "My list is getting populated with all of rows in my database table(dept). The problem is, I cant use where clause in my query string. "
    I thought you might not have permissions to modify the code in the class, and that's why I suggested that you could write new classes.
    1) write new classes,? means if I need just two columns from a table >(instead of all columns), do I need to write new class with just two >columns (getters and setters)?You can write a SQL statement anyway you want , there are no restrictions in JDBC , so if you need just 2 columns you can write a custom query for that.
    "select column1 as a, column2 as be from table_name where column3 = xyz" etc.
    2) Is not anyway to write SQL statement like
    select dept_code,dept_name from dept where dept_name='anything'The above SQL is correct, it should work.
    3) Stored procedures? can I get some example(code) to get an idea >how to implement complex queries with where clause and joinings >using stored procedures?Stored procedures are specific to the database you are using, Oracle has a particular syntax for store procdures.
    You can call stored procedures from JDBC also, search on Google for
    "Calling stored procedures from JDBC"
    4) I m using hibernate. can I get any example of using hibernate own >query?Tutorial on Hibernate Query Language:
    http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html

  • Simple Select query with 'where', 'and', 'between' clauses takes time

    Hi,
    I have a select query as below
    SELECT * FROM (SELECT a.*,ROWNUM currentStartRecord From (select ai_inbound.ai_inb_seq tableseq,'AI_INBOUND' tablename,'INBOUND' direction,ai_inbound.appl,ai_inbound.ai_date datetime,ai_inbound.ic_receiver_id pg_id,ai_inbound.ic_sender_id tp_id,ai_inbound.session_no,ai_inbound.ic_ctl_no,ai_inbound.msg_set_id msg_type,ai_inbound.appl_msg_ctl_no reference_no,ai_inbound.fg_version version,ai_inbound.msg_status status,ai_inbound.input_file_name,ai_inbound.output_file_name,ai_inbound.ack_file_name from ai_inbound where ai_inbound.appl = ? and ai_inbound.ai_date between ? and ? )a where ROWNUM <= 49)where currentStartRecord >= 0
    The above query takes longer time through application than expected when the date fields are passed whereas it works fine when no date fields are passed. We are using oracle9.2 version of the database. All the indexed columns and partitioned indexed columns are rebuild.
    Kindly let me know how can i tune up the query to improve the performance.
    Thanks

    Hi,
    I have a select query as below
    SELECT * FROM (SELECT a.*,ROWNUM currentStartRecord From (select ai_inbound.ai_inb_seq tableseq,'AI_INBOUND' tablename,'INBOUND' direction,ai_inbound.appl,ai_inbound.ai_date datetime,ai_inbound.ic_receiver_id pg_id,ai_inbound.ic_sender_id tp_id,ai_inbound.session_no,ai_inbound.ic_ctl_no,ai_inbound.msg_set_id msg_type,ai_inbound.appl_msg_ctl_no reference_no,ai_inbound.fg_version version,ai_inbound.msg_status status,ai_inbound.input_file_name,ai_inbound.output_file_name,ai_inbound.ack_file_name from ai_inbound where ai_inbound.appl = ? and ai_inbound.ai_date between ? and ? )a where ROWNUM <= 49)where currentStartRecord >= 0
    The above query takes longer time through application than expected when the date fields are passed whereas it works fine when no date fields are passed. We are using oracle9.2 version of the database. All the indexed columns and partitioned indexed columns are rebuild.
    Kindly let me know how can i tune up the query to improve the performance.
    Thanks

  • Select query with UNION clause in database adapter

    Friends,
    I have got a SQL query with two UNION clause withing it. like
    select a,b,c
    from a
    union
    select a,b,c
    from b
    The schema generated is like below in sequence
    <element>a</element>
    <element>b</element>
    <element>c</element>
    <element>a</element>
    <element>b</element>
    <element>c</element>
    So, the columns from different select queries joined with UNION clause are all appeared in schema instead of the distinct columns.
    Is there any way around to solve this issue ? or will need to with DB function/procedure.

    I think I know what you are saying but your example doesn't make sense, your SQL should produce something like
    I had to change a, b, c with elementA, elementB, elementC as a and b are reserved html tags.
    <elementA>DateA</elementA>
    <elementB>DataB</elementB>
    <elementC>DataC</elementC>
    ...What is the result of the query when you run it in SQLPlus? Is it what you expect?
    cheers
    James

  • Select sdo_nn with where-clause on large table

    Hi spatial-experts,
    I've following problem. I use a table with more than 2.500.000 dataset. Every dataset represents one point (SDO_GEOMETRY, SDO_POINT_TYPE) and have further (text) information. (PI (point of interest) table from TeleAtlas mn_pi)
    Following Select works fine:
    SELECT /*+ ORDERED NO_INDEX(pi IX_PI_FEATTYP) */
    sdo_nn_distance(1) distance,
    id, feattyp, arnamelc, name, stname, stnamelc, hsnum, postcode
    FROM multinet.mn_pi pi
    WHERE
    pi.feattyp IN ( 9373,9374)
    AND
    SDO_NN(
    pi.geom,
    SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(613.86865234375/60, 3002.94677734375/60, NULL), NULL, NULL),
    'sdo_batch_size=0', 1) = 'TRUE'
    AND rownum = 1
    ORDER BY distance
    (P.S. IX_PI_FEATTYP is an index on column feattyp, here not used. When I'm using it the select do not work (it last very long...))
    When I now modify the where clause to an pi which feat type that does not exists (no entry in table) the select lasts very long (I canceled the select after 5 minutes).
    for example: pi.feattyp = 9756
    Otherwise I add a further where clause like
    AND arnamelc = 'ITA'
    the select also works very long. The point are now in italy. refrence point is in germany and is more than 500km away.
    Are there some solutions to solve this problems
    (should I use for every feat type a own table)?
    Why is the select so slow?
    Thanks for all helpfully answers,
    Matthias

    Hi Matthias,
    When you use nearest neighbor, Oracle will use the spatial index to find the nearest neighbor that meets the query criteria. It probes into the index, calculates distances, and returns data in distance order.
    Using SDO_BATCH_SIZE, it keeps going back to the database, returning more and more records in distance order until the other predicates are satisfied (in your case, pi.feattyp IN ( 9373,9374).
    When you specified pi.feattyp = 9756 (which doesn't exist), you told Oracle to keep going and calculating distances for each of the more than 2.500.000 records in the the data set (you killed it after 5 minutes).
    I don't know why adding: AND arnamelc = 'ITA' caused Oracle to take extra time. Sometimes I will do a: set autotrace trace exp
    then compare plans for something that runs quickly vs. something that runs slowly. If you want to post the plans for the fast and slow versions it might be interesting to look at.
    My guess is you may want to look at a solution that incorporates various possibilities. For instance, if there are only a few of some feattyp values, you may want to select those using the non-spatial index then use sdo_geom.sdo_distance to find the closest point rather than risk having to look through a few million values for a match.
    I'm sure other people have thoughts as well...
    Kind regards,
    Dan

  • SELECT QUERY CHECK & WHERE CLAUSE DIFFERENCE.URGENT PLEASE.

    My question is ,
    IF SBOOK_WA-CARRID = 'LH' CONDITION IS AGAIN PRESENT IN THE DATABASE TABLE ,,,,WILL THE CHECK CONDITION SEARCH IT AGAIN EVEN AFTER FINDING IT IN THE FIRST OF THE TABLE???OR SIMPLY TERMINATE IN THE FIRST TIME THE CONDITION SATISFIES??IF DOES TERMINATE THEN ERRONEOUS RESULT IS BOUND TO COME SINCE ONE MORE RECORS IS UNSELECTED.
    SIMILARLY DOES THE WHERE CLAUSE CONTINUE SEARCHING EVEN AFTER HAVING FOUND THE CONDITION ALREADY ONCE IN THE TABLE??
    PLEASE HELP ME IN THIS ASPECT.PLEASE CHECK THE TWO DIFFERENT CODES & ANSWER ACCORDINGLY..
    SELECT * FROM SBOOK INTO SBOOK_WA.
      CHECK: SBOOK_WA-CARRID = 'LH' AND
             SBOOK_WA-CONNID = '0400'.
    ENDSELECT.
    SELECT * FROM SBOOK INTO SBOOK_WA
      WHERE CARRID = 'LH' AND
            CONNID = '0400'.
    ENDSELECT.

    Hi,
    The Check condition will be true, so it will not going to display the records, it will fill the internal table.
    In the Where condition it will fill the Internal table as well as it will display also.
    Hope now your query is solved
    Regards,
    Dhruv Shah

  • Query Bean Where clause

    In Query Bean (Auto Customization Criteria) , OA FWK generate
    where clause automatically .
    I want to disable this criteria in some cases. Is there any way exists?
    (Disable means remove some condition )
    I want to see runtime query with where clause(In case of Query Bean) . Is there is any way to find out it?
    Thanks, Avaneesh

    You have couple of options to do this,
    1. You can use getCriteria API on the queryBean which will stop the query execution by framework and it's the responsibility of the developer to use the dictionary returned by getCriteria and bind the complete where clause yourself.
    2. You can use getNonViewAttrCriteria API on queryBean. This provides a dictionary of criteria on the column which are not mapped to a viewAttribute, for example if you want a poplist in the search region which has values like "Last one week", "Last 2 weeks", "Last 15 days" and so on and want to bind the criteria your self by interpreting the value selected.
    In this case, framework still binds the where clause for the other criteria's which are mapped to a vo based column in the table and expects the developer to bind the criteria for these non viewattribute mappings in the controller.
    I think your requirement fits in the second approach.

  • Dynamic select query with dynamic where condition

    Hi all,
    I want to use the dynamic select query with dynamic where condition. For that I used the below code but I am getting dump when using this code.
    Please advice, if there is any other way to achieve this requirement.
    Thanks,
    Sanket Sethi
    Code***************
    PARAMETERS: p_tabnam      TYPE tabname,
                p_selfl1      TYPE edpline,
                p_value       TYPE edpline,
                p_where1      TYPE edpline .
    DATA: lt_where    TYPE TABLE OF edpline,
          lt_sel_list TYPE TABLE OF edpline,
          l_wa_name   TYPE string,
          ls_where    TYPE edpline,
          l_having    TYPE string,
          dref        TYPE REF TO data,
          itab_type   TYPE REF TO cl_abap_tabledescr,
          struct_type TYPE REF TO cl_abap_structdescr,
          elem_type   TYPE REF TO cl_abap_elemdescr,
          comp_tab    TYPE cl_abap_structdescr=>component_table,
          comp_fld    TYPE cl_abap_structdescr=>component.
    TYPES: f_count TYPE i.
    FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
    *                <ls_outtab> TYPE ANY,
                    <l_fld> TYPE ANY.
    struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
    elem_type   ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
    comp_tab = struct_type->get_components( ).
    comp_fld-name = 'F_COUNT'.
    comp_fld-type = elem_type.
    APPEND comp_fld TO comp_tab.
    struct_type = cl_abap_structdescr=>create( comp_tab ).
    itab_type   = cl_abap_tabledescr=>create( struct_type ).
    l_wa_name = 'l_WA'.
    CREATE DATA dref TYPE HANDLE itab_type.
    ASSIGN dref->* TO <lt_outtab>.
    *CREATE DATA dref TYPE HANDLE struct_type.
    *ASSIGN dref->* TO <ls_outtab>.
    * Creation of the selection fields
    APPEND p_selfl1 TO lt_sel_list.
    APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
    ** Creation of the "where" clause
    *CONCATENATE p_selfl1 '= '' p_value ''.'
    *            INTO ls_where
    *            SEPARATED BY space.
    *APPEND ls_where TO lt_where.
    * Creation of the "where" clause
    APPEND p_where1 TO lt_where.
    * Creation of the "having" clause
    l_having = 'count(*) >= 1'.
    * THE dynamic select
    SELECT          (lt_sel_list)
           FROM     (p_tabnam)
           INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>.
    *       WHERE    (lt_where).

    Hi Sanket,
    The above given logic of mine works for you, put the code in the If condition and try-
    just like below:
    IF NOT P_EBELN IS INITIAL.
    lt_where = '& = ''&'' '.
    REPLACE '&' WITH p_ebeln INTO lt_where.
    REPLACE '&' WITH field_value INTO lt_where.
    SELECT (lt_sel_list) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
    FROM (p_tabnam)
    WHERE (lt_where).
    ENDIF.
    thanks\
    Mahesh

  • Absolute dynamic select query with dynamic join and where

    Has anyone ever tried creating an absolutely dynamic SELECT query with dynamic Join and Where conditions.
    I have a requirement of creating such a query in an Utility Class, and i have written the code. But its throwing my sysntax errors.
    Please let me know where am I going wrong OR is it really possible to create such a dynamic Query??
        SELECT (FIELDS) INTO TABLE IT_TABLES
          FROM ( (ME->TABLE1)  inner join ( me->table2 )
          on ( on_condition ) )
          WHERE (me->where_fields).
    Ags.

    It worked for me in a following way:
    select * into corresponding fields of table <result_table>
            from (join_string)
            where (l_where).
    Where the contents of join_string were dynamically build using concatenation. So it will be something like
    concatenate ME->TABLE1 'as a INNER JOIN' me->table2 'as b ON (' into join_string separated by space.
    <...>
    add here matching/reference colums, something like
    concatenate 'a~' me->TABLE1_JOIN_COL into temp1.
    concatenate 'b~' me->TABLE2_JOIN_COL into temp2.
    concatenate join_string temp1 '=' temp2 into join_string separated by space.
    <...>
    concatenate join_string ')' into join_string separated by space.
    And then use similar approach for l_where variable.

  • Outer Join with Where Clause in LTS

    HI all,
    I have a requirement like this in ANSI SQL:
    select p1.product_id, p1.product_name, p2.product_group
    from product p1 left outer join product_group p2 on p1.product_id = p2.product_id
    and p2.product_group = 'NEW'
    In Regular SQL:
    select p1.product_id, p1.product_name, p2.product_group
    from product p1, product_group p2
    WHERE p1.product_id *= p2.product_id and p2.product_group = 'NEW'
    In OBIEE, I am using a left outer join between these two in Logical table Source, and also, Gave
    p2.product_group = 'NEW' in WHERE clause of LTS.
    This doesn't seem to solve purpose.
    Do you have any idea how to convert WHERE clause in physical query that OBIEE is generating to something like
    product p1 left outer join product_group p2 on p1.product_id = p2.product_id AND p2.product_group = 'NEW'
    I am using Version 10.1.3.4.1
    Creating an Opaque view would be my last option though.

    Hello
    I have read your post and the responses as well. and I understand that you have issues with the Outer Join with where Clause in LTS.
    Try this solution which worked for me (using your example ) -
    1. In the Physical Layer created a Complex join between PRODUCT and PRODUCT_GROUP tables and use this join relationship :
    PRODUCT.PROD_ID = PRODUCT_GROUP.PROD_ID  AND  PRODUCT_GROUP.GROUP_NAME = 'MECHANICAL' 
    2. In the General Tab of PRODUCT table LTS add PRODUCT_GROUP  table and select Join Type as Left Outer Join.
    3. Check Consistency and make sure there are no errors .
    when you run a request you should see the following query generated -
    select distinct T26908.PROD_ID as c1,
         T26908.PROD_NAME as c2,
         T26912.GROUP_NAME as c3
    from
         PRODUCT T26908 left outer join PRODUCT_GROUP T26912 On T26908.PROD_ID = T26912.PROD_ID and T26912.GROUP_NAME = 'MECHANICAL'
    order by c1, c2, c3
    Hope this works for you. If it does please mark this response as 'Correct' .
    Good Luck.

  • Export (expdp) with where clause

    Hello Gurus,
    I am trying to export with where clause. I am getting below error.
    Here is my export command.
    expdp "'/ as sysdba'" tables = USER1.TABLE1 directory=DATA_PUMP dumpfile=TABLE1.dmp logfile=TABLE1.log query= “USER1.TABLE1:where auditdate>'01-JAN-10'” Here is error
    [keeth]DB1 /oracle/data_15/db1> DATA_PUMP dumpfile=TABLE1.dmp logfile=TABLE1.log query= USER1.TABLE1:where auditdate>'01-JAN-10'                    <
    Export: Release 11.2.0.3.0 - Production on Tue Mar 26 03:03:26 2013
    Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    Starting "SYS"."SYS_EXPORT_TABLE_03":  "/******** AS SYSDBA" tables=USER1.TABLE1 directory=DATA_PUMP dumpfile=TABLE1.dmp logfile=TABLE1.log query= USER1.TABLE1:where auditdate
    Estimate in progress using BLOCKS method...
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    Total estimation using BLOCKS method: 386 MB
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
    Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
    Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/TRIGGER
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    ORA-31693: Table data object "USER1"."TABLE1" failed to load/unload and is being skipped due to error:
    ORA-00933: SQL command not properly ended
    Master table "SYS"."SYS_EXPORT_TABLE_03" successfully loaded/unloaded
    Dump file set for SYS.SYS_EXPORT_TABLE_03 is:
      /oracle/data_15/db1/TABLE1.dmp
    Job "SYS"."SYS_EXPORT_TABLE_03" completed with 1 error(s) at 03:03:58Version
    SQL> select * from v$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE    11.2.0.3.0      Production
    TNS for IBM/AIX RISC System/6000: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production

    Hello,
    You should use parameter file.Another question i can see you are using 11g.Why don't you use data pump?.
    Data Pump is faster and have more features and enhancement than regular imp and exp.
    You can do the following:
    sqlplus / as sysdba
    Create directory DPUMP_DIR3  for 'Type here your os path that you want to export to';then touch a file:
    touch par.txt
    In this file type the following the following :
    tables=schema.table_name
    dumpfile=yourdump.dmp
    DIRECTORY=DPUMP_DIR3
    logfile=Your_logfile.log
    QUERY =abs.texp:"where hiredate>'01-JAN-13' "then do the following
    expdp username/password parfile='par.txt'
    If you will import from Oracle 11g to version 10g then you have to addthe parameter "version=10" to the parameter file above
    BR
    Mohamed ELAzab
    http://mohamedelazab.blogspot.com/

  • Require a query with all clauses

    Hi All,
    I have been asked for a query to write by using all sql clauses like group by, where, having, order by in a single query. I just wrote the below query but I did not get out put. Please do correct where I am missing.
    select deptno, max(sal) from emp group by deptno having  deptno > ( select distinct deptno from emp where deptno >= 20)
    Thanks for your time and info.

    1007912 wrote:
    select deptno, max(sal) from emp group by deptno having  deptno > ( select distinct deptno from emp where deptno >= 20)
    Logically it was wrong; as your sub-query will return more then one record for comparison. By the way if you are querying the same data from same table then why you need a sub query in having clause! No need of it. Even if you are not using any aggregate predicates then use it with where clause.
    This is how your query and output should come out!
    SQL> select
      2   deptno, max(sal)
      3  from emp
      4  where deptno >= 20
      5  group by deptno
      6  order by 1
      7  /
        DEPTNO   MAX(SAL)
            20       3000
            30       2850
    SQL>

  • Outer join on query with OR clause

    hi all, i am having problem outerjoining a query with or clause
    here is my data
    WITH table1 AS
    SELECT  'test' txt1, 'pak' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    SELECT  null txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, 'ced3' txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual
    ,table2 AS
    SELECT 111 pid, 'test' txt1, 'pak4' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    SELECT 222 pid, 'test1' txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    SELECT 333 pid, 'test2' txt1, 'pak3' txt2, 'ced3' txt3, 'su4' txt4 FROM dual UNION ALL
      SELECT 444 pid, 'test2' txt1, 'pak3' txt2, 'ced4' txt3, 'su3' txt4 FROM dual
    SELECT b.pid, a.*
    from table1 a, table2 b
    WHERE (a.txt1 = b.txt1 OR
           a.txt1 IS NULL AND a.txt2=b.txt2 OR
           Nvl(a.txt2, a.txt1) IS NULL AND a.txt3 = b.txt3 OR
           Nvl(a.txt2, a.txt1) IS NULL  AND a.txt3 IS NULL AND a.txt4 = b.txt4
           ) as you can see i am joining table1 and table 2. i am joining with txt1, if txt1 is null then join by txt2, if null then join by txt3 and so on.
    the code above product this output
    PID     TXT1     TXT2     TXT3     TXT4
    ===     ====     ===   ==== ====
    111     test     pak      ced     su
    222             pak      ced2     su2
    333                     ced3     su3
    444                          su3this output is partially correct. only 4 rows were display and two was left out
    SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
    SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual i tried using outer join but oracle complain that i cannot use outerjoin with OR clause.
    can someone help modify my query to display the output below ?
    PID     TXT1     TXT2     TXT3     TXT4
    ===    ====      ===   ====  ====
    111     test     pak      ced     su
    222             pak      ced2     su2
    333                     ced3     su3
    444                          su3
    NULL  NULL   NULL    NULL   NULL
         test5

    Not sure you can do it with the Oracle style outer joins, but wioth ANSI style joins it is simple, actually exactly as you had it.
    SQL> set null null;
    SQL> WITH table1 AS (
      2   SELECT  'test' txt1, 'pak' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
      3   SELECT  null txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
      4   SELECT  null txt1, NULL txt2, 'ced3' txt3, 'su3' txt4 FROM dual UNION ALL
      5   SELECT  null txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
      6   SELECT  'text5' txt1, NULL txt2, null txt3, 'su3' txt4 FROM dual UNION ALL
      7   SELECT  null txt1, NULL txt2, null txt3, null txt4 FROM dual),
      8  table2 AS (
      9   SELECT 111 pid, 'test' txt1, 'pak4' txt2, 'ced' txt3, 'su' txt4 FROM dual UNION ALL
    10   SELECT 222 pid, 'test1' txt1, 'pak' txt2, 'ced2' txt3, 'su2' txt4 FROM dual UNION ALL
    11   SELECT 333 pid, 'test2' txt1, 'pak3' txt2, 'ced3' txt3, 'su4' txt4 FROM dual UNION ALL
    12   SELECT 444 pid, 'test2' txt1, 'pak3' txt2, 'ced4' txt3, 'su3' txt4 FROM dual)
    13  SELECT b.pid, a.*
    14  from table1 a
    15     LEFT JOIN table2 b
    16        ON (a.txt1 = b.txt1 OR
    17            a.txt1 IS NULL AND a.txt2=b.txt2 OR
    18            Nvl(a.txt2, a.txt1) IS NULL AND a.txt3 = b.txt3 OR
    19            Nvl(a.txt2, a.txt1) IS NULL  AND a.txt3 IS NULL AND a.txt4 = b.txt4);
           PID TXT1   TXT2   TXT3   TXT4
           111 test   pak    ced    su
           222 null   pak    ced2   su2
           333 null   null   ced3   su3
           444 null   null   null   su3
    null       text5  null   null   su3
    null       null   null   null   nullJohn

  • How to re-write this big SELECT Query with INNER JOINs?

    Hi Experts
    I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
    SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
    INTO TABLE INT_TAB_RES
    FROM VBAP AS A INNER JOIN VBAK AS D
    ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
    INNER JOIN VBEP AS E
    ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
    WHERE  A~VBELN IN s_VBELN AND
           D~auart in s_auart AND
           D~vkorg in s_vkorg AND
           D~vbtyp eq 'C'     AND
           ( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
           OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
           A~ABGRU EQ SPACE AND
           A~MTVFP IN R_MTVFP AND
           A~PRCTR IN R_PRCT AND
           E~ETENR EQ '1'.
    SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
    DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
    CHECK NOT INT_TAB_RES [] IS INITIAL.
    SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
    FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
    WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
    Now, the requirement is:
    I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
    Please help.
    Thx n Rgds

    Hi Nagraj
    As of your suggestion, I have re-written the query as below:
    * Declarations
    TYPES: BEGIN OF TYP_COLL_ORD,
            VBELN  LIKE VBAK-VBELN,
            POSNR  LIKE VBUP-POSNR,
            MATNR  LIKE VBAP-MATNR,
            KWMENG LIKE VBAP-KWMENG,
            KBMENG LIKE VBAP-KBMENG,
            ERDAT  LIKE VBAK-ERDAT,
            ERZET  LIKE VBAK-ERZET,
            PSTYV  LIKE VBAP-PSTYV,
            AUART  LIKE VBAK-AUART, u201Calready exists in type
            ETTYP  LIKE VBEP-ETTYP,
            EDATU  LIKE VBEP-EDATU.
    TYPES: END OF TYP_COLL_ORD.
    DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
    TYPES: BEGIN OF TYP_VBUK,
            AUART  LIKE VBAK-AUART, u201Chave added this field
            VBELN  LIKE VBUK-VBELN,
            UVALL  LIKE VBUK-UVALL,
            CMGST  LIKE VBUK-CMGST.
    TYPES: END OF TYP_VBUK.
    DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
    *QUERY#1 u2013 for VBAK & VBUK Join
    SELECT A~AUART B~VBELN B~UVALL B~CMGST
    INTO TABLE INT_VBUK
    FROM VBAK AS A INNER JOIN VBUK AS B
    ON A~VBELN EQ B~VBELN
    WHERE A~VBELN IN s_VBELN AND
    A~auart in s_auart AND
    A~vkorg in s_vkorg AND
    A~vbtyp eq 'C' AND
    B~UVALL NE 'A'.
    IF NOT INT_VBUK[] IS INITIAL.
    SORT INT_VBUK BY VBELN.
    DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
    *QUERY#2 u2013 for VBAP & VBEP Join
    SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
    INTO TABLE INT_COLL_ORD
    FROM VBAP AS A INNER JOIN VBEP AS B
    ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
    FOR ALL ENTRIES IN INT_VBUK
    WHERE A~VBELN = INT_VBUK-VBELN AND
    ( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
    OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
    A~ABGRU EQ SPACE AND
    A~MTVFP IN R_MTVFP AND
    A~PRCTR IN R_PRCT AND
    B~ETENR EQ '1'.
    ENDIF.
      SORT INT_COLL_ORD BY  VBELN POSNR ETTYP.
      DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
        COMPARING VBELN POSNR.
      CHECK NOT INT_COLL_ORD[] IS INITIAL.
      LOOP AT INT_COLL_ORD.
        CLEAR: L_MTART,L_ATPPR,L_ETTYP.
        IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
          L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
          READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
          CHECK SY-SUBRC NE 0.
    Now, the issue is:
    Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
    But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
    Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
    How to include this value into the INT_COLL_ORD?
    Plz suggest....

Maybe you are looking for

  • Closing Stock of a particular date:Function Module

    Hello Experts, I m developig a report in which I want Closing Stock amount of a particular date. Is there any funtion module for this, as in tables S031, mard I m getting Closing Stock as on date. Pls suggest Priyanka

  • I have an IPad Air with wifi only. What do I have to do when traveling in Europe to receive wifi?

    I have an IPad Air with wifi only. What do I have to do when traveling in Europe to receive wifi?

  • Anybogy know anything about PHP Classes & Objects?

    I need some guidance. I've got a problem that I think I know the way around, but need a bit of help. Basically I'm trying to create dynamic classes - which I don't think I can do. I *should* be able to, but for some reason I can't make my mind see ho

  • Home Screen and mic on iPod touch 2G

    My little brother has an iPod touch 2G and he wants to have home screen backgrounds on it, as well as multitasking. His iPod doesnt support these featurees in iOS 4, and  I heard somewhere that if you insert a strand of code into the 'SpringBoard' fi

  • How to Learn to use Numbers?

    Hey everyone, I always want to learn, and understand what I don't understand, and Numbers really stumps me. I look up functions in the help menu, but have no clue what the instructions even say! Is there any way to learn the basics of the functions a