Sorting in a Union Clause

We have a table which has data a following
Regime Country EffectiveFrom
USTax          US     01-09-1998               
UkTax UK 01-09-1983
CATax US 01-09-2010
FRTax FR 01-09-1999
We have a bind variable :bCountry.Which we are passing to the country column.
Results should be :
1. All the Regimes which belong to :bCountry passed and sorted by
a. Regimes in alphabetical ascending sort within each Country
b. 'EffectiveFrom' in descending order within Country and Regime.
Plus
1. All the Regimes which are not in :bCountry passed and sorted by
a. Tax Regime Code in alphabetical ascending sort within each Country
b. 'Effective Start Date' in descending order within Country and Tax Regime Code.
So i am planning to write the query as
select regime, country,effectivefrom
from reg_tbl
where country = :bCountry
union all
select regime, country,effectivefrom
from reg_tbl
where country <> :bCountry
But iam not sure of how to do the sorting in this case.
Please help

Hi,
Whenever you have a question, it helps if you post sample data in an executable form, such as CREATE TABLE and INSERT statements, and also post the results you want from that data.
You can ORDER BY a CASE expression:
ORDER BY  country
,            CASE
          WHEN  country = :bCountry
          THEN  regime
          ELSE  tax_regime_code
             END
,       effective_from     DESCAs always, all values returned by the CASE expression must be the same type (e.g., all VARCHAR2s or all DATEs).

Similar Messages

  • Implement a UNION clause

    Hi All,
    My requirement is simple.
    I want a Z table which combines the distinct Cost Center and Employee Pernr combination from two tables into one ztable.
    e.g.
    table 1..................................
    Cost Center         Pernr
    A                         emp1
    A                         emp2
    A                         emp3
    B                         emp1
    C                         emp3
    table 2.................................
    Cost Center         Pernr
    A                         emp3
    B                         emp1
    B                         emp2
    B                         emp3
    Resultant ZTable.................
    Cost Center         Pernr
    A                         emp1
    A                         emp2
    A                         emp3
    B                         emp1
    B                         emp2
    B                         emp3
    C                         emp3
    In SQL a simple UNION clause will do the trick, but in ABAP i am not able to achieve it.
    Please advise.

    Hi Saqi....
    types : begin of ty_tab,
          cc type string,
          emp type string,
         end of ty_tab.
    data : it_tab1 type table of ty_tab.
    data : it_tab2 type table of ty_tab.
    data : it_result type table of ty_tab.
    data : wa_tab type ty_tab.
    clear wa_tab.
    wa_tab-cc = 'A'.
    wa_tab-emp = 'emp1'.
    append wa_tab to it_tab1.
    clear wa_tab.
    wa_tab-cc = 'A'.
    wa_tab-emp = 'emp2'.
    append wa_tab to it_tab1.
    clear wa_tab.
    wa_tab-cc = 'A'.
    wa_tab-emp = 'emp3'.
    append wa_tab to it_tab1.
    clear wa_tab.
    wa_tab-cc = 'B'.
    wa_tab-emp = 'emp1'.
    append wa_tab to it_tab1.
    clear wa_tab.
    wa_tab-cc = 'C'.
    wa_tab-emp = 'emp3'.
    append wa_tab to it_tab1.
    clear wa_tab.
    wa_tab-cc = 'A'.
    wa_tab-emp = 'emp3'.
    append wa_tab to it_tab2.
    clear wa_tab.
    wa_tab-cc = 'B'.
    wa_tab-emp = 'emp1'.
    append wa_tab to it_tab2.
    clear wa_tab.
    wa_tab-cc = 'B'.
    wa_tab-emp = 'emp2'.
    append wa_tab to it_tab2.
    clear wa_tab.
    wa_tab-cc = 'B'.
    wa_tab-emp = 'emp3'.
    append wa_tab to it_tab2.
    clear wa_tab.
    loop at it_tab1 into wa_tab.
      append wa_tab to it_result.
      clear wa_tab.
      endloop.
    clear wa_tab.
    loop at it_tab2 into wa_tab.
      append wa_tab to it_result.
      clear wa_tab.
      endloop.
      sort it_result by cc emp.
      delete adjacent duplicates from it_result comparing cc emp.
      clear wa_tab.
    loop at it_result into wa_tab.
      write : wa_tab-cc.
      write : wa_tab-emp.
      clear wa_tab.
      endloop.
    Hope this above code helps you...
    It is able to create a union of the two tables...
    Please revert back in case of any issues...

  • 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

  • SQL statement with union clause

    Hi,
    I have a scenario where i need to generate a sql statement with UNION .
    Eg:
    SELECT B.YY_ID,
    FROM XXXX.YY B
    WHERE (B.YY_TY= 'as')
    UNION
    SELECT B.YY_ID
    FROM XXXX.YY_ALT_NAME A, XXXX.YY B
    WHERE (A.YY_TY= 'zx'
    AND (B.YY_ID=A.YY_ID)"
    I tried ns1:table1() union ns3:table2() in XQuery.
    But it is pushdown as 2 seperate sqls.
    Is it possible to create XQuery that pushdown as SINGLE SQL statement with union clause ?
    Thanks

    No. See 3.1.1.5 in the document I referenced in your previous post.

  • Re: adding Union clause to existing seeded VO

    Hi all,
    we have a requirement to add union clause to existing VO which is standard one.. please help me if it is possible...
    regards,
    Raghava.

    Hi,
    Following is the error stack for the code i wrote above....
    we have a standard EO based VO and we need to add union clause to the VO query.. is there any approach to achieve this except extending VO?
    The VO query is...
    SELECT pca.COST_ALLOCATION_ID,
    pca.EFFECTIVE_START_DATE,
    pca.EFFECTIVE_END_DATE,
    pca.BUSINESS_GROUP_ID,
    pca.COST_ALLOCATION_KEYFLEX_ID,
    pca.ASSIGNMENT_ID,
    pca.PROPORTION as proportion,
    pca.REQUEST_ID,
    pca.PROGRAM_APPLICATION_ID,
    pca.PROGRAM_ID,
    pca.PROGRAM_UPDATE_DATE,
    pca.LAST_UPDATE_DATE,
    pca.LAST_UPDATED_BY,
    pca.LAST_UPDATE_LOGIN,
    pca.CREATED_BY,
    pca.CREATION_DATE,
    pca.OBJECT_VERSION_NUMBER,
    pca.ROWID
    FROM PAY_COST_ALLOCATIONS_F pca
    WHERE to_date(:1,'YYYY-MM-DD') between effective_start_date and effective_end_date and :2 = assignment_id
    The Union Clause needed is
    and 1 = 2
    union
    (select cost_allocation_flex_id,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null from test_table)
    I tried it thru CO as below...
    OAApplicationModule oa =(OAApplicationModule) oapagecontext.getApplicationModule(oawebbean);
    OAViewObject vo = (OAViewObject)oa.findViewObject("CostingEntryDetailVO");
    String whereclause ="COST_ALLOCATION_KEYFLEX_ID=6217) " +
    " AND 1=2 " +
    " union (( select cost_allocation_flex_id,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null from test_table )";
    if (vo!=null)
    vo.setWhereClause(whereclause);
    vo.executeQuery();
    The error stack is:
    oracle.apps.fnd.framework.OAException: oracle.jbo.SQLStmtException: JBO-27122: SQL error during statement preparation. Statement: SELECT * FROM (SELECT pca.COST_ALLOCATION_ID,
    pca.EFFECTIVE_START_DATE,
    pca.EFFECTIVE_END_DATE,
    pca.BUSINESS_GROUP_ID,
    pca.COST_ALLOCATION_KEYFLEX_ID,
    pca.ASSIGNMENT_ID,
    pca.PROPORTION as proportion,
    pca.REQUEST_ID,
    pca.PROGRAM_APPLICATION_ID,
    pca.PROGRAM_ID,
    pca.PROGRAM_UPDATE_DATE,
    pca.LAST_UPDATE_DATE,
    pca.LAST_UPDATED_BY,
    pca.LAST_UPDATE_LOGIN,
    pca.CREATED_BY,
    pca.CREATION_DATE,
    pca.OBJECT_VERSION_NUMBER,
    pca.ROWID
    FROM PAY_COST_ALLOCATIONS_F pca
    WHERE to_date(:1,'YYYY-MM-DD') between effective_start_date and effective_end_date and :2 = assignment_id) QRSLT WHERE (nullCOST_ALLOCATION_KEYFLEX_ID=6217 AND 1=2 union (( select cost_allocation_flex_id,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null from test_table )) ORDER BY effective_start_date
         at oracle.apps.fnd.framework.OAException.wrapperException(OAException.java:896)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:612)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.OAPageLayoutHelper.processRequest(OAPageLayoutHelper.java:1166)
         at oracle.apps.fnd.framework.webui.beans.layout.OAPageLayoutBean.processRequest(OAPageLayoutBean.java:1569)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.form.OAFormBean.processRequest(OAFormBean.java:385)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.OABodyBean.processRequest(OABodyBean.java:353)
         at oracle.apps.fnd.framework.webui.OAPageBean.processRequest(OAPageBean.java:2513)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:1894)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:538)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:426)
         at OA.jspService(_OA.java:212)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:713)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.ServletRequestDispatcher.unprivileged_forward(ServletRequestDispatcher.java:259)
         at com.evermind.server.http.ServletRequestDispatcher.access$100(ServletRequestDispatcher.java:51)
         at com.evermind.server.http.ServletRequestDispatcher$2.oc4jRun(ServletRequestDispatcher.java:193)
         at oracle.oc4j.security.OC4JSecurity.doPrivileged(OC4JSecurity.java:284)
         at com.evermind.server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:198)
         at com.evermind.server.http.EvermindPageContext.forward(EvermindPageContext.java:395)
         at OA.jspService(_OA.java:221)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
         at oracle.apps.jtf.base.session.ReleaseResFilter.doFilter(ReleaseResFilter.java:26)
         at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:15)
         at oracle.apps.fnd.security.AppsServletFilter.doFilter(AppsServletFilter.java:318)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:621)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
         at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:313)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:199)
         at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
         at java.lang.Thread.run(Thread.java:619)
    ## Detail 0 ##
    java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis
         at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
         at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
         at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
         at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
         at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
         at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
         at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:791)
         at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:866)
         at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1189)
         at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
         at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3431)
         at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
         at oracle.jbo.server.QueryCollection.buildResultSet(QueryCollection.java:860)
         at oracle.jbo.server.QueryCollection.executeQuery(QueryCollection.java:669)
         at oracle.jbo.server.ViewObjectImpl.executeQueryForCollection(ViewObjectImpl.java:3723)
         at oracle.jbo.server.OAJboViewObjectImpl.executeQueryForCollection(Unknown Source)
         at oracle.apps.fnd.framework.server.OAViewObjectImpl.executeQueryForCollection(OAViewObjectImpl.java:4559)
         at oracle.jbo.server.ViewRowSetImpl.execute(ViewRowSetImpl.java:743)
         at oracle.jbo.server.ViewRowSetImpl.executeQueryForMasters(ViewRowSetImpl.java:892)
         at oracle.jbo.server.ViewRowSetImpl.executeQueryForMode(ViewRowSetImpl.java:806)
         at oracle.jbo.server.ViewRowSetImpl.executeQuery(ViewRowSetImpl.java:800)
         at oracle.jbo.server.ViewObjectImpl.executeQuery(ViewObjectImpl.java:3643)
         at oracle.apps.fnd.framework.server.OAViewObjectImpl.executeQuery(OAViewObjectImpl.java:439)
         at genus.oracle.apps.pay.selfservice.assignmentcosting.webui.xxxAssignmentCostingEntryCO.processRequest(xxxAssignmentCostingEntryCO.java:80)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:596)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.OAPageLayoutHelper.processRequest(OAPageLayoutHelper.java:1166)
         at oracle.apps.fnd.framework.webui.beans.layout.OAPageLayoutBean.processRequest(OAPageLayoutBean.java:1569)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.form.OAFormBean.processRequest(OAFormBean.java:385)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.OABodyBean.processRequest(OABodyBean.java:353)
         at oracle.apps.fnd.framework.webui.OAPageBean.processRequest(OAPageBean.java:2513)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:1894)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:538)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:426)
         at OA.jspService(_OA.java:212)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:713)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.ServletRequestDispatcher.unprivileged_forward(ServletRequestDispatcher.java:259)
         at com.evermind.server.http.ServletRequestDispatcher.access$100(ServletRequestDispatcher.java:51)
         at com.evermind.server.http.ServletRequestDispatcher$2.oc4jRun(ServletRequestDispatcher.java:193)
         at oracle.oc4j.security.OC4JSecurity.doPrivileged(OC4JSecurity.java:284)
         at com.evermind.server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:198)
         at com.evermind.server.http.EvermindPageContext.forward(EvermindPageContext.java:395)
         at OA.jspService(_OA.java:221)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
         at oracle.apps.jtf.base.session.ReleaseResFilter.doFilter(ReleaseResFilter.java:26)
         at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:15)
         at oracle.apps.fnd.security.AppsServletFilter.doFilter(AppsServletFilter.java:318)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:621)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
         at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:313)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:199)
         at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
         at java.lang.Thread.run(Thread.java:619)
    java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis
         at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
         at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
         at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
         at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
         at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
         at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
         at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:791)
         at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:866)
         at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1189)
         at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
         at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3431)
         at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
         at oracle.jbo.server.QueryCollection.buildResultSet(QueryCollection.java:860)
         at oracle.jbo.server.QueryCollection.executeQuery(QueryCollection.java:669)
         at oracle.jbo.server.ViewObjectImpl.executeQueryForCollection(ViewObjectImpl.java:3723)
         at oracle.jbo.server.OAJboViewObjectImpl.executeQueryForCollection(Unknown Source)
         at oracle.apps.fnd.framework.server.OAViewObjectImpl.executeQueryForCollection(OAViewObjectImpl.java:4559)
         at oracle.jbo.server.ViewRowSetImpl.execute(ViewRowSetImpl.java:743)
         at oracle.jbo.server.ViewRowSetImpl.executeQueryForMasters(ViewRowSetImpl.java:892)
         at oracle.jbo.server.ViewRowSetImpl.executeQueryForMode(ViewRowSetImpl.java:806)
         at oracle.jbo.server.ViewRowSetImpl.executeQuery(ViewRowSetImpl.java:800)
         at oracle.jbo.server.ViewObjectImpl.executeQuery(ViewObjectImpl.java:3643)
         at oracle.apps.fnd.framework.server.OAViewObjectImpl.executeQuery(OAViewObjectImpl.java:439)
         at genus.oracle.apps.pay.selfservice.assignmentcosting.webui.xxxAssignmentCostingEntryCO.processRequest(xxxAssignmentCostingEntryCO.java:80)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:596)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.OAPageLayoutHelper.processRequest(OAPageLayoutHelper.java:1166)
         at oracle.apps.fnd.framework.webui.beans.layout.OAPageLayoutBean.processRequest(OAPageLayoutBean.java:1569)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.form.OAFormBean.processRequest(OAFormBean.java:385)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:964)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequestChildren(OAWebBeanHelper.java:931)
         at oracle.apps.fnd.framework.webui.OAWebBeanHelper.processRequest(OAWebBeanHelper.java:655)
         at oracle.apps.fnd.framework.webui.OAWebBeanContainerHelper.processRequest(OAWebBeanContainerHelper.java:251)
         at oracle.apps.fnd.framework.webui.beans.OABodyBean.processRequest(OABodyBean.java:353)
         at oracle.apps.fnd.framework.webui.OAPageBean.processRequest(OAPageBean.java:2513)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:1894)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:538)
         at oracle.apps.fnd.framework.webui.OAPageBean.preparePage(OAPageBean.java:426)
         at OA.jspService(_OA.java:212)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:713)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.ServletRequestDispatcher.unprivileged_forward(ServletRequestDispatcher.java:259)
         at com.evermind.server.http.ServletRequestDispatcher.access$100(ServletRequestDispatcher.java:51)
         at com.evermind.server.http.ServletRequestDispatcher$2.oc4jRun(ServletRequestDispatcher.java:193)
         at oracle.oc4j.security.OC4JSecurity.doPrivileged(OC4JSecurity.java:284)
         at com.evermind.server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:198)
         at com.evermind.server.http.EvermindPageContext.forward(EvermindPageContext.java:395)
         at OA.jspService(_OA.java:221)
         at com.orionserver.http.OrionHttpJspPage.service(OrionHttpJspPage.java:59)
         at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:379)
         at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:594)
         at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:518)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
         at oracle.apps.jtf.base.session.ReleaseResFilter.doFilter(ReleaseResFilter.java:26)
         at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:15)
         at oracle.apps.fnd.security.AppsServletFilter.doFilter(AppsServletFilter.java:318)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:621)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
         at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
         at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:313)
         at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:199)
         at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
         at java.lang.Thread.run(Thread.java:619)

  • Union clause cut off decimal places?

    Dear all,
    I am trying to create a query with "union" clause (to join 3 parts of records).
    If I run them separately, I got the figures in Qty field (of Bill of Material) with 3 decimal places.
    However, I only got 2 decimal places, when I combined the 3 sub-queries.
    Any idea please?
    Many thanks
    Yang
    Edited by: Y on Jul 10, 2009 4:16 PM

    Dear all,
    I just tried to create a view in the database for the "union" query, and I can see it is a 6 decimal numeric field from the SQL sever management studio, as all data are displayed fine.
    Unfortunately, I queried this view in SAP query manager, and it is still showing only 2 decimals ... 
    I am very keen to know if there is a solution.
    However, for now, I have let the view join the ITT1 table again in SAP to use the qantities figures.
    It seems that SAP only shows 2 digit for numeric fields, unless it is getting data from a generic data table?
    Cheers
    Yang

  • Using ORDER BY in UNION clause in cursor

    Hi Everybody,
    I have one situation and i need your help guys. I have to use ORDER BY in UNION of two queries and return value in cursor.
    OPEN cursor FOR
    SELECT ID, DESC,SID, ITID, SID_DESC
    FROM (SELECT A.ID,
    A.DESC,
    B.SID,
    NULL AS ITID,
    B.SID_DESC
    FROM TABLEA A, TABLEB B
    WHERE A.ID = B.ID
    order by A.SORTORDER asc,B.SORTORDER)
    UNION
    SELECT ID, DESC, ,SID,ITID, ITID_DESCRIPTION,
    FROM (SELECT A.ID,
    A.DESC,
    NULL AS SID,
    C.ITID,
    C.ITID_DESC,
    FROM TABLEA A,
    TABLEC C
    WHERE A.ID = C.ID
    order by A.SORTORDER asc,C.SORTORDER)
    I SORT ORDER is column in all three tables. TABLEA has unique number for each record as sort order. TABLEB has sortorder as 1 for each id. If id is two times then its 1 and 2 for each id. TABLEC has sort order as TABLEB, 1 for each id and if id is two times then it is 1 and 2 and id id is three times, it is 1, 2, 3.
    I am not getting correct sorting as i cant use order by like this in UNION. Please let me know how i can handle this.
    Thank you in advance to everybody.
    I will really appreciate your comments and responses.

    Hi,
    You can use ORDER BY in a sub-query, but there's usually no point in doing so, because the super-query won't necessarily preserve that order. You need to make the columns that you want to ORDER BY available to the main query, so that they can be used there.
    Try this:
    SELECT      ID, DESC,SID, ITID, SID_DESC
    FROM      (
             SELECT    A1.ID,
                        A1.DESC,
                    B.SID,
                    NULL          AS ITID,
                    B.SID_DESC,
                    A1.SORTORDER     AS sortorder_1
                    B.SORTORDER     AS sortorder_2
             FROM      TABLEA     A1,
                        TABLEB     B
             WHERE     A1.ID = B.ID
        UNION
             SELECT    A2.ID,
                        A2.DESC,
                    NULL          AS SID,
                           C.ITID,
                    C.ITID_DESC,
                    A2.SORTORDER     AS sortorder_1
                    B.SORTORDER     AS sortorder_2
             FROM      TABLEA          A2,
                        TABLEC         C
             WHERE     A2.ID = C.ID
    order by  SORTORDER_1     ASC     -- ASC is the default, but it doesn't hurt to say it
           SORTORDER_2This assumes that tableb.sortorder and tablec.sortorder have the same, or at least similar, data types. If not, explicitly convert sortorder_1 in one branch of the UNION to the type of sortorder_2 in the other branch.
    Using the same alias to mean different things in the same query is just asking for trouble. I changed the table alias a (which was used in different places to mean two different things) to A1 and A2, each of which means only one thing.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • Pipelined function with Union clause(Invalid Number(ORA-01722) error )

    Hi,
    I have a pipelined function.
    I am fetching the data with the use of a REF cursor.
    The query has two part: query1 and query2
    when I am opeing the cursor with query1 UNION query2,it is giving me Invalid Number(ORA-01722) error.
    but when I open the cursor separately for query1 and query2,I am getting the resultset for each of the query.
    query1 and query2 are running fine with UNION in the sql editor.
    But,when I put them into two variables (var1 :='query1' and var2:='query2' and try to open the cursor with var1 UNION var2 , it's giving me the error.
    But when I put query2 as 'Select ..... from dual' and then try to open the cursor for query1 UNION query2,
    then getting the result of query1 and one extra row for query2(dual).
    The FROM table set is same for query1 and query2..
    can anyone please help me , why this Invalid Number(ORA-01722) error is coming.

    query1 := 'SELECT to_char(st.store_no) STORE_NUMBER,pp.name PROGRAM_NAME, ''HBS'' DISPENSING_SYSTEM,
    pt.PATIENT_SRC_NO SOURCE_ID, null RX2000_PATIENT_IDENTIFIER, pt.last_name PATIENT_LAST_NAME, pt.first_name
    PATIENT_FIRST_NAME, to_char(LPAD (pppd.rx_number, 7, '' '')|| ''-''|| LPAD (pppd.refill_number, 2, 0)) RX_REFILL
    FROM
    pega_patient_program_dispense pppd,pega_patient_program_reg pppr,health_care_profs hcp1,
    health_care_profs hcp2,prescription_dispenses pd,prescriptions p, pega_program_status_reason ppsr ,master_doctors md,
    prescription_disp_orders pdo, hbs_shipment_extract hse,stores st,master_stores ms,pega_programs pp,patients pt,master_patients mpt
    WHERE
    pppr.referring_hcp_id=md.id(+) and md.dispensing_system_id=hcp1.hcp_id and
    pppr.ID=pppd.PEGA_PATIENT_PROGRAM_REG_ID and pppd.prescription_dispense_id=pd.id and pd.prescriptions_id=p.id and
    p.hcp_id=hcp2.hcp_id and ppsr.ID=pppd.PEGA_PROGRAM_STATUS_REASON_ID and pppd.prescription_dispense_id = pdo.prescriptions_dispenses_id(+) AND
    pdo.shipment_number = hse.shp_no(+) and pppd.store_id=ms.id and st.id=ms.dispensing_system_id and pppr.pega_program_id=pp.id
    and pppr.patient_id=mpt.id and pt.patient_id=mpt.dispensing_system_id and pppd.dispensing_system=''HBS'' and pp.name LIKE ''REV%''
    AND
    ((pppd.prescription_dispense_id IS NOT NULL AND pppd.quantity_dispensed &gt; 0 AND pppd.reported_date IS NULL AND
    pppd.src_delete_date IS NULL AND ((pppr.program_specific_patient_code IS NULL ) OR (hcp1.last_name IS NULL) OR
    (hcp1.first_name IS NULL) OR (hcp2.first_name IS NULL) OR (hcp2.last_name IS NULL) OR (hcp2.address_1 IS NULL) OR
    (hcp2.city IS NULL) OR (hcp2.state_cd IS NULL) OR (hcp2.zip_1 IS NULL)OR (pppr.referral_date is NULL) OR
    (hcp1.state_cd IS NULL) OR (hcp1.zip_1 IS NULL)
    OR (hcp2.zip_1 IS NULL) OR (pppr.last_status_change_date IS NULL) OR (pppr.varchar_1 IS NULL) OR
    (pppr.varchar_7 IS NULL OR pppr.varchar_7 LIKE ''NOT AVAILABLE AT REFERRAL%'') OR (pppr.varchar_5 IS NULL OR
    pppr.varchar_5 LIKE ''NOT AVAILABLE AT REFERRAL%'')) ) AND ppsr.INCLUDE_ON_EXCEPTION_RPT_IND=''Y'')
    OR
    ((pppd.authorization_number IS NULL OR NVL(TRUNC(pdo.shipped_date),TRUNC(hse.shp_act_date_dt)) NOT BETWEEN
    TRUNC(pppd.date_1) AND TRUNC(pppd.date_2)) OR (pppd.varchar_4 IS NULL OR NVL(TRUNC(pdo.shipped_date),
    TRUNC(hse.shp_act_date_dt)) NOT BETWEEN TRUNC(pppd.date_3) AND TRUNC(pppd.date_5)))
    OR
    (pppr.pega_program_competitors_id IS NULL AND (ppsr.manufacturer_status=''TRIAGE'' AND ppsr.manufacturer_reason=''COMPETITOR''))
    OR
    (ppsr.manufacturer_reason IS NULL)
    query2 := ' SELECT ''150'' STORE_NUMBER,''test'' PROGRAM_NAME , ''RX2000'' DISPENSING_SYSTEM
    ,''01'' SOURCE_ID, null RX2000_PATIENT_IDENTIFIER, ''test'' PATIENT_LAST_NAME, ''test'' PATIENT_FIRST_NAME,
    ''rx01'' RX_REFILL
    FROM
    pega_patient_program_reg pppr,health_care_profs hcp1, pega_program_status_reason ppsr ,
    master_doctors md,stores st,master_stores ms,pega_programs pp,patients pt,master_patients mpt
    WHERE
    pppr.referring_hcp_id=md.id(+) and md.dispensing_system_id=hcp1.hcp_id and
    ppsr.ID=pppr.REFERRAL_STATUS_ID and st.store_type=''M'' and pppr.store_id=ms.id and st.id=ms.dispensing_system_id
    and pppr.pega_program_id=pp.id and pppr.patient_id=mpt.id and pt.patient_id=mpt.dispensing_system_id and pp.name LIKE ''REV%''
    AND
    ((((pppr.program_specific_patient_code IS NULL ) OR (hcp1.last_name IS NULL) OR (hcp1.first_name IS NULL) OR
    (pppr.referral_date is NULL) OR (hcp1.state_cd IS NULL) OR (hcp1.zip_1 IS NULL) OR (pppr.last_status_change_date IS NULL)
    OR (pppr.varchar_1 IS NULL) OR (pppr.varchar_7 IS NULL) OR (pppr.varchar_5 IS NULL OR pppr.varchar_5 LIKE
    ''NOT AVAILABLE AT REFERRAL%'')) ) AND ppsr.INCLUDE_ON_EXCEPTION_RPT_IND=''Y'')
    OR
    (pppr.pega_program_competitors_id IS NULL AND (ppsr.manufacturer_status=''TRIAGE'' AND ppsr.manufacturer_reason=''COMPETITOR''))
    OR
    (ppsr.manufacturer_reason IS NULL)
    AND
    pppr.id NOT IN(select pppd.PEGA_PATIENT_PROGRAM_REG_ID from PEGA_PATIENT_PROGRAM_DISPENSE pppd)';
    BUT IF I put
    query2 := ' SELECT ''150'' STORE_NUMBER,''test'' PROGRAM_NAME , ''RX2000'' DISPENSING_SYSTEM
    ,''01'' SOURCE_ID, null RX2000_PATIENT_IDENTIFIER, ''test'' PATIENT_LAST_NAME, ''test'' PATIENT_FIRST_NAME,
    ''rx01'' RX_REFILL
    FROM DUAL';
    then it is giving result.

  • Union clause in Informix JDBC

    Hello for all
    I'm using informix JDBC driver and when I make a select with union it returns the error messages:
    java.sql.SQLException: Corresponding column types must be compatible for each UNION statement.
    java.sql.SQLException: Must have the same number of selected columns in each UNION element.
    Please, help me
    Thanks

    this is a problem in your SQL-statement:
    there should be equal number of columns and they should be all of the same type:
    select id, description, url from table1
    union
    select description from table2is wrong....
    select id, description, url from table1
    union
    select number,name, address from table2is right, because the number of columns is equal and the type is equal...
    Consider this: you are appending the output of one query with another.... it all should match in order for it to be 1 table....
    hth,
    SurfMan

  • Can I eliminate '0' in Union Clause?

    Hi,
    My Output is ,
    select to_char(duedate,'yyyymm') duemth,count(duedate) cntduedt,0 cntwrkdt from a1
    group by to_char(duedate,'yyyymm')
    union
    select to_char(nonworkdate,'yyyymm') duemth,0 cntduedt, count(nonworkdate) cntwrkdt from b1
    group by to_char(nonworkdate,'yyyymm');
    DUEMTH     CNTDUEDT     CNTWRKDT
    200502     0     2
    200502     1     0
    200503     1     0
    200506     1     0
    200511     0     1
    200512     0     1
    200512     1     0
    My Required Output is,
    DUEMTH CNTDUEDT CNTWRKDT
    200502 1 2
    200503 1 0
    200506 1 0
    200511 0 1
    200512 1 1
    Can anybody write a query for my req.?
    Thanks in Advance,
    SSV Hari.

    1  select deptno,dname,null empno
    2  from dept
    3  union
    4  select null,ename,empno
    5* from emp
    QL> /
       DEPTNO DNAME               EMPNO
           10 ACCOUNTING
           12 fgdfgdfgdfgd
           20 RESEARCH
           30 SALES
           40 OPERATIONS
           60 TEST
           70 TEST
              ADAMS                7876
              ALLEN                7499
              BLAKE                7698
              CLARK                7782

  • SQR Union Clause

    below is a code snippet.Is there a way to insure the data in the SELECT statement is subject to the same manipulations as the data in the BEGIN_SELECT? begin-selectA.GD_ENTITY_ID A.GD_ENTITY_OIDA.GD_ENTITY_CLASSA.GD_EFFDTA.GD_ATTN_TOA.ADDRESS3A.ADDRESS4A.STATEA.CITYA.POSTALB.GDB_PLB_NUMB.GDB_PLB_LTR_IDB.GDB_PLB_PERIODB.GDB_PLB_TYPEC.GDB_LAST_BILL_DTD.AMOUNT let $GD_ENTITY_ID = &GD_ENTITY_ID let $GD_ENTITY_OID = &A.GD_ENTITY_OID let $GD_ENTITY_CLASS = upper(&A.GD_ENTITY_CLASS) let $GD_EFFDT = &A.GD_EFFDT let $GD_ATTN_TO = &A.GD_ATTN_TO let $ADDRESS3 = &A.ADDRESS3 let $ADDRESS4 = &A.ADDRESS4 let $STATE = &A.STATE let $CITY = &A.CITY let $POSTAL = &A.POSTAL let #GDB_PLB_NUM = &B.GDB_PLB_NUM let $GDB_PLB_LTR_ID = &B.GDB_PLB_LTR_ID let $GDB_PLB_PERIOD = &B.GDB_PLB_PERIOD let $GDB_PLB_TYPE = &B.GDB_PLB_TYPE let $GDB_LAST_BILL_DT = &C.GDB_LAST_BILL_DT let #AMOUNT = &D.AMOUNT if $GD_ENTITY_OID <> $Previous_Facility or #GDB_PLB_NUM <> #Previous_Invoice if not isnull($Previous_Facility) let $Previous_Invoice = edit(#Previous_Invoice, '9999999999999999') let $Total_Amount = edit(#Total_Amount, '9,999,999.99') do Write-Detail-Record let #Total_Amount = 0 if $GD_ENTITY_OID <> $Previous_Facility add 1 to #Facilities end-if end-if end-if if $Program <> $Previous_Program let $Bill_Date = substr(&C.GDB_LAST_BILL_DT, 1, 10) let $Previous_Program = $Program do Write-Program-Record add 1 to #Programs_Summarized end-if if $GD_ENTITY_CLASS <> $Previous_Class do Write-Entity-Class-Record add 1 to #Entity_Classes end-if let $Previous_Class = $GD_ENTITY_CLASS let $Previous_Facility = $GD_ENTITY_OID let $Previous_Period = $GDB_PLB_PERIOD let $Previous_City = $CITY let #Previous_Invoice = #GDB_PLB_NUM add #AMOUNT to #Total_Amount add 1 to #Detail_Records_Summarizedfrom PS_GD_LTR_ALL_VW A , PS_GDB_PRVDR_BILL B , PS_GDB_CASE C , PS_GDB_CASE_LINE D where A.GD_ENTITY_CLASS like 'Kaiser%' and C.GDB_ACCESSION_NUM like $Program_Prefix and A.GD_ADDR_CLASS = $Address_Class and A.GD_ENTITY_ID = B.GDB_PLB_LTR_ID and B.GDB_PLB_PERIOD = $Target_PLB_Period and C.GDB_PLB_NUM = B.GDB_PLB_NUM and D.GDB_ACCESSION_NUM = C.GDB_ACCESSION_NUM and D.GDB_BILLABLE_STAT = 'Y' UNIONSELECTA.GD_ENTITY_ID,A.GD_ENTITY_OID,A.GD_ENTITY_CLASS,A.GD_EFFDT,A.GD_ATTN_TO,A.ADDRESS3,A.ADDRESS4,A.STATE,A.CITY,A.POSTAL,B.GDB_PLB_NUM,B.GDB_PLB_LTR_ID,B.GDB_PLB_PERIOD,B.GDB_PLB_TYPE,C.GDB_LAST_BILL_DT,D.GDB_AMOUNT from PS_GD_LTR_ALL_VW A , PS_GDB_PRVDR_BILL B , PS_GDB_EVENT C , PS_GDB_EVENT_LINE D where A.GD_ENTITY_CLASS like 'Kaiser%' and A.GD_ADDR_CLASS = $Address_Class and A.GD_ENTITY_ID = B.GDB_PLB_LTR_ID and B.GDB_PLB_PERIOD = $Target_PLB_Period and (B.GDB_PLB_NUM = C.GDB_PLB_NUM and C.GDB_EVENT_ID = D.GDB_EVENT_ID) order by A.GD_ENTITY_CLASS , A.GD_ENTITY_OID , A.CITYend-select

    It's basic set operations from school book mathematics..
    Two sets of data:
    SQL> select * from t1;
            RN
             1
             2
             3
             4
             5
    SQL> select * from t2;
            RN
             4
             5
             6
             7
             8The unique set of numbers contains in both sets is the UNION of those two sets:
    SQL> select * from t1 union select * from t2;
            RN
             1
             2
             3
             4
             5
             6
             7
             8
    8 rows selected.The complete set of numbers contains in both sets is the UNION ALL of those two sets:
    SQL> select * from t1 union all select * from t2;
            RN
             1
             2
             3
             4
             5
             4
             5
             6
             7
             8
    10 rows selected.The common numbers between both sets is the INTERSECTion of those two sets:
    SQL> select * from t1 intersect select * from t2;
            RN
             4
             5The numbers in 1 set but not in the other MINUS between those two sets:
    SQL> select * from t1 minus select * from t2;
            RN
             1
             2
             3And the other way:
    SQL> select * from t2 minus select * from t1;
            RN
             6
             7
             8
    SQL>

  • Sql query help,i need a query to get like this without union clause

    empno ename sum(sal) sum(deptno)
    200 tom 2000 10
    201 smith 3000 10
    202 alfred 3000 20
    total 8000 40

    Hi,
    group by rollup (empno,ename) add one row in the end of each same group empno,ename : empno ename sum(sal) sum(deptno) and one in the end ( total)
    but group by rollup ((empno,ename)) add only one row in the end total ..... SQL> select nvl(to_char(empno),'Total'), ename,sum(sal),sum(deptno)
      2  from emp
      3  group by rollup (empno,ename)
      4  /
    NVL(TO_CHAR(EMPNO),'TOTAL')              ENAME        SUM(SAL) SUM(DEPTNO)
    7900                                     JAMES             950          30
    7900                                                       950          30
    7369                                     SMITH             800          20
    7369                                                       800          20
    7499                                     ALLEN            1600          30
    7499                                                      1600          30
    7521                                     WARD             1250          30
    7521                                                      1250          30
    7566                                     JONES            2975          20
    7566                                                      2975          20
    7654                                     MARTIN           1250          30
    7654                                                      1250          30
    7698                                     BLAKE            2850          30
    7698                                                      2850          30
    7782                                     CLARK            2450          10
    7782                                                      2450          10
    7788                                     SCOTT            3000          20
    7788                                                      3000          20
    7839                                     KING             5000          10
    7839                                                      5000          10
    7844                                     TURNER           1500          30
    7844                                                      1500          30
    7876                                     ADAMS            1100          20
    7876                                                      1100          20
    7902                                     FORD             3000          20
    7902                                                      3000          20
    7934                                     MILLER           1300          10
    7934                                                      1300          10
    Total                                                    29025         310
    29 rows selected.
    SQL>  select nvl(to_char(empno),'Total'), ename,sum(sal),sum(deptno)
      2   from emp
      3   group by rollup ((empno,ename));
    NVL(TO_CHAR(EMPNO),'TOTAL')              ENAME        SUM(SAL) SUM(DEPTNO)
    7900                                     JAMES             950          30
    7369                                     SMITH             800          20
    7499                                     ALLEN            1600          30
    7521                                     WARD             1250          30
    7566                                     JONES            2975          20
    7654                                     MARTIN           1250          30
    7698                                     BLAKE            2850          30
    7782                                     CLARK            2450          10
    7788                                     SCOTT            3000          20
    7839                                     KING             5000          10
    7844                                     TURNER           1500          30
    7876                                     ADAMS            1100          20
    7902                                     FORD             3000          20
    7934                                     MILLER           1300          10
    Total                                                    29025         310
    15 rows selected.
    SQL> Edited by: Salim Chelabi on May 9, 2009 5:12 PM
    Edited by: Salim Chelabi on May 9, 2009 5:28 PM

  • Order by clause with union

    Hi,
    I wanted to know if this is possible with order by clause and union. some how union messes up the ordering of the left part of the union statement.
    assume the data is as below in table tab1
    c1 c2
    4 1
    4 2
    5 1
    5 2
    7 1
    7 2
    8 3
    9 4
    the expected output must be for all c1 < 7 ordering must be on c1 and for all c1 > 7 ordering must be on c2.
    so the query i tried was
    select c1,c2 from tab1 where c1 <7 order by c1
    union
    select c1,c2 from tab1 where c1 >=7 order by c2
    it is giving the message 'Sql command not properly ended'.
    Suggestions are welcome

    SQL>
    SQL> With t As
      2  (
      3  Select 4 c1, 1 c2 From dual Union All
      4  Select 4 ,2       From dual Union All
      5  Select 5 ,1       From dual Union All
      6  Select 5 ,2       From dual Union All
      7  Select 7 ,1       From dual Union All
      8  Select 7 ,2       From dual Union All
      9  Select 8 ,3       From dual Union All
    10  Select 9 ,4       From dual
    11  ), t1 As (Select c1,c2 From t Where c1 <7 Order By c1)
    12  ,t2 As (select c1,c2 from t where c1 >=7 Order By c2)
    13  Select * From t1 Union All
    14  Select * From t2;
            C1         C2
             4          1
             4          2
             5          1
             5          2
             7          1
             7          2
             8          3
             9          4
    8 rows selected
    SQL> /*-- Union All
      2   select c1,c2 from t where c1 >=7
      3   Order By c1,c2;
      4  */
      5 
    with union clause order by must be placed in the end,,
    select c1 from dual union all
    select c2 from dual
    order by  c1

  • Need sql query to remove duplicates using UNION ALL clause

    Hi,
    I have a sql query which has UNION clause.But the UNION clause is causing some performance issues.
    To overcome that I have used UNION ALL to improve performance but its returning duplicates.
    Kindly anyone send a sample SQL query where my primary objective is used to use UNION ALL clause and to consider unique rows (elimating duplicate
    ones)
    Any help will be needful for me
    Thanks and Regards

    why not UNION? :(
    another way also use MINUS
    SQL>
    SQL> with t as
      2  (
      3  select 1 if from dual union all
      4  select 2 if from dual union all
      5  select 1 if from dual union all
      6  select 3 if from dual union all
      7  select 3 if from dual
      8  )
      9  ,t2 as
    10  (
    11  select 1 if from dual union all
    12  select 2 if from dual union all
    13  select 3 if from dual union all
    14  select 4 if from dual union all
    15  select 5 if from dual
    16  )
    17  (select if from t
    18  union all
    19  select if from t2)
    20  /
            IF
             1
             2
             1
             3
             3
             1
             2
             3
             4
             5
    10 rows selected
    SQL> so
    SQL>
    SQL> with t as
      2  (
      3  select 1 if from dual union all
      4  select 2 if from dual union all
      5  select 1 if from dual union all
      6  select 3 if from dual union all
      7  select 3 if from dual
      8  )
      9  ,t2 as
    10  (
    11  select 1 if from dual union all
    12  select 2 if from dual union all
    13  select 3 if from dual union all
    14  select 4 if from dual union all
    15  select 5 if from dual
    16  )
    17  (select if from t
    18  union all
    19  select if from t2)
    20  minus
    21  select -99 from dual
    22  /
            IF
             1
             2
             3
             4
             5
    SQL>

  • Default Sorting behaviour of Oracle 9i in 11g along with group by clause

    Hi,
    We have recently migrated from 9i to 11g. The reports from application comes in a jumbled fashion. Later we understood when there is a group by clause in the query, the recordset will be sorted by default in 9i and this feature is not available in 11g. Do anyone faced the same issue and resolved at the DB level.
    Only alternate we found is the change in code with addittional order by clause which will take a long time to complete and roll out the same.
    If anyone has got any immediate solution, please let me know.
    Thx in advance.
    Sheen

    Hi,
    A group by can sort (depending on the method of grouping) but it isn't necessary. If you want to sort the output you need the ORDER BY clause. There are different group by mechanismes between 9i and 11g. 10g introduced HASH GROUP BY where in 9i only the SORT GROUP BY existed. The latter gives a sorted set, the first not.
    if you want the same behaviour you can use "_gby_hash_aggregation_enabled parameter" = false, which disables the hash group by.
    Have also a look at the support document "'Group By' Does Not Guarantee a Sort Without Order By Clause In 10g and Above [ID 345048.1]".
    Herald ten Dam
    http://htendam.wordpress.com

Maybe you are looking for