SQL Question - two queries

One works, One does not.....Why?
SQL> select m.sales_rep,
2 sum(decode(to_char(m.enddate,'YYYY'),'2006',sum(d.qty*d.billrate),0)) as sales_2006,
3 sum(decode(to_char(m.enddate,'YYYY'),'2005',sum(d.qty*d.billrate),0)) as sales_2005
4 from tempnet.sysuser s, tempnet.invmast m, tempnet.invdetail d
5 where trunc(s.shortid) = trunc(m.sales_rep)
6 and m.invoice = d.invoice
7 and m.enddate >= '01-jan-2006'
8 and m.enddate <= '01-jan-2007'
9 and m.posted = 'Y'
10 and s.status = 'A'
11 and s.title = '1000232'
12 /*and m.sales_rep is not null*/
13 group by m.sales_rep;
select m.sales_rep,
ERROR at line 1:
ORA-00937: not a single-group group function
SQL>
ORA-00937 not a single-group group function
Cause: A SELECT list cannot include both a group function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, and an individual column expression, unless the individual column expression is included in a GROUP BY clause.
Action: Drop either the group function or the individual column expression from the SELECT list or add a GROUP BY clause that includes all individual column expressions listed.
SQL> select c.firstname||' '||c.lastname as name,
2 sum(decode(to_char(a.enddate,'YYYY'),'2006',(b.qty*b.billrate),0)) as inv_sales_2006,
3 sum(decode(to_char(a.enddate,'YYYY'),'2005',(b.qty*b.billrate),0)) as inv_sales_2005
4 from tempnet.invmast a, tempnet.invdetail b, tempnet.sysuser@devuser_tn8 c
5 where a.invoice = b.invoice
6 and (b.line_type in ('RH','OH','PH','DH')
7 or (b.line_type = 'CH'
8 and b.description = 'Buyout'))
9 and trunc(a.enddate) >= to_date('01-jan-2005')
10 and trunc(a.enddate) <=to_date('01-jan-2007')
11 and a.posted = 'Y'
12 and c.shortid = a.sales_rep
13 and c.title = '1000232'
14 and c.status = 'A'
15 group by c.firstname||' '||c.lastname
16 /
NAME INV_SALES_2006 INV_SALES_2005
Alex Boufidis 5059635.99 5181504.95
Anthony Tillman 1510936.49 186792.043
Bob Golder 857637.413 0
Brian Joyce 1285177.53 25005.9
Chris Batten 526838.688 0
Clark McHenry 159057.095 0
Craig Ward 2108217.81 736089.318
Daniel Fried 372353.863 0

One works, One does not.....Why?You've SUM function twice in the first query, but not in the second one..
sum(decode(to_char(m.enddate,'YYYY'),'2006',sum(d.qty*d.billrate),0))

Similar Messages

  • How to Combine two queries into One

    Hi Gurus,
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    SQL>
    I have following two queries. How I can convert these two queries into one with the same result ?
    Q1
    SELECT id,vdate,vendorname,venddate,vid
    FROM VENDOR
    WHERE processid=32
    AND venddate is null
    AND vid in(select vid from view_vid where type='PDX')
    AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=55)
    AND (id,vdate) in (select id,vidate from market_data where marketed_date is null)
    AND id not in (select id from location)
    Q2
    SELECT id,vdate,vendorname,venddate,vid
    FROM VENDOR
    WHERE processid=20
    AND venddate is null
    AND vid in(select vid from view_vid where type='PBX')
    AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=40)
    AND (id,vdate) in (select id,vidate from market_data where region_date is null)
    AND id not in (select id from location)
    I can UNION these two queries, but, is there any way I can write a single query with which gives me same result as above two queries?
    Any help would be highly appreciated
    Thanks in advance

    Hi,
    You can do something like this, which will be more efficient than a UNION:
    SELECT  id, vdate, vendorname, venddate, vid
    FROM  vendor
    WHERE  (   (    -- Conditions that apply only to q1
                               processid = 32
                AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PDX')
                AND (id,vdate) NOT IN ( SELECT id, vdate
                                        FROM   vkfl_is
                                        WHERE  task_id = 55
                AND (id,vdate) IN ( SELECT id, vidate
                                    FROM   market_data
                                    WHERE  marketed_date IS NULL
            OR  (  -- Conditions that apply only to q2
                     processid = 20
                AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PBX')
                AND (id,vdate) NOT IN ( SELECT id, vdate
                                        FROM   vkfl_is
                                        WHERE  task_id = 40
                AND (id,vdate) IN ( SELECT  id, vidate
                                    FROM  market_data
                                    WHERE  region_date IS NULL
                   -- The remaining conditions apply to both q1 and q2
    AND     venddate   IS NULL
    AND     id         NOT IN (SELECT id FROM location)
    I hope this answers your question.
    If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Why are two queries better than one

    I'm trying to help out our developers who are struggling with the performance of a very simple select statmenent that for some reason is exhibiting some very inexplicable results.
    I'll show the SQL as a single statement first.....
    select * from transaction1 t where t.hid in (select distinct h.hid from history1 h,filter1 f where h.fid = f.fid and f.match='value');
    transaction 1 has 250k records, history1 has about 100k as does filter1. We know generally the subselect will only return a total of half a dozen or less records (only 1 in our testing here).
    Running the above query takes around 7 seconds.
    Changing this to a with clause.....
    WITH a as (select distinct h.hid from history1 h,filter1 f where h.fid = f.fid and f.match='value') select * from transaction1 t where t,hid in (select a.hid from a);
    and the runtime drops to 3 seconds. But then re-running the first statement again also now takes 3 seconds (so I guess there's some caching going on there).
    Now what really puzzles is if we split this into two queries.....
    select distinct h.hid from history1 h,filter1 f where h.fid = f.fid and f.match='value';
    This takes .1 of a second,
    select * from transaction1 t where t.hid in (12345);
    And this takes .1 of a second.
    So why when run seperately are they so fast, yet combined they take so long. I'm a bit baffled by this. We've rewritten the same SQL in half a dozen different ways with the same result and also done the same thing with other tables as well. Is it an optimisation issue?

    Appologies for not following protocol, my first time posting on here.
    Oracle 10g clustered.
    We have limited access as it's a customers server, but the explain plan comes back as....
    PLAN_TABLE_OUTPUT
    Plan hash value: 898894568
    | Id  | Operation            | Name              | Rows  | Bytes | Cost (%CPU)|Time     |
    PLAN_TABLE_OUTPUT
    |   0 | SELECT STATEMENT     |                   | 49553 |    37M|  7075   (2)|00:01:25 |
    |*  1 |  HASH JOIN RIGHT SEMI|                   | 49553 |    37M|  7075   (2)|00:01:25 |
    |   2 |   VIEW               | VW_NSO_1          | 26131 |   331K|   476   (4)|00:00:06 |
    |*  3 |    HASH JOIN         |                   | 26131 |   791K|   476   (4)|00:00:06 |
    PLAN_TABLE_OUTPUT
    |*  4 |     TABLE ACCESS FULL| FILTER1           | 26131 |   484K|   246   (4)|00:00:03 |
    |   5 |     TABLE ACCESS FULL| HISTORY1          |   104K|  1225K|   227   (3)|00:00:03 |
    |   6 |   TABLE ACCESS FULL  | TRANSACTION1      |   199K|   150M|  6593   (1)|00:01:20 |
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       1 - access("T"."TID"="$nso_col_1")
       3 - access("H"."HID"="F"."ID")
       4 - filter("F"."MATCH"='value')
    20 rows selectedI'll admit, I'm not completely clear on what I'm reading here, other than the fact there are some big differences in the timing values
    We don't have privaleges to run trace, so I'm a bit stuffed there as I'm on the clients site, however I will get my developers to try this on our local server.
    Edited by: user1410957 on 15-Sep-2009 06:14

  • Requirement wherein I want to call two queries in parallel using ABAP Prg

    We are using RSCRM_BAPI to run a query in background and write the results to a table. We later read this table and create a file. I have a requirement u2013 wherein I want to call two queries in parallel using an ABAP program.
    For our discussion assume:
    1) Program : ZCALL_RCRMBAPI  is a program that calls RSCRM_BAPI in background and takes the query name as input
    2) Program: ZMAIN_PROGRAM  this is the main program
    The requirement is to call the ZCALL_RCRMBAPI in a loop. Also within the loop call it in parallel. Basically we have an ODS that stores ; FISCPER. So I call the two queries for a list of FISCPER.
    ZMAIN_PROGRAM - A program that reads a table (ODS ) in our case and call the two programs in a loop and both should run in parallel .
    Now if it was sequential u2013 it was easy u2013 I would just say
    Loop at L_T_FISCPER .
    Update Z_FISCPER with L_T_FISCPER-FISCPER u201C Assume Z_FISCPER is a Z table . The VARIABLE in the query Q1 and Q2 use a customer exit to read this table.
    A)       SUBMIT ZCALL_RCRMBAPI with P_QUERy = Q1 and return .
    B)      SUBMIT ZCALL_RCRMBAPI with P_QUERy = Q2 and return .
    Endloop .
    Question: How do I make these calls in parallel u2013 yet retain control in the main program?
    A)       SUBMIT ZCALL_RCRMBAPI with P_QUERy = Q1 and return .
    B)      SUBMIT ZCALL_RCRMBAPI with P_QUERy = Q2 and return .

    Instead of RSCRM BAPI you can use the RRW3_QUERY_VIEW_DATA function module for greater control over program execution...
    /people/arun.varadarajan/blog/2009/07/29/make-the-most-out-of-query-execution--part-1
    will give you pointers as to how this can be done...

  • 0PA_C01  different results from two queries on the same cube ....

    HI
    Can you please help with this  problems ...
    i am running a two queries with the same restrictions e.g
    Sep 08 for employee 22345 ,
    In one report it shows the Pay Scale level as A1 , then in the other report it shows Pay scale Scale level as A2  ,
    looking  at the master data in 0employee  , the first report is right ... this is how the data looks like in 0employee
    Employee  Valid from      To                Pay scale Level
    222345       2007-11-03   2008-09-30     A1
    222345       2008-10-01   9999-12-31     A2
    Can someone please shed some light on this , im thinking it has something to do with update rule but even that is supposed to use last date of the month , not 1st day of the following month. The Cube is a stndard cube 0PA_C01 and the update rule aswell .....

    Hi,
    Please check in the cube whether the data for the Employee is getting with two values like shown in your question:
    Employee Valid from To Pay scale Level
    222345 2007-11-03 2008-09-30 A1
    222345 2008-10-01 9999-12-31 A2
    and also check whether when the data loaded to the cube.
    There may be some change in the report structure where the difference is getting may in the column wise or row wise restriction may present.
    Please check on the structure of the report also.
    With Regards,
    Ravi Kanth

  • SQL Question Bank and Answers for Practise

    Dear Readers:
    Does any one have any recommendation on SQL question bank and answers where I could practice my SQL.
    I have developed some basic knowledge of SQL thanks to the MS community, but I am looking for some additional Questions or textbook  recommendations which has questions and answers to queries for practice.
    Best Wishes,
    SQL75

    Hi,
    Refer below post.
    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/446b2247-5124-49c1-90c9-b7fea0aa4f83/sql-dba-books?forum=sqlgetstarted
    Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
    Praveen Dsa | MCITP - Database Administrator 2008 |
    My Blog | My Page

  • Drill down in report based on two queries

    Hi,
    I have a problem with drilling down in report which is based on two queries.
    Queries are based on different universes.
    Both queries contains almost the same dimensions but different measures.
    In my report is a calculated measure based on measures from both queries.
    In both universes are the same hierachies.
    When I drill down in report for the first time I have to chose hierarchy  but then data are filtered to the choosen  value only from one query , data from second query are not filtered and the values of calculated measure are incorrect
    How can I solve this issue without adding dimensions belonging to the hierarchies to queries.
    Please help.
    Regards.
    MG

    Hi MG,
    First of all, what do you mean by "Both queries contains almost the same dimensions but different measures"
    "Almost" is not a good word in the IT world, especially when trying to merge/join tables. You need to be exact.
    That sounds like a possible reason for the problem.
    I am also not sure about your question:
    "How can I solve this issue without adding dimensions belonging to the hierarchies to queries."
    You may have to add those dimensions to the queries. Why would you not want to?
    Thanks

  • Combining two queries to one

    I have two queries and I would like to output the results as
    one and sort by Name ASC
    Any help would be great thanks

    Either use union in your SQL query or use Coldfusion to
    combine exisitng queries and create a new query resultset.
    Something like this:
    <CFQUERY NAME="getDetailsQuery1"
    DATASOURCE="#DSNNAME#">
    SELECT * FROM getDetailsA
    </CFQUERY>
    <CFQUERY NAME="getDetailsQuery2"
    DATASOURCE="#DSNNAME#">
    SELECT * FROM getDetailsB
    </CFQUERY>
    <CFQUERY NAME="getDetails" DBTYPE="query">
    SELECT * FROM getDetailsQuery1
    UNION
    SELECT * FROM getDetailsQuery2
    ORDER BY Name ASC
    </CFQUERY>
    Hope this helps!
    Cheers / Manu.

  • Outer join: difference between two queries

    Below two queries that should give the same results in my opinion. I want all the records from u_protocol and only the value of pval.u_protocol_variable_value if present.
    Why does the outer join in query2 doesn't work like in query1?
    Query1:
    select p.u_protocol_id, i.u_protocol_variable_value
    from lims_sys.u_protocol p,
       select pval.u_protocol_id, pval.u_protocol_variable_value
       from lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval
       where pvar.u_protocol_variable_id = pval.u_protocol_variable_id
       and pvar.name = 'VALUE_Protocol_Group'
    ) i  
    where p.u_protocol_id  = i.u_protocol_id (+)
    Query2:
    select prt.u_protocol_id, pval.u_protocol_variable_value
    from lims_sys.u_protocol prt, lims_sys.u_protocol_variable pvar, lims_sys.u_protocol_value_user pval
    where pvar.u_protocol_variable_id = pval.u_protocol_variable_id
    and prt.u_protocol_id = pval.u_protocol_id (+)
    and pvar.name = 'VALUE_Protocol_Group'

    In the first query restriction pvar.name = 'VALUE_Protocol_Group' is limited to your inline view. So when you do a outer join with the u_protocol table you will get the number of records which are there in the u_protocol table.
    But when you gave the restriction pvar.name = 'VALUE_Protocol_Group' outside the inline view, the restriction was based on the resultset as a whole. So you will get only those records which have pvar.name = 'VALUE_Protocol_Group' condition satisfied.
    Hope the following illustration helps:
    SQL> CREATE TABLE TEST_TAB
      2  AS
      3  SELECT level col_1, chr(65+level-1) col_2 FROM Dual
      4  CONNECT BY LEVEL <= 10
      5  /
    Table created.
    SQL> SELECT * FROM TEST_TAB
      2  /
         COL_1 COL_
             1 A
             2 B
             3 C
             4 D
             5 E
             6 F
             7 G
             8 H
             9 I
            10 J
    10 rows selected.
    SQL> CREATE TABLE TEST_TAB_B
      2  AS
      3  SELECT level col_3, chr(65+level-1) col_4 FROM Dual
      4  WHERE Level NOT IN (2,3,4)
      5  CONNECT BY LEVEL <= 10
      6  /
    Table created.
    SQL> SELECT * FROM TEST_TAB_B
      2  /
         COL_3 COL_
             1 A
             5 E
             6 F
             7 G
             8 H
             9 I
            10 J
    7 rows selected.
    SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
      2  TEST_TAB_B a2
      3  where a1.col_1 = a2.col_3(+)
      4  order by a1.col_1
      5  /
         COL_1 COL_      COL_3 COL_
             1 A             1 A
             2 B
             3 C
             4 D
             5 E             5 E
             6 F             6 F
             7 G             7 G
             8 H             8 H
             9 I             9 I
            10 J            10 J
    10 rows selected.Notice the output without any extra conditions: You will get all the values from TEST_TAB and matching records from TEST_TAB_B. Non-matching records are outputed as NULL.
    Following Query is resemblence to your first query
    SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
      2  (SELECT * FROM TEST_TAB_B where col_4='A') a2
      3  where a1.col_1 = a2.col_3(+)
      4  order by a1.col_1
      5  /
         COL_1 COL_      COL_3 COL_
             1 A             1 A
             2 B
             3 C
             4 D
             5 E
             6 F
             7 G
             8 H
             9 I
            10 J
    10 rows selected.Here TEST_TAB_B Table is restricted with a condition which will restrict the inline view to have only one record. So when you outer join the inline view you will get output as shown above.
    The following query resembles to your second query.
    SQL> SELECT a1.col_1, a1.col_2, a2.col_3, a2.col_4 FROM TEST_TAB a1,
      2  TEST_TAB_B a2
      3  where a1.col_1 = a2.col_3(+)
      4  and a2.col_4 = 'A'
      5  order by a1.col_1
      6  /
         COL_1 COL_      COL_3 COL_
             1 A             1 A
    1 row selected.
    SQL> DROP TABLE TEST_TAB_B
      2  /
    Table dropped.
    SQL> DROP TABLE TEST_TAB
      2  /
    Table dropped.
    SQL> To understand this lets break up the resultset.
    Resultset brought by join condition would be something like :
        COL_1 COL_      COL_3 COL_
             1 A             1 A
             2 B
             3 C
             4 D
             5 E             5 E
             6 F             6 F
             7 G             7 G
             8 H             8 H
             9 I             9 I
            10 J            10 JAgreed?
    Now when you add the extra condition a2.col_4 = 'A' thecondition will act upon the above resultset there by restricting the records to:
         COL_1 COL_      COL_3 COL_
             1 A             1 AHope this helps.
    Regards,
    Jo

  • Combining two Queries

    Query 1 show me a list of users, each user should tell me how many orders, how many row, and total amount they placed <b>for a certain date</b>.
    Query 2 show me again a list of user, each user should me how many orders and how many row they placed <b>excluding that date above that are still open</b>.
    I would like these two queries to show on one report.  Is it possible?
    Query 1:
    SELECT COUNT(DISTINCT T0.UserSign) AS 'Today_order',
    COUNT(T1.PickStatus) AS 'Today_row', SUM(T1.Price *
    T1.OpenQty) AS 'Today_amount' FROM  [dbo].[RDR1] T1
    INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry = T0.DocEntry
    WHERE <b>T0.DocDate = CONVERT(DATETIME, '20051109', 112)</b>
    AND  T1.OpenQty > 0
    Query 2:
    SELECT COUNT(DISTINCT T0.UserSign) AS 'Other_order',
    COUNT(T1.PickStatus) AS 'Other_row' FROM  [dbo].[RDR1]
    T1 INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry =
    T0.DocEntry WHERE T1.OpenQty > 0  AND  <b>T0.DocDate <>
    CONVERT(DATETIME, '20051109', 112)</b>

    Hi Laura,
    It's probably easiest to use temporary tables or a cursor for this query.
    This code works for me (in SQL Query Analyzer and SBO):
    set nocount on
    create table #users (UserSign int, UserName nvarchar(30))
    create table #today (UserSign int, Today_row int, Today_amount numeric(15, 2))
    create table #other (UserSign int, Other_row int)
    insert  into #users
         select T0.USERID, T0.U_NAME from OUSR T0
    insert into #today
    SELECT DISTINCT T0.UserSign AS 'Today_order',
    COUNT(T1.PickStatus) AS 'Today_row', SUM(T1.Price *
    T1.OpenQty) AS 'Today_amount'
    FROM [dbo].[RDR1] T1
    INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry = T0.DocEntry
    WHERE T0.DocDate = CONVERT(DATETIME, '20051109', 112)
    AND T1.OpenQty > 0
    GROUP BY T0.UserSign
    insert into #other
    SELECT DISTINCT T0.UserSign AS 'Other_order',
    COUNT(T1.PickStatus) AS 'Other_row' FROM  [dbo].[RDR1]
    T1 INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry =
    T0.DocEntry WHERE T1.OpenQty > 0  AND  T0.DocDate <>
    CONVERT(DATETIME, '20051109', 112)
    GROUP BY T0.UserSign
    set nocount off
    select T0.UserSign, T0.UserName, isnull(T1.Today_row, 0) as Today_row,
         isnull(T1.Today_amount, 0) as Today_amount,
         isnull(T2.Other_row, 0) as Other_row
    from
         #users T0 left outer join #today T1 on T0.UserSign = T1.UserSign
         left outer join #other T2 on T0.UserSign = T2.UserSign
    drop table #users
    drop table #today
    drop table #other
    I've added the user name from the OUSR table to make the query a bit easier to understand.
    Hope this helps,
    Owen

  • Joining these two queries

    I have two queries. The First one is long but very simple. All it has is long list of columns.
    First one:
    SELECT CL.CLIENT_ID, CA.CASE_ID, to_char(SYSDATE,'RRMMDD'),
    CL.MASTER_CLIENT_ID,
    CL.NAME1,
    nvl(CL.NAME2,' '),
    nvl(CL.ADDRESS1,' '),
    nvl(CL.ADDRESS2,' '),
    CL.POCO_POSTAL_CODE, nvl(CL.CITY,' '),
    nvl(CL.AUTO_SURV_CODE,0),
    CA.Col_Collector_Id
    FROM CLIENT CL, CASES CA
    WHERE CA.CL_CLIENT_ID = CL.CLIENT_ID AND
    CA.CASE_ID = :current_case_id
    Second one:
    SQL SELECT nvl(REF_NO,' '), nvl(to_char(DATE_OF_JUDGEMENT,'rrmmdd'),' ')
    FROM CASE_INSTANCES
    WHERE IN_INSTANCE_TYPE IN (1,8) AND
    CA_CASE_ID = :current_case_id AND
    PETITION_DATE = (SELECT max(PETITION_DATE)
    FROM CASE_INSTANCES
    WHERE IN_INSTANCE_TYPE IN (1,8)
    AND CA_CASE_ID = :current_case_id
    AND ROWNUM < 2 );
    The :current_case_id is passed as a parameter. The table in the second query i.e. CASE_INSTANCES is related to the first query with the CA_CASE_ID. Not all records fetched by the first query have a record in the second query. So you might have to use a LEFT JOIN or something. trying to join these two has worn me out. Please help me out someone.

    Hi,
    Simplified Solution:
    SELECT * FROM (
                        SELECT CL.CLIENT_ID, CA.CASE_ID, TO_CHAR(SYSDATE,'RRMMDD'),
                        CL.MASTER_CLIENT_ID,
                        CL.NAME1,
                        NVL(CL.NAME2,' '),
                        NVL(CL.ADDRESS1,' '),
                        NVL(CL.ADDRESS2,' '),
                        CL.POCO_POSTAL_CODE, NVL(CL.CITY,' '),
                        NVL(CL.AUTO_SURV_CODE,0),
                        CA.Col_Collector_Id
                        FROM CLIENT CL, CASES CA
                        WHERE CA.CL_CLIENT_ID = CL.CLIENT_ID ) a,
                        (SELECT NVL(REF_NO,' '), NVL(TO_CHAR(DATE_OF_JUDGEMENT,'rrmmdd'),' '),CA_CASE_ID
                             FROM CASE_INSTANCES
                             WHERE IN_INSTANCE_TYPE IN (1,8) AND
                             CA_CASE_ID = :current_case_id AND
                             PETITION_DATE = (SELECT MAX(PETITION_DATE)
                                                      FROM CASE_INSTANCES
                                                      WHERE IN_INSTANCE_TYPE IN (1,8)
                                                      AND CA_CASE_ID = :current_case_id
                                                      AND ROWNUM < 2
                        ) b
    WHERE a.case_id = b.ca_case_id(+)
    Regards
    K.Rajkumar

  • Can I use same result set for two queries?

    Hi,
    Can I use the same result set for two queries?
    For example:
    ResultSet rs = null;
    rs = ps.executeQuery(query1);
    while (rs.next()) {
    rs = ps.executeQuery(query2);
    while (rs.next()) {
    Is it OK to use? or Is there any preformance issues?
    Appreciate your help.
    Thanks in advance.
    Prasad Vagolu

    Sure. You have a variable of ResultSet type. First you create a ResultSet and assign it to that variable. Then later you create another ResultSet and assign it to that same variable. Assigning an object reference to a variable takes essentially no time, and at any rate it isn't any faster to assign it to a different variable. Also, no matter whether you use one variable or two, you are still creating two ResultSets.
    However, you really should be closing your ResultSets after you finish using them. That's nothing to do with the assigning-to-a-variable question.

  • Is it possible to use two queries in a bw workbook?

    How can i attach two queries into a bw workbook?

    Hi
    I can't understood perfectly what ur asking
    but as per understanding, you mean we can't do complicated caluculation?
    If that is ur question it is not true u can do any type of caluculation and conditions
    Regards
    Kiran

  • Joining two queries causes big performance hit

    There are two queries: one with a sub query and one without
    although they both return the same data, one uses a full table scan of the indexed 'trade_index' table
    and thus is much slower
    any Ideas why it does this?:
    SQL> select count(*) from trade_index ti, counterparty_xref cx
    2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
    3 and cx.XREF_VALUE in (select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB')
    4 and cx.XREF_TYPE = 'Client RXM'
    5 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
    6 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
    COUNT(*)
    6195
    Elapsed: 00:00:15.08
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2572 Card=1 Bytes=57)
    1 0 SORT (AGGREGATE)
    2 1 HASH JOIN (Cost=2572 Card=26581 Bytes=1515117)
    3 2 HASH JOIN (Cost=460 Card=5619 Bytes=219141)
    4 3 VIEW OF 'VW_NSO_1' (Cost=8 Card=16 Bytes=272)
    5 4 SORT (UNIQUE) (Cost=8 Card=16 Bytes=960)
    6 5 HASH JOIN (Cost=4 Card=16 Bytes=960)
    7 6 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS' (Cost=1 Card=2 Bytes=60)
    8 7 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE) (Cost=2 Card=2)
    9 6 TABLE ACCESS (FULL) OF 'MIS_LIST_VALUES' (Cost=2 Card=817 Bytes=24510)
    10 3 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451 Card=35116 Bytes=772552)
    11 2 TABLE ACCESS (FULL) OF 'TRADE_INDEX' (Cost=2056 Card=286928 Bytes=5164704)
    SQL> select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB';
    VALUE
    B49540
    B60010
    Elapsed: 00:00:00.00
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 NESTED LOOPS
    2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS'
    3 2 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE)
    4 1 INDEX (RANGE SCAN) OF 'MISLV_PK' (UNIQUE)
    SQL> select count(*) from trade_index ti, counterparty_xref cx
    2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
    3 and cx.XREF_VALUE in ('B49540','B60010')
    4 and cx.XREF_TYPE = 'Client RXM'
    5 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
    6 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
    COUNT(*)
    6195
    Elapsed: 00:00:02.06
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=532 Card=1 Bytes=40)
    1 0 SORT (AGGREGATE)
    2 1 NESTED LOOPS (Cost=532 Card=41 Bytes=1640)
    3 2 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451 Card=1 Bytes=22)
    4 2 TABLE ACCESS (BY INDEX ROWID) OF 'TRADE_INDEX' (Cost=81 Card=286928 Bytes=5164704)
    5 4 INDEX (RANGE SCAN) OF 'CPY_SID_IDX' (NON-UNIQUE) (Cost=2 Card=286928)
    What I dont understand is that:
    cx.XREF_VALUE in (select ml.VALUE from v_mis_lists ml where ml.LIST_NAME = 'Deutsche Bank DB')
    and
    cx.XREF_VALUE in ('B49540','B60010')
    are essentially the same thing
    (ie the subquery returns the values I have used)
    but the optimizer uses a totally different (slower) method to execute the rest of the former query?
    Many thanks
    Ian

    Hi,
    Here is with join and not using the view: still have full table scan of large table trade_index
    SQL> select count(*) from mis_lists ml, mis_list_values mlv, counterparty_xref cx, trade_index ti
    2 where ti.CPY_SID = cx.CPY_SID and ti.TRADE_OR_ARCHIVE = 'A'
    3 and cx.XREF_VALUE = mlv.VALUE
    4 and mlv.MISL_SID = ml.MISL_SID
    5 and ml.LIST_NAME = 'Deutsche Bank DB'
    6 and cx.XREF_TYPE = 'Client RXM'
    7 and ti.START_DATE < to_date('01/01/2004','DD/MM/YYYY')
    8 and (ti.END_DATE >= to_date('01/01/2003','DD/MM/YYYY') or ti.END_DATE = '01-Jan-1900') ;
    COUNT(*)
    6195
    Elapsed: 00:00:12.08
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2602 Card=1 Bytes=10
    0)
    1 0 SORT (AGGREGATE)
    2 1 HASH JOIN (Cost=2602 Card=26581 Bytes=2658100)
    3 2 HASH JOIN (Cost=456 Card=5619 Bytes=460758)
    4 3 HASH JOIN (Cost=4 Card=16 Bytes=960)
    5 4 TABLE ACCESS (BY INDEX ROWID) OF 'MIS_LISTS' (Cost1 Card=2 Bytes=60)
    6 5 INDEX (RANGE SCAN) OF 'MISL_NAT_UK' (UNIQUE) (Cost=2 Card=2)
    7 4 TABLE ACCESS (FULL) OF 'MIS_LIST_VALUES' (Cost=2 Card=817 Bytes=24510)
    8 3 TABLE ACCESS (FULL) OF 'COUNTERPARTY_XREF' (Cost=451Card=35116 Bytes=772552)
    9 2 TABLE ACCESS (FULL) OF 'TRADE_INDEX' (Cost=2056 Card=286928 Bytes=5164704)
    Statistics
    0 recursive calls
    47 db block gets
    16519 consistent gets
    4981 physical reads
    0 redo size
    205 bytes sent via SQL*Net to client
    622 bytes received via SQL*Net from client
    3 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    1 rows processed
    Thanks for you help,
    Ian

  • Plan hash value for two queries!

    Hi,
    DB : Oracle 11g (11.2.0.3.0)
    OS: RHEL 5
    I have two question:
    1. Can two queries have same plan hash value? I mean I have below two queries:
    SELECT /+ NO_MERGE */ MIN(payor.next_review_date)*
    * FROM payor*
    * WHERE payor.review_complete = 0*
    * AND payor.closing_date IS NULL*
    * AND payor.patient_key = 10;*
    and
    SELECT  MIN(payor.next_review_date)
    * FROM payor*
    * WHERE payor.review_complete = 0*
    * AND payor.closing_date IS NULL*
    * AND payor.patient_key = 10;*
    When I tried to review the execution plan for both queries, the plan hash value remain same. Does it mean that execution plan for both queries are same? If yes, then how Oracle understands or changes the execution plan based on hint. If no then what plan hash value represents?
    2. If the execution plan with hint and without hint is same except for a given query except no.of rows and bytes. Does it mean that query with less rows and bytes scanned is better?
    Thanks in advance
    -Onkar

    Hi,
    there are two different things. One is EXPLAIN PLAN, which is how the optimizer thinks the query will be executed. It contains some estimates of cost, cardinalities etc. There is also EXECUTION PLAN. It also contains all this information regarding the optimizer estimates, but on the top of that, it also contains information about actual I/O incurred, actual cardinalities, actual timings etc.
    So if a hint is changing optimizer estimates, but the plan stays the same, then impact on query's performance is zero.
    If the actual numbers are changing, this is probably also irrelevant to the hint (e.g. you can have less physical reads because more blocks are found in the buffer cache the second time you're running the query, or you less work because you don't have to parse the statement etc.).
    Actually, most of optimizer hints don't affect optimizer estimates; rather, they try to get the optimizer to use a certain access method or a certain join order etc. regardless of the cost. So you must be talking about such hints as cardinality, dynamic_sampling etc. If that's not the case -- please clarify, because this means that something wrong is going on here (e.g. an INDEX hint may work or it may fail to work, but if it fails, optimizer estimates shouldn't change).
    Best regards,
    Nikolay

Maybe you are looking for

  • Basic vs Advanced Compression in Oracle 11g

    Hi, We are going to install Oracle 11gR2 in one new database server. Since the database will be used for Data Warehouse purposes and our company has rejected to pay licenses for "Oracle Advanced Compression", I wanted to know which options of "Basic

  • The keys 5tgb are not working

    the keys 5tg and b do not always work. Ideas?

  • Asian double-byte characters

    Hello, When importing English data into the CRM field lengths are defined for example "Name" = 100 chartacters. When im importing some Asian languages these are defined as double byte. Can anyone confirm if this has any effects on the field lenghts i

  • Real instrument Troubles (built in mic wont turn off...kinda)

    Hey guys, I'm new to this whole GB3 biz, but I've used CoolEdit Pro alot in the past. Anyway, in CEP, I was able to connect my electric guitar directly into my microphone jack using an adabter, and it would pick it up just fine. But Im not having so

  • HP Printer Photosmart C4180 All in One

    HP Photosmart C4180 All in One Printer. I did reinstall the printer using the original disk, did uninstall  it and then reinstall it, but did not work right. Then I try to download the driver direct from HP and I get the same result (The printer work