Unexpected result with select max(colmn) in anonymous block

Hi
The following query gives me 28,800 as sum(sal)
SELECT SUM(salary)
FROM employees
    WHERE department_id =60
O/P is :  28800But when i use the above query in anonymous block it gives me 684400
DECLARE
v_sum_sal NUMBER;
department_id employees.department_id%TYPE := 60;
BEGIN
    dbms_output.put_line ('The department_id is'||department_id);
   SELECT SUM(salary)
      into v_sum_sal
    FROM employees
    WHERE department_id = department_id ;
   dbms_output.put_line ('The sum of sal is'||v_sum_sal);
END;The above output statements gives me 684400 as output.. But the expected is 28800
Could you please hint me why the output differs in anonymous block

Smile wrote:
Hi
The following query gives me 28,800 as sum(sal)
SELECT SUM(salary)
FROM employees
WHERE department_id =60
O/P is :  28800But when i use the above query in anonymous block it gives me 684400
DECLARE
v_sum_sal NUMBER;
department_id employees.department_id%TYPE := 60;
BEGIN
dbms_output.put_line ('The department_id is'||department_id);
SELECT SUM(salary)
into v_sum_sal
FROM employees
WHERE department_id = department_id ;
dbms_output.put_line ('The sum of sal is'||v_sum_sal);
END;The above output statements gives me 684400 as output.. But the expected is 28800
Could you please hint me why the output differs in anonymous blockBecause you've made the mistake of having the same name for your column and your variable.
department_id is taken to be the column on both sides of the equality in your query - so it's picking up all rows.

Similar Messages

  • Unexpected Result with Application Process

    Hi Everyone,
    I have an application process which is producing unexpected results. The code is below. What does is, updates one table, then based on that update inserts some data into a different table. The process workes fine. However, when the table gets updated it updates by 2 when it should only update by 1. Does anyone see where i am going wrong?
    Its really anoying lol
    Thanks in Advance
    -N.S.N.O.
    Code*
    BEGIN
    update pzrckt01 set CLK_NEXT_NR = CLK_NEXT_NR+1 where CLK_PARTITION_ID='QWC';
    htp.p('Start debugging');
    htp.p('QWC_KEY_ID : ' ||wwv_flow.g_x02);
    htp.p('SYS_ID : ' ||wwv_flow.g_x03);
    htp.p('QWC_DELETE_CD: ' ||wwv_flow.g_x04);
    htp.p('QWC_CUS_ID : ' ||wwv_flow.g_x05);
    htp.p('COU_ID_QWC : ' ||wwv_flow.g_x06);
    insert into PZRCUT01 (QWC_MYTNT_ID,QWC_KEY_ID,SYS_ID,QWC_DELETE_CD,QWC_ADD_TD,QWC_ADD_USER_ID,QWC_UPDT_TD,QWC_UPDT_USER_ID,QWC_SOFTLOCK_TS,QWC_CUS_ID,COU_ID_QWC) values ((select clk_next_nr from pzrckt01 where CLK_PARTITION_ID ='QWC'),wwv_flow.g_x02,wwv_flow.g_x03,wwv_flow.g_x04,(select sysdate from dual),(select :APP_USER from dual),(select sysdate from dual),(select :APP_USER from dual),(select systimestamp from dual),wwv_flow.g_x05,wwv_flow.g_x06);
    EXCEPTION WHEN OTHERS THEN
    htp.p('Exception');
    htp.p(SQLERRM);
    END;

    Hey,
    There are no triggers on these tables im working with. A way to get around waht im tryin to do is have 2 application processes, one for the update and one for the insert. but to do this i would need them to be fired off using the same button.
    for example in my HTML Header having a function within a function.
    function pzrcut01Insert(){
    function pzrckt01Update(){
    var ajaxRequest = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=update_pzrckt01',null);
    var ajaxRequest1 = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=PZRCUT01_INSERT',null);
    ajaxRequest1.addParam('x02', $v('P6_QWC_KEY_ID'));
    ajaxRequest1.addParam('x03', $v('P6_SYS_ID'));
    ajaxRequest1.addParam('x04', $v('P6_QWC_DELETE_CD'));
    ajaxRequest1.addParam('x05', $v('P6_QWC_CUS_ID'));
    ajaxRequest1.addParam('x06', $v('P6_COU_ID_QWC'));
    var Answer = confirm('Are you sure you want to update?');
    if (Answer) {
    gReturn=ajaxRequest1.get();
    if (gReturn) {
    alert(gReturn);
    ajaxRequest1.get();
    alert('Attempted Insert Processed');
    }else{
    alert('Attempted Insert Cancelled');
    would this be possible?
    -N.S.N.O.

  • Problem with select max(FIELD)

    I have a table with the following fields:
    PARTID     ART_TYP     AEC     SERVICE     PLANT     STATUS
    4711     A     A     2     P     230
    4711     A     B     0     P     230
    4712     A     B     2     P     230
    4713     A     B     0     P     230
    I need a sqlscript where I get the records with MAX(SERVICE)
    In this example
    4711     A     A     2     P     230
    4712     A     B     2     P     230
    4713     A     B     0     P     230
    I tried this with:
    select distinct partid,
         art_typ,
         aec,
         max(service),
         plant,
         status
    from t_msltmp
    group by partid,art_typ,aec,plant,status
    But I get both records of partid=4711
    Can someone help me with this problem
    kind regards
    Menk Slot

    ROW_NUMBER() requires explicit ORDER BY clause in OVER:
    http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/functions105a.htm#86312
    SQL> select partid, art_typ, aec, mx, plant, status
      2  from(select partid,
      3                art_typ,
      4                aec,
      5                max(service) over (partition by partid) mx,
      6                row_number() over (partition by partid order by service desc) rn
      7                plant,
      8                status
      9         from t_t)
    10  where rn = 1
    11  /
        PARTID A A         MX P     STATUS
          4711 A A          2 P        230
          4712 A B          2 P        230
          4713 A B          0 P        230Rgds.
    P.S.
    Another way to get this (more tedoius):
    SQL> select partid,
      2  max(art_typ) keep (dense_rank first order by service desc) art_typ,
      3  max(aec) keep (dense_rank first order by service desc) aec,
      4  max(service) service,
      5  max(plant) keep (dense_rank first order by service desc) plant,
      6  max(status) keep (dense_rank first order by service desc) status
      7  from t_t
      8  group by partid
      9  /
        PARTID A A    SERVICE P     STATUS
          4711 A A          2 P        230
          4712 A B          2 P        230
          4713 A B          0 P        230Message was edited by:
    dnikiforov

  • Materialized View hangs with SELECT MAX

    Hi there,
    I'm using Oracle 10.2.0.4 on a 64bit AIX system and I am having issues with creating a materialized view.
    We have a large (1Tb) database and the large table the materialized view looks at is 200m rows.
    I've created 5 other materialized views each with a select max clause and all looking at the same table.
    When I created my problem MV I forget the select max and it created in 22mins.
    I corrected my error by putting in the select max clause (so as to retrieve the top record) and the create MV ran for 16hrs+, I killed it.
    If I just run the select statement at a sqlplus prompt it runs through in 22mins, if I create another object e.g. a table from the query it creates in 22mins.
    So the question would be, why can I not create a MV using SELECT MAX on 10.2.0.4?
    If I've missed any details don't hesitate to ask.
    Thanks in advance.

    Hi Justin,
    Thank you for your reply.
    It has been upgraded to 9.2.0.8.0 from 9.1.... I'm not aware about the procedure used.
    I could see a job scheduled for the materialized view , but that fails and it's broken after 16 attempts.
    How to log the error generated by the Refresh Job?
    Recreating the View - After the upgrade I have created the Materialized view again. Object T1 exists in the user schema User1.
    I'm not explicitly getting any error but the refresh doesn't happens , so I couldn't find any records in the materialized view.
    Thanks
    GM

  • Can select resource content in anonymous block, but not in stored procedure

    I know the problem relates to the ACLs but am curious about the following behaviour:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE 10.2.0.1.0 Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    running on Windows XP sp2
    Trying to access an existing resource '/home/test/test.dtd'
    /home is owned by SYS
    /home/test is owned by me
    /home/test.test.dtd is owned by me
    The following SQL and anonymous block retrieve the content of the resource:
    select extract(p.res,'/*').getClobVal()
    from resource_view p
    where p.any_path = '/home/test/test.dtd'
    declare
    alldata clob;
    begin
    select extract(p.res,'/*').getClobVal()
    into alldata
    from resource_view p
    where p.any_path = '/home/test/test.dtd'
    dbms_output.put_line(substr(alldata, 1, 255));
    end;
    BUT when I encapsulate the anonymous block in a stored procedure, I am told that the folder '/home/test/' does not exist.
    As said, the problem related to the ACL on the folders, what I cannot understand, and caused me considerable confusion, is why I was unable to get the content from a stored procedure when I was able to do so from an anonymous block.
    regards
    Richard

    Use equals_path(res,'/home/test/test.dtd') = 1OK, OK, but how do you use wildcards (if you want to) since any_path like '/home/test%' works?
    Who was the stored procedure owned by. Was it a stand-alone stored procedure or a
    method on a package. If it was a method on a package is it AUTHID creator or AUTHID
    definer.Everything apart from the '/home' folder is owned by the current user.
    The PL/SQL was in a packaged procedure, a stand-alone stored procedure, and an anonymous block. It exhibited the same behaviour in each. All were defined with default AUTHID which is definers rights which is the same as the current user in this case.

  • Problem with Select MAX( field ) in OO

    Hi all,
    I have this statement:
        SELECT SINGLE RUECK MAX( RMZHL ) BUDAT STZHL
          INTO K_CONFIRM
            FROM AFRU
              WHERE AUFNR EQ P_ORDER
                AND VORNR EQ P_OPER
                  GROUP BY RUECK RMZHL BUDAT STZHL.
    My requirement is I need to use a work area (not an internal table) and ENDSELECT is not allowed. The result is not selecting the Maximum RMZHL, it will always return the first record.
    How can I correctly do this in OO? Thanks in advanc.e

    If you use GROUP BY in your query then the set functions (MAX,SUM, Count) not possible...
    refer:
    http://help.sap.com/saphelp_nw70/helpdata/en/be/c7fe3f70cac342e10000000a1550b0/frameset.htm

  • Unexpected result with RELATE

    The following query returns the distance between 2 points as 144.120474 meters:
    select sdo_geom.sdo_distance(
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.688771, -23.625872, NULL), NULL, NULL),
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.68777, -23.62679, NULL), NULL, NULL),
    1) Distance
    from dual;
    DISTANCE
    144.120474
    If I use WITHIN_DISTANCE for a distance of 100 meters it returns FALSE, what is correct:
    select sdo_geom.within_distance(
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.688771, -23.625872,NULL), NULL, NULL),
    100,
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.68777, -23.62679, NULL), NULL, NULL),
    1) Within_Distance
    from dual;
    WITHIN_DISTANCE
    FALSE
    but, If I use RELATE with ANYINTERACT or DETERMINE masks and the tolerance of 100 meters, the results are 'TRUE' and 'EQUAL':
    select sdo_geom.relate(
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.688771, -23.625872,NULL), NULL, NULL),
    'anyinteract',
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.68777, -23.62679, NULL), NULL, NULL),
    100) AnyInteract
    from dual;
    ANYINTERACT
    TRUE
    select sdo_geom.relate(
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.688771, -23.625872,NULL), NULL, NULL),
    'determine',
    MDSYS.SDO_GEOMETRY(2001, 8307, MDSYS.SDO_POINT_TYPE(-46.68777, -23.62679, NULL), NULL, NULL),
    100) Determine
    from dual;
    DETERMINE
    EQUAL
    Is this the correct behavior for SDO_GEOM.RELATE? If the calculated distance is 144.120474 meters (1st query) I thought SDO_RELATE with a tolerance of 100 meters should return FALSE for ANYINTERACT and DISJOINT for DETERMINE.

    Hi Ivan,
    You are right, in most versions of spatial the tolerance is how far apart things are in the x or y dimension, but not the true distance.
    I noticed the same thing a while ago, and entered bug 3458372, which was then subsumed into another bug, 3630522. I believe this is fixed in 9.2.0.6, and in 10.1.0.3 and higher.
    From looking at all of the information, there is probably also a patch to apply on top of 9.2.0.5 (I saw patches for solaris, linux, and hp_ux).
    Hope this helps,
    dan

  • Currency Translation. Unexpected result with currency transl indicator 1

    Dear Experts,
    Durring currency translation we get the following result:
    Company  ConsProfitC  PostL     Item         MovType  TransIndLocCur  ValueTransCur       ValueLocCur           ValueGroupCur
    C0803     DUMMY        00          100001     600              USD            100.000,00-            100.000,00-             75.930,14-
    C0803     DUMMY        00     100001     600     1       USD            0,00                         0,00                       1.586,17-
    The first line shows the value translated by accumulated currency translation key, closing rate which is equal 1,317. The second line with the currency translation indicator 1 in completely unexpected.
    Do you have any ideas why we get the second line?
    Thanks.
    Sorry, I've already found the solution. Currency translation indicator 1 is the translation delta between specific value and reference value.
    Edited by: Barrie Roche on Sep 2, 2008 10:45 PM

    The translation indicator 1 is for translation where the translation rate type is different from the reference rate type. Could this item be in the translation method twice with different rate types?

  • Can someone explain me that unexpected result with dbms_random ?

    Hi There,
    I wonder how executing dbms_random only once could return two different values?
    select x,x from (select dbms_random.value(0,100) x from dual)
             X          X
    62,6555943 31,0639443Thanks a lot for your feedback
    Laurent

    The optimizer the merged view into main query and called the function twice (whether or not it should be allowed to do this with non-deterministic function is another matter). Compare explain plan and results below with those with NO_MERGE or ROWNUM to prevent merging of the view.
    Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.5.0 - Production
    SQL> SET AUTOTRACE ON EXPLAIN
    SQL> /* Original */
    SQL> SELECT x, x
      2  FROM (
      3    SELECT DBMS_RANDOM.VALUE (0, 100) x
      4    FROM   dual);
             X          X
    89.1805061 93.2011152
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   TABLE ACCESS (FULL) OF 'DUAL'
    SQL> /* With NO_MERGE hint in view */
    SQL> SELECT x, x
      2  FROM (
      3    SELECT /*+ NO_MERGE */ DBMS_RANDOM.VALUE (0, 100) x
      4    FROM   dual);
             X          X
    39.9149281 39.9149281
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=17 Card=8168 Bytes=1
              06184)
       1    0   VIEW (Cost=17 Card=8168 Bytes=106184)
       2    1     TABLE ACCESS (FULL) OF 'DUAL' (Cost=17 Card=8168)
    SQL> /* With ROWNUM in view */ 
    SQL> SELECT x, x
      2  FROM (
      3    SELECT DBMS_RANDOM.VALUE (0, 100) x, ROWNUM
      4    FROM   dual);
             X          X
    35.2401179 35.2401179
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   VIEW
       2    1     COUNT
       3    2       TABLE ACCESS (FULL) OF 'DUAL'
    SQL>

  • Is there an easier way to get tabular report results with select lists?

    Ok, as a newbie, I have gotten this query to work in my report region but I figure if I am doing something this convoluted then I am not letting a wonderful feature of HTML DB do its job.
    I have 3 select lists that I want to "AND" together to filter the result list. Two of the selected items to filter could have null values.
    I want to have the entire results to appear when the list is reset (clear cache is working and sets each select list value to null). However, it appears to me, if one select list is used and the others are not, the other select lists by default have %null% (Null return value setting left blank) which isn't really null.
    Well if that wasn't confusing so far. Here is the select:
    select [my columns]
    from my_table
    where (instr(upper("COL1"),upper(
    decode(:P1_FILTER_COL1,'%null%',"COL1",
    null,"COL1",:P1_FILTER_COL1))) > 0 -- COL1 is mandatory
    AND
    (instr(upper("COL2"),upper(
    decode(:P1_FILTER_COL2,'%null%',"COL2",
    null,"COL2",:P1_FILTER_COL2))) > 0 OR "COL2" IS NULL)
    AND
    (instr(upper("COL3"),upper(
    decode(:P1_FILTER_COL3,'%null%',"COL3",
    null,"COL3",:P1_FILTER_COL3))) = 1 OR "COL3" IS NULL)
    Do I have to use decode for the select values being reset, selected/not selected? Is there some way for the Null return value setting to really be null not %null%?
    Thanks,
    Kelly

    Rather than creating your report directly from a SQL statement use the "Sql Query (PL/SQL Function Body returning Sql Query)" option and re-write your code in PL/SQL. PL/SQL gives you greater control and allows you to test the "null" values and add where clauses to your SQL query based on whether or not a value was selected in the SELECT list.
    For example, I have a report parameter screen that has a state drop down list that returns a value of -1 if no state is selected. I can, therefore, code:
    IF :P6_STATE_CODE != -1 THEN
    query := query + " WHERE state_code = ' ||
    :P6_STATE_CODE ;
    END IF ;
    I have a more complete example if you would like to send me an email ([email protected])
    Jeff

  • Create Sequence Number with Select Query

    Hi All,
    I would like to create a sequence number in oracle but instead of hard coding the "start with" I want to select the max value of the primary key of a table and add 1 and use this instead:
    So what I want is:
    CREATE SEQUENCE crg_mrec_seq
    MINVALUE 1
    MAXVALUE 999999999999999999999999999
    START WITH select max(primarykey)+1 from table1
    INCREMENT BY 1
    CACHE 20;I'm guessing I need to pass this max value as a variable into the create sequence number but I'm not sure what syntax to use.
    Thanks,
    Ed

    spalato76 wrote:
    Hi All,
    I would like to create a sequence number in oracle but instead of hard coding the "start with" I want to select the max value of the primary key of a table and add 1 and use this instead:
    So what I want is:
    CREATE SEQUENCE crg_mrec_seq
    MINVALUE 1
    MAXVALUE 999999999999999999999999999
    START WITH select max(primarykey)+1 from table1
    INCREMENT BY 1
    CACHE 20;I'm guessing I need to pass this max value as a variable into the create sequence number but I'm not sure what syntax to use.
    Thanks,
    Edconstruct SQL statement & then EXECUTE IMMEDIATE

  • Select Max and ResultSet Problem With Access

    The following code is producing a 'null pointer excepetion' error and I know why it is occurring I just do not know how to fix the problem.
    Basically I want to automitically generate a unique ID that is one number higher than the max ID (data is stored within an Access database). The ID field is made up of first and last initial taken from parsing previous login info ('JS-01', 'JS-02', ect.). If there are no IDs in the database that match your login info I want to set the new ID equal to 'JS-01' if your login is 'James Smith' for example.
    The problem is explained within the code below.
    <%
    //define resultset and statement
    ResultSet rss=null;
    ResultSet rs=null;
    Statement stmt=null;
    //HERE IS WHERE YOU PARSE THE LOGIN INFO
    String finitial = (String)session.getAttribute("vfirst");//vfirst=JIM
    String linitial = (String)session.getAttribute("vlast");//vlast=SMITH
    char f = finitial.charAt(0);
    char n = linitial.charAt(0);
    String sID = f+""+n;//NOW sID CONTAINS 'JS'
    try {
    //Using the current database connection create a statement
    stmt=con.createStatement();
    //QUERY TO SELECT MAX ID
    //NOTE: CURRENTLY THERE ARE NO IDs LIKE 'JS' IN THE DATABASE !!!!!!
    String sql="SELECT Max(ID) As MaxID FROM tblError Where ID LIKE '%"+sID+"%'" ;
    rs = stmt.executeQuery(sql);
    String newID;
    //HERE THE RESULT SET SHOULD BE NULL BUT IT IS NOT. I KNOW THIS BECAUSE WHEN I REPLACE String iID WITH A LITERAL LIKE 'JS-03' THE LOGIC WILL EXECUTE CORRECTLY AND GIVE ME 'JS-04'. IF I LEAVE THE CODE LIKE IT IS THEN I GET THE NULL POINTER VALUE ERROR BECAUSE YOU CANNOT RESOLVE "MaxID" WHEN THE RESULT SET IS NULL. IF THE RESULT SET IS NULL IT SHOULD NOT EVEN EXECUTE ANY OF THIS CODE WITHIN THE 'if' STATEMENT, BUT IT IS. SO BASICALLY JSP IS LEAVING ME WITH A MAJOR OXYMORON AND I WOULD APPRECIATE ANY ADVICE THAT WOULD HELP ME SOLVE THIS PROBLEM.
    if(rs.next()){
    String iID = rs.getString("MaxID");
    String letters = iID.substring(0,3);
    int numbers = Integer.parseInt(iID.substring(3,5));
    numbers = numbers + 1;
    if(numbers < 10){
    newID = letters + "0" + numbers;}
    else{
    newID = letters + numbers;
    else{//IF THERE IS NO RESULT SET THAN THE ID SHOULD BE 'JS-01'
    newID = sID + "-01";
    %>
    Because this an Access database I cannot use any null exception functions such as NVL or COALESCE which are specific to Oracle and SQL Server I beleive.

    The max() will return a result set, even if the max value is null.
    You should check to see if iID is null.
    if(rs.next())
       String iID = rs.getString("MaxID");
       if (iID == null)
           newID = sID + "-01";
       else
          String letters = iID.substring(0,3);
          ... etc ...
    }

  • Anonymous Block with Variable and SELECT

    Hi Guys,
    I am struggling to connect the material I am learning. I read about anonymous blocks and variables so I want to write a
    nice and neat select with all the fancy stuff (variables, exceptions) to get it right and to it as I read in the book.
    DECLARE
    MYSTRING VARCHAR(10);
    MYSTIRNG := 'X';     
    BEGIN
    SELECT * FROM DUAL WHERE DUMMY = MYSTRING
    END;
    but....this causes one error after the other. Am I getting the concept wrong here or is it a syntax error?

    cant I just get the result without using variable to store the result in the first place???Perhaps you're better off using a REF CURSOR (a.k.a. cursor variable).
    That way you'll pass a resultset to the caller and you don't need any variables to select into.
    You can read about them here:
    http://www.oracle-base.com/articles/misc/UsingRefCursorsToReturnRecordsets.php
    Re: PL/SQL 101 : Understanding Ref Cursors
    http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10766/tdddg_subprograms.htm#TDDDG99939

  • Select max date from a table with multiple records

    I need help writing an SQL to select max date from a table with multiple records.
    Here's the scenario. There are multiple SA_IDs repeated with various EFFDT (dates). I want to retrieve the most recent effective date so that the SA_ID is unique. Looks simple, but I can't figure this out. Please help.
    SA_ID CHAR_TYPE_CD EFFDT CHAR_VAL
    0000651005 BASE 15-AUG-07 YES
    0000651005 BASE 13-NOV-09 NO
    0010973671 BASE 20-MAR-08 YES
    0010973671 BASE 18-JUN-10 NO

    Hi,
    Welcome to the forum!
    Whenever you have a question, post a little sample data in a form that people can use to re-create the problem and test their ideas.
    For example:
    CREATE TABLE     table_x
    (     sa_id          NUMBER (10)
    ,     char_type     VARCHAR2 (10)
    ,     effdt          DATE
    ,     char_val     VARCHAR2 (10)
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0000651005, 'BASE',    TO_DATE ('15-AUG-2007', 'DD-MON-YYYY'), 'YES');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0000651005, 'BASE',    TO_DATE ('13-NOV-2009', 'DD-MON-YYYY'), 'NO');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0010973671, 'BASE',    TO_DATE ('20-MAR-2008', 'DD-MON-YYYY'), 'YES');
    INSERT INTO table_x (sa_id,  char_type, effdt,                          char_val)
         VALUES     (0010973671, 'BASE',    TO_DATE ('18-JUN-2010', 'DD-MON-YYYY'), 'NO');
    COMMIT;Also, post the results that you want from that data. I'm not certain, but I think you want these results:
    `    SA_ID LAST_EFFD
        651005 13-NOV-09
      10973671 18-JUN-10That is, the latest effdt for each distinct sa_id.
    Here's how to get those results:
    SELECT    sa_id
    ,         MAX (effdt)    AS last_effdt
    FROM      table_x
    GROUP BY  sa_id
    ;

  • Selecting the rows with the max rank

    Using Siebel CRM and tested conditions in Answers - It was observed that one email ID is linked to multiple customers. For an email campaign we need to select only one customer per email basically had to remove all the duplicate email addresses from the list as customer IDs are anyways unique.
    Tried using the RANK function - RANK(customer ID by Email) = 1 it gave me unique email addresses and customer IDs but the problem is that the RANK 1 is given to the latest customer added (customer IDs are numeric)
    I need a solution to select all the rows with the max rank / all the customers that were added first for all the emails

    Don't know what you want to achieve but here is way to do this:
    SQL> select * from test;
    NO
    1
    2
    3
    4
    5
    SQL> select * from test union all select * from test;
    NO
    1
    2
    3
    4
    5
    1
    2
    3
    4
    5
    10 rows selected.
    Daljit Singh

Maybe you are looking for

  • Apple ID management for the family

    My family has been using one apple id for years, but we've outgrown it. how can I turn my one apple id into multiple id's? What I'd like to do is separate my daughter's music, and create accounts for them, and then have accounts for my wife and I. Is

  • The stubborn standby redo log

    I publish this question here, Due to the similarity between the technologies, and I know for you this is very familiar. I have a dowstreams which has four standbys redo log groups. One of these groups is still in a redo of the day 23/04/2010. This ol

  • Live Type "effects" not present

    I've periodically loaded and unloaded the (2) Live type Media discs when my laptop has been low on space. Never had the problem about to describe before. I have my hard drive partioned into two drives: The hard drive and a Videostore (where I keep my

  • How to iterate through files in a directory in real time?

    I have a program where users can write RPN equations into txt files so that the number and type of equations are completely customizable at run time.  I need to be able to iterate through .txt files in an Equations folder to load the equations into m

  • Chrome failing when switching users

    This also happens when powering down. Chrome widows shrink and I canot open them. Cheers SteveW