Select max problem

Hi experts
I have this long query:
SELECT DISTINCT MAX( vbka~ktabg )    "add
         mseg~mblnr
         mseg~mjahr
         mseg~ebeln
         mseg~lifnr
         mseg~matnr
         mseg~grund
         mseg~erfmg
         mseg~erfme
         mseg~shkzg
         knvv~kvgr1
         tvv1t~bezei
         lfa1~name1
         lfa1~stras
         lfa1~ort01
         lfa1~telf1
         lfa1~kunnr
         eipa~preis
         eipa~bwaer
         eipa~peinh
         eipa~bprme
    FROM mseg JOIN mkpf  ON msegmblnr = mkpfmblnr
                        AND msegmjahr = mkpfmjahr
              JOIN ekpo  ON msegebeln = ekpoebeln
              JOIN eine  ON ekpoinfnr = eineinfnr
              JOIN eipa  ON eineebeln = eipaebeln
                        AND eineebelp = eipaebelp
              JOIN lfa1  ON mseglifnr = lfa1lifnr
              JOIN knvv  ON lfa1kunnr = knvvkunnr
              JOIN tvv1t ON knvvkvgr1 = tvv1tkvgr1
             JOIN vbka ON lfa1kunnr = vbkakunnr     "add
    into CORRESPONDING FIELDS OF TABLE i_data1
    WHERE mseg~lifnr IN r_lifnr
    AND   mseg~matnr = 'B1992'
    AND  ( msegbwart = '101' or msegbwart = '102' )
    AND  mseg~grund IN r_grund
    AND  mkpf~budat IN r_budat
    AND tvv1t~spras = 'B'
    AND eine~esokz  = '0'
    and vbka~ktaar = '0010'   "add
    group by
         mseg~mblnr
         mseg~mjahr
         mseg~ebeln
         mseg~lifnr
         mseg~matnr
         mseg~grund
         mseg~erfmg
         mseg~erfme
         mseg~shkzg
         knvv~kvgr1
         tvv1t~bezei
         lfa1~name1
         lfa1~stras
         lfa1~ort01
         lfa1~telf1
         lfa1~kunnr
         eipa~preis
         eipa~bwaer
         eipa~peinh
         eipa~bprme.
I get results, but in the field vbka~ktabg  i get 00000000 although i know that there is values there.
What's wrong here, and how can i do this right.
Thanks
Amit

You don't need to use DISTINCT and GROUP BY and this may be what is causing the problem.  Try with GROUP BY only.

Similar Messages

  • ResultSet Select MAX() Problem

    Im using mySQL and when i write the following query :
    SELECT MAX(attribute) FROM table;
    and theres no records in the table i cant know what it returns ... my problem is i want to get the max() number
    <code>
    resultSet = connectivity.statement.executeQuery("SELECT MAX(number) FROM urls");
    resultSet.next();
    if(resultSet.getObject(1) != null)
    .... Do certain things
    else
    ....Do other
    </code>
    an SQLexcecption is thrown from this line :
    if(resultSet.getObject(1) != null)
    so how can i solve it ??!

    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3908)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5699)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410)
    at indexer.Indexer.insertNewRecords(Indexer.java:80)
    at indexer.Indexer.startIndexing(Indexer.java:31)
    at jranker1.JRanker.writeToDB(JRanker.java:187)
    Its an SQL Exception ...

  • 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 ...
    }

  • SQL query problem - select max (case... aggregate function)

    Hi,
    I have a problem with below sql query, it gives me problem/error message 'ORA-00937: not a single-group group function', why?
    select sag.afdeling, sag.sagsnr, to_char(sag.start_dato, 'yyyy-mm-dd'), sag.stat, BOGF_TRANS.TRANSTYPE,
    max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
    max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
    max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
    from sag
    join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
    where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
    group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
    If I exclude (columns) as below it give me correct summations (max (case... sum(...)) but then I miss some important info that I need
    select
    max (case when BOGF_TRANS.TRANSTYPE = 'K' then sum(bogf_trans.belobdkk) end) + -- as "TRANSTYPE K",
    max (case when BOGF_TRANS.TRANSTYPE = 'D' then sum(bogf_trans.belobdkk) end) as "TRANSTYPE K & D",
    max (case when BOGF_TRANS.TRANSTYPE = 'S' then sum(bogf_trans.belobdkk) end) as "SUM TRANSTYPE S"
    from sag
    join bogf_trans on sag.selskab = bogf_trans.selskab and sag.sagsnr = bogf_trans.sagsnr and sag.afdeling = bogf_trans.afdeling
    where SAG.SELSKAB=37 and SAG.AFDELING = 'SUS' AND SAG.SAGSNR = 10876
    group by sag.afdeling, sag.sagsnr, sag.start_dato, sag.stat, BOGF_TRANS.TRANSTYPE
    Any ideas?

    Moved to more sutable forum, sorry.

  • Problem selecting MAX YEAR MAX MONTH MAX DATE in one query

    CREATE TABLE TEMP
    C_INVOICE_ID NUMBER(10),
    DIA NUMBER,
    MES NUMBER,
    ANO NUMBER,
    SOCIO_NEGOCIO NVARCHAR2(60) NOT NULL,
    PRODUCTO_NOM NVARCHAR2(60) NOT NULL,
    M_PRODUCT_ID NUMBER(10),
    CATEGORIA NVARCHAR2(60) NOT NULL,
    COSTO NUMBER
    INSERT INTO TEMP VALUES(10111,1,2,2010,'1585','ALURON 100MG X 30 TABLETAS',1530,15,1.15);
    INSERT INTO TEMP VALUES(1015,15,2,2010,'1520','ALURON 100MG X 30 TABLETAS',1530,15,2.15);
    INSERT INTO TEMP VALUES(5654,5,2,2010,'1520','AMARYL 2MG X 15 TABLETAS',1531,15,4.2);
    INSERT INTO TEMP VALUES(15321,4,6,2010,'1520','AMARYL 2MG X 15 TABLETAS',1531,15,4.8);
    INSERT INTO TEMP VALUES(13548,8,6,2010,'1585','AMARYL 2MG X 15 TABLETAS',1531,15,4.3);
    INSERT INTO TEMP VALUES(19456,31,4,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4);
    INSERT INTO TEMP VALUES(116544,8,8,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.8);
    INSERT INTO TEMP VALUES(132,2,3,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.1);
    INSERT INTO TEMP VALUES(168,15,1,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.9);
    INSERT INTO TEMP VALUES(4898,7,4,2010,'1220','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.8);
    INSERT INTO TEMP VALUES(15132,25,9,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.4);
    INSERT INTO TEMP VALUES(1684,18,8,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.3);
    INSERT INTO TEMP VALUES(14988,8,8,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,2.9);
    INSERT INTO TEMP VALUES(84941,8,9,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,4.1);
    INSERT INTO TEMP VALUES(1155,7,4,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,4.7);
    INSERT INTO TEMP VALUES(184,1,1,2010,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    INSERT INTO TEMP VALUES(48994,8,4,2010,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.7);
    INSERT INTO TEMP VALUES(1465465,9,5,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.8);
    INSERT INTO TEMP VALUES(16,18,6,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    INSERT INTO TEMP VALUES(894886,20,4,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    CREATE TABLE TEMP
    C_INVOICE_ID NUMBER(10),
    DIA NUMBER,
    MES NUMBER,
    ANO NUMBER,
    SOCIO_NEGOCIO NVARCHAR2(60) NOT NULL,
    PRODUCTO_NOM NVARCHAR2(60) NOT NULL,
    M_PRODUCT_ID NUMBER(10),
    CATEGORIA NVARCHAR2(60) NOT NULL,
    COSTO NUMBER
    INSERT INTO TEMP VALUES(10111,1,2,2010,'1585','ALURON 100MG X 30 TABLETAS',1530,15,1.15);
    INSERT INTO TEMP VALUES(1015,15,2,2010,'1520','ALURON 100MG X 30 TABLETAS',1530,15,2.15);
    INSERT INTO TEMP VALUES(5654,5,2,2010,'1520','AMARYL 2MG X 15 TABLETAS',1531,15,4.2);
    INSERT INTO TEMP VALUES(15321,4,6,2010,'1520','AMARYL 2MG X 15 TABLETAS',1531,15,4.8);
    INSERT INTO TEMP VALUES(13548,8,6,2010,'1585','AMARYL 2MG X 15 TABLETAS',1531,15,4.3);
    INSERT INTO TEMP VALUES(19456,31,4,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4);
    INSERT INTO TEMP VALUES(116544,8,8,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.8);
    INSERT INTO TEMP VALUES(132,2,3,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.1);
    INSERT INTO TEMP VALUES(168,15,1,2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.9);
    INSERT INTO TEMP VALUES(4898,7,4,2010,'1220','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.8);
    INSERT INTO TEMP VALUES(15132,25,9,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.4);
    INSERT INTO TEMP VALUES(1684,18,8,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.3);
    INSERT INTO TEMP VALUES(14988,8,8,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,2.9);
    INSERT INTO TEMP VALUES(84941,8,9,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,4.1);
    INSERT INTO TEMP VALUES(1155,7,4,2010,'1585','AMOXAL 250MG X 75ML SUSPENSION',1534,15,4.7);
    INSERT INTO TEMP VALUES(184,1,1,2010,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    INSERT INTO TEMP VALUES(48994,8,4,2010,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.7);
    INSERT INTO TEMP VALUES(1465465,9,5,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.8);
    INSERT INTO TEMP VALUES(16,18,6,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    INSERT INTO TEMP VALUES(894886,20,4,2009,'1520','AMOXAL 250MG X 75ML SUSPENSION',1534,15,3.9);
    I have this query so far,
    SELECT
    MAX (TEMP.COSTO) COSTO,
    TEMP.M_PRODUCT_ID,
    TEMP.ANO
    FROM TEMP TEMP
    INNER JOIN(
    SELECT MAX(ANO) ANO, M_PRODUCT_ID
    FROM TEMP I
    GROUP BY M_PRODUCT_ID
    ) LA ON LA.ANO = TEMP.ANO AND LA.M_PRODUCT_ID = TEMP.M_PRODUCT_ID
    GROUP BY
    TEMP.M_PRODUCT_ID,
    TEMP.ANO
    but my intention is not to have the max cost, my intention is to query in this order
    first i need to select distinct m_product_id then
    for every m_product_id i need to
    filter max (ano) then filter max (mes) then filter max (dia)
    I need the result set like this.
    C_INVOICE_ID DIA MES ANO SOCIO PRODUCTO_NOM M_PRODUCT_ID CATERGORIA COSTO
    1015 15 2 2010 1520 ALURON 100MG X 30 TABLETAS 1530 15 2.15
    5654 5 2 2010 1520 AMARYL 2MG X 15 TABLETAS 1531 15 4.2
    15132 25 9 2010 1585 AMOXAL 250MG X 75ML SUSPENSION 1534 15 3.4
    please i really apritiated any help, thanks pd:i'm using oracle 9i

    Hi,
    Thanks for posting the CREATE TABLE and INSERT statements. Why are there two tables, and two sets of data? Was that a mistake?
    Sorry, I don't uderstand the problem.
    For m_product_id=1531, for example, why do you want
    INSERT INTO TEMP VALUES(5654,     5, 2, 2010,'1520','AMARYL 2MG X 15 TABLETAS',1531,15,4.2);selected, and not
    INSERT INTO TEMP VALUES(116544,     8, 8, 2010,'1220','AMARYL 2MG X 15 TABLETAS',1531,15,4.8);Both rows have ano=2010, so don't you want the one with the higher mes (8 > 2)?
    I think what you'll want is a Top-N Query , something like this:
    WITH     got_r_num     AS
         SELECT     temp.*     -- or list columns you want
         ,     RANK () OVER ( PARTITION BY  m_product_id
                          ORDER BY          ano     DESC     NULLS FIRST
                          ,          mes     DESC     NULLS FIRST
                          ,          dia     DESC     NULLS FIRST
                        )          AS r_num
         FROM     temp
    --     WHERE     ...     -- any filtering goes here
    SELECT     *     -- or list all columns except r_num
    FROM     got_r_num
    WHERE     r_num     = 1
    ;What if there's a tie? For example, if you add another row to the sample data:
    INSERT INTO TEMP VALUES(9876,     15,2, 2010,'1520','ALURON 100MG X 30 TABLETAS',1530,15,2.15);The query above would include both c_invoice_id=9876 and 1015. If you want just one of those rows, add more expressions to the analytic ORDER BY clause and/or use ROW_NUMBER instead of RANK.

  • Select Max record for the report

    Post Author: ROMZILBER
    CA Forum: WebIntelligence Reporting
    Hello,
    Here is our problem we have history data on a table like
    Example
    Data:
    11111   bbb       12/31/2004
    11111   nnn       12/31/2005
    11111   ddd       12/31/2007
    On a report we only need to display the most current record (based on a date).
    11111   ddd       12/31/2007
    How can we do that, we have try to create subquery but it does not allowed to enter logic for the max date for the same id.  So we are kind of stuck, I am more then sure this would be a very common conditions specially for data warehousing.
    we are on Oracle 10g
    Thank you for all your help.

    Post Author: ROMZILBER
    CA Forum: WebIntelligence Reporting
    To create a view is not a problem, supporting it is a different story
    But we have find a solution, you were correct about the filter.  I just did not know you can put the whole subselect in a  filter
    but here is a logic for the filter if someone would come up with the same problem
    tablename.NNNN_PROOF_DT = (SELECT MAX(X.NNNN_PROOF_DT) FROM tablename X  WHERE tablename.NNNN_CLAIM_NUMBER = X.NNNN_CLAIM_NUMBER AND   tablename.NNNN_LINE_CD = X.NNNN_LINE_CD)
    please let me know if you have any questions.
    sorry I am new to this so please do not take any offense

  • Same select max is very slow in one program but fast in another

    Hi,
    I have a report that becomes very slow these few months. I used SQL trace for the report and found out its these codes that slow down the report:
    SELECT MAX( mkpf~budat )
                  FROM mkpf
        INNER JOIN mseg
                       ON mseg~mblnr = mkpf~mblnr AND mseg~mjahr = mkpf~mjahr
                    INTO posting_date
               WHERE mseg~werks  = w_matl-batch_reservations-werks
                     AND mseg~charg  = w_matl-batch_reservations-charg
                     AND mseg~bwart  IN ('261', 'Z61').
    The thing is these codes have been used in different system, DEV, QAS, and PRD. But only in PRD it is very slow, other systems are pretty fast.
    I even created a local copy of that report in PRD, with the same code. The local file runs fast and perfectly. But the original code is just slow.
    Just wondering if anybody met this problem too? any ideas??

    Hi Liu,
    Index creation is not a advisable solution.Please follow the existing indexes by adding Mandt field.
    Try like this
    SELECT MAX( mkpf~budat )
                  FROM mkpf
        INNER JOIN mseg
                       ON mseg~mblnr = mkpf~mblnr AND mseg~mjahr = mkpf~mjahr
                    INTO posting_date
               WHERE mseg~mandt = sy-mandt
                      AND mkpf~mandt = sy-mandt
                      AND mseg~werks  = w_matl-batch_reservations-werks
                     AND mseg~charg  = w_matl-batch_reservations-charg
                     AND mseg~bwart  IN ('261', 'Z61').
    Hope it will be helpful.
    Regards,
    Kannan

  • Report counterpart sample for   field = select(max)

    Post Author: JuneCruz
    CA Forum: Crystal Reports
    Hi Guys,
    This the contenct of my table.
    NFstCol NSndCol NTrdCol -
    1       One     12       One     13       One     14       Two     15       Two     16       Two     17       One     28       One     29       One     210      One     311      One     312      One     316      Two     217      Two     218      Two     219      Four    NULL20      Four    NULL21      Four    NULL
    Now I want to display the records all records that have the maximum ntrdcol group by nsndcolso the output should be.  Can this be done without using view, command as source for the database, or a subreport ?thanks.
      NfstCol NtrdColOne
          10     3      11     3      12     3
    Two       16     2      17     2      18     2
    Four       19     null      20     null      21     null
    Again.  Thanks guys.

    Post Author: ROMZILBER
    CA Forum: WebIntelligence Reporting
    To create a view is not a problem, supporting it is a different story
    But we have find a solution, you were correct about the filter.  I just did not know you can put the whole subselect in a  filter
    but here is a logic for the filter if someone would come up with the same problem
    tablename.NNNN_PROOF_DT = (SELECT MAX(X.NNNN_PROOF_DT) FROM tablename X  WHERE tablename.NNNN_CLAIM_NUMBER = X.NNNN_CLAIM_NUMBER AND   tablename.NNNN_LINE_CD = X.NNNN_LINE_CD)
    please let me know if you have any questions.
    sorry I am new to this so please do not take any offense

  • Select MAX(BUDAT) from ztab (custom table) where... NOT work!

    We use the following statement where BUDAT is one of the fields in our custom table ztab:
    Select MAX(BUDAT) from ztab (custom table) where...
    When activating the above code, get the following error:
    "Unknown column name "MAX(BUDAT)". not determined until runtime, you cannot specify a field list."
    How to resolve this problem to get a max value of the field BUDAT in custom table ztab (it's not an internal table)?
    <REMOVED BY MODERATOR>
    Edited by: Alvaro Tejada Galindo on Apr 10, 2008 3:56 PM

    HI,
    Tyr having a space after and before BUDAT.
    ( BUDAT ).
    Hope it helps,
    Shreekant

  • SELECT MAX(field) / SELECT MIN(field) return wrong values

    Hi,
    I have the query:
    Select 
        MAX(b1.time_s) as time_s 
    From
        BUSINESS_INVOCATIONS b1 
    Where
        b1.time_s >= 1219217034000 
        AND  b1.rec_sess_id = 2197756621378027521 
        AND  b1.comp_id IN  ( Select id  From J2EE_CONFIGURATION_BEAN  Where j2eeApplicationsTableId = 378302371920347137 )
    This return the MAX value of the comp_id and not the MAX value of the b1.time_s
    looks like there is a BUG in MAXDB when i have a join of two tables the MAX/MIN functions works on the wrong fields!
    If i change the query to be:
    Select 
        max(b1.time_s) as time_s 
    From
        BUSINESS_INVOCATIONS b1,
        J2EE_CONFIGURATION_BEAN jcb
    Where
        b1.time_s >= 1219217034000 
        AND  b1.rec_sess_id = 2197756621378027521
        AND jcb.j2eeApplicationsTableId = 378302371920347137
        AND  b1.comp_id = jcb.id
    The MIN/MAX functions works good and return the right value
    Pleasehelp me to find what is the problem and if any one already have a solution.
    Thanks,
    Yosef

    Dta for BUSINESS_INVOCATIONS tabel:
    ID,CLASS_TYPE,M_TIME,FUNCTIONENTRYID,TOP_PARENT,START_TIME,TIME_S,COMP_ID,REC_SESS_ID,PARENT,HTTP_SESSION,INVOCATION_HASH_CODE,MINOR_INVOCATION,TRANSACTION_ID,USER_TRACING_COOKIE,TRACINGTYPE
    2089670230321135617,H,1219738865499,1387108688451338241,2089670230321135617,1219217053734,1219217053734,414331168939311111,2197756621378027521,(null),2179742222868545537,-1238173181,GET,null,null,-1
    2089670230321135618,H,1219738865499,1387108688451338242,2089670230321135618,1219217071156,1219217071156,414331168939311108,2197756621378027521,(null),2179742222868545538,-628203986,GET,null,null,-1
    2089670230321135619,H,1219738865499,1387108688451338279,2089670230321135619,1219217073531,1219217073531,414331168939311112,2197756621378027521,(null),2179742222868545538,545948335,GET,null,null,-1
    2089670230321135620,H,1219738865499,1387108688451338284,2089670230321135620,1219217075671,1219217075671,414331168939311119,2197756621378027521,(null),2179742222868545538,1958406838,GET,null,null,-1
    2089670230321135621,H,1219738865499,1387108688451338289,2089670230321135621,1219217077359,1219217077359,414331168939311106,2197756621378027521,(null),2179742222868545538,-2089005650,GET,null,null,-1
    2089670230321135622,H,1219738865499,1387108688451338292,2089670230321135622,1219218442312,1219218442312,414331168939311115,2197756621378027521,(null),2179742222868545538,-1298180580,GET,null,null,-1
    2089670230321135623,H,1219738865499,1387108688451338297,2089670230321135623,1219218467765,1219218467765,414331168939311107,2197756621378027521,(null),2179742222868545538,1318337921,GET,null,null,-1
    2107684628830617601,E,1219738865515,1387108688451338244,2089670230321135618,1219217071171,1219217071171,324259176391901185,2197756621378027521,2089670230321135618,2179742222868545538,586868856,(null),(null),(null),-1
    2107684628830617602,E,1219738865515,1387108688451338281,2089670230321135619,1219217073531,1219217073531,324259176391901185,2197756621378027521,2089670230321135619,2179742222868545538,1886325459,(null),(null),(null),-1
    2107684628830617603,E,1219738865515,1387108688451338286,2089670230321135620,1219217075671,1219217075671,324259176391901185,2197756621378027521,2089670230321135620,2179742222868545538,-550916224,(null),(null),(null),-1
    2107684628830617604,E,1219738865515,1387108688451338291,2089670230321135621,1219217077359,1219217077359,324259176391901185,2197756621378027521,2089670230321135621,2179742222868545538,1676270392,(null),(null),(null),-1
    2107684628830617605,E,1219738865515,1387108688451338294,2089670230321135622,1219218442312,1219218442312,324259176391901185,2197756621378027521,2089670230321135622,2179742222868545538,-2074647744,(null),(null),(null),-1
    2107684628830617606,E,1219738865515,1387108688451338299,2089670230321135623,1219218467765,1219218467765,324259176391901185,2197756621378027521,2089670230321135623,2179742222868545538,63795915,(null),(null),(null),-1
    3278620531946946561,T,1219738865483,1387108688451338291,2089670230321135621,1219217077359,1219217077359,2828260569209896968,2197756621378027521,2107684628830617604,2179742222868545538,-1511714853,java.lang.ArithmeticException,(null),(null),18
    Daa for J2EE_CONFIGURATION_BEAN Table:
    select * from IDENTIFY.J2EE_CONFIGURATION_BEAN
    ID,J2EEAPPLICATIONSTABLEID,J2EEAPPLICATIONID,J2EECOMPONENTID
    180144094616485889,0,0,0
    180144094616485890,0,0,0
    180144094616485891,0,0,0
    180144094616485892,0,0,0
    180144094616485893,378302478220787713,0,0
    180144094616485894,0,0,0
    306244884182859777,378302478220787713,360288079711305731,306244884182859777
    324259282692341761,378302478220787713,360288079711305731,306244884182859777
    360288079711305729,378302478220787713,360288079711305729,0
    360288079711305730,378302478220787713,360288079711305730,0
    360288079711305731,378302478220787713,360288079711305731,0
    360288079711305732,378302478220787713,360288079711305732,0
    360288079711305733,378302478220787713,360288079711305733,0
    360288079711305734,378302478220787713,360288079711305734,0
    414331275239751681,378302478220787713,360288079711305731,594475260334571522
    414331275239751682,378302478220787713,360288079711305731,594475260334571522
    414331275239751683,378302478220787713,360288079711305731,594475260334571522
    414331275239751684,378302478220787713,360288079711305731,594475260334571522
    414331275239751685,378302478220787713,360288079711305731,594475260334571522
    414331275239751686,378302478220787713,360288079711305731,594475260334571522
    414331275239751687,378302478220787713,360288079711305731,594475260334571522
    414331275239751688,378302478220787713,360288079711305731,594475260334571522
    414331275239751689,378302478220787713,360288079711305731,594475260334571522
    414331275239751690,378302478220787713,360288079711305731,594475260334571522
    414331275239751691,378302478220787713,360288079711305731,594475260334571522
    414331275239751692,378302478220787713,360288079711305731,594475260334571522
    414331275239751693,378302478220787713,360288079711305731,594475260334571522
    414331275239751694,378302478220787713,360288079711305731,594475260334571522
    414331275239751695,378302478220787713,360288079711305731,594475260334571522
    540432064806125569,378302478220787713,360288079711305732,594475260334571523
    540432064806125570,378302478220787713,360288079711305732,594475260334571523
    540432064806125571,378302478220787713,360288079711305732,594475260334571523
    540432064806125572,378302478220787713,360288079711305732,594475260334571523
    540432064806125573,378302478220787713,360288079711305732,594475260334571523
    540432064806125574,378302478220787713,360288079711305732,594475260334571523
    540432064806125575,378302478220787713,360288079711305734,594475260334571524
    540432064806125576,378302478220787713,360288079711305734,594475260334571524
    576460861825089537,378302478220787713,360288079711305730,576460861825089537
    576460861825089538,378302478220787713,360288079711305731,576460861825089538
    576460861825089539,378302478220787713,360288079711305732,576460861825089539
    576460861825089540,378302478220787713,360288079711305733,576460861825089540
    576460861825089541,378302478220787713,360288079711305734,576460861825089541
    594475260334571521,378302478220787713,360288079711305729,594475260334571521
    594475260334571522,378302478220787713,360288079711305731,594475260334571522
    594475260334571523,378302478220787713,360288079711305732,594475260334571523
    594475260334571524,378302478220787713,360288079711305734,594475260334571524
    2810246277000855553,378302478220787713,360288079711305729,2810246277000855553
    2810246277000855554,378302478220787713,360288079711305729,2810246277000855554
    2810246277000855555,378302478220787713,360288079711305729,2810246277000855555
    2810246277000855556,378302478220787713,360288079711305729,2810246277000855556
    2810246277000855557,378302478220787713,360288079711305729,2810246277000855557
    2810246277000855558,378302478220787713,360288079711305729,2810246277000855558
    2810246277000855559,378302478220787713,360288079711305729,2810246277000855559
    2828260675510337537,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337538,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337539,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337540,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337541,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337542,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337543,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337544,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337545,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337546,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337547,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337548,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337549,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337550,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337551,378302478220787713,360288079711305729,2846275074019819521
    2828260675510337552,378302478220787713,360288079711305729,2846275074019819521
    2846275074019819521,378302478220787713,360288079711305729,2846275074019819521
    3152519848681013249,378302478220787713,360288079711305729,3152519848681013249
    3152519848681013250,378302478220787713,360288079711305730,3152519848681013250
    3152519848681013251,378302478220787713,360288079711305731,3152519848681013251
    3152519848681013252,378302478220787713,360288079711305732,3152519848681013252
    3152519848681013253,378302478220787713,360288079711305733,3152519848681013253
    3152519848681013254,378302478220787713,360288079711305734,3152519848681013254
    3170534138742571009,0,0,0
    3170534138742571010,0,0,0
    3170534247190495233,0,0,0
    3170534247190495234,0,0,0
    3602879811418062849,378302478220787713,360288079711305729,594475260334571521
    Hope this will help:)
    Thanks,
    Yosef

  • 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
    ;

  • 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

  • How to select max (field) and one more field from table?

    Hi experts!
    I need to select maximum value of ENDDA from PA0023 and BRANC of max ENDDA.
    How can I do that ?
    When I trying this code:
    This is the  code:
    SELECT MAX( endda ) branc
      FROM pa0023
      INTO (pa0023-endda, pa0023-branc).
    I get error message:
    The field "PA0023~BRANC" from the SELECT list is missing
    in the GROUP BY clause. Addition INTO wa or INTO (g1,...,gn)  is required.
    So what is the problem?
    Thanks forehead.

    Hi
    Though am not totally sure of your requirement, check below code samples without any syntax errors:
    1. As per you current coding:
    TABLES: pa0023.
    SELECT MAX( endda ) branc
           FROM pa0023
           INTO (pa0023-endda, pa0023-branc)
           GROUP BY branc.
    ENDSELECT.
    2. Above code results only on one record where as the criteria can be more than one. Eg: for a specific data more than one record can exist. Below code helps you handle the same:
    TABLES: pa0023.
    TYPES: BEGIN OF t_pa0023,
             endda TYPE endda,
             branc TYPE brsch,
           END OF t_pa0023.
    DATA: i_pa0023 TYPE TABLE OF t_pa0023,
          wa_pa0023 TYPE t_pa0023.
    SELECT MAX( endda ) branc
           FROM pa0023
           INTO TABLE i_pa0023
           GROUP BY branc.
    LOOP AT i_pa0023 INTO wa_pa0023.
    ENDLOOP.
    3. If the requirement is to get the Industry Key for the record with highest End Date. We can acheive it by using subquery something like:
    TABLES: pa0023.
    SELECT SINGLE endda branc
           FROM pa0023
           INTO (pa0023-endda, pa0023-branc)
           WHERE endda = ( SELECT MAX( endda ) FROM pa0023 ).
    Kind Regards
    Eswar

  • How to : SELECT MAX(EmpID) / count(*) in SELECT Query

    Hi,
    I like to find out the Maximum of the EmpID that is in the DB table.
    So I need to perform a select Query:
    Select MAX(EmpID) from EmpTable
    or Select Count(*) from EmpTable
    <StatementName>
    <dbTableName action=”SELECT”>
       <table>realDbTableName</table>
         <access>
             <??? />
          </access>
    </dbTableName> 
    </StatementName>
    I cannot type MAX(EmpID) or count(*) as access element
    So what should I mention in the Access?
    Thanks & regards,
    Siva Maranani.

    if 0CALDAY is used as a characteristic then you have to convert that as a key figure using a replacement path variable.
    Please see the below link for changing 0CALDAY as a key figure using Replacement path
    http://www.sd-solutions.com/documents/SDS_BW_Replacement%20Path%20Variables.html
    https://websmp106.sap-ag.de/~sapdownload/011000358700001963972003E/HowToCalcWithAttr.pdf
    Now you have to create an exception "Top N Rows" based on the replacement path variable created above.
    This will solve your problem.
    Regs
    Gopi.
    Award points if it helps ...

  • Select statement problem. how to get data in one row

    SELECT
    razm.datum_sklenitve,
    razm.datum_prenehanja,
    FROM
    zket_zaposlenci zap,
    zket_delovna_razmerja razm,
    zket_delovne_razporeditve razp,
    zket_sistemizacije_dm sistdm,
    zket_organizacijske_enote oe,
    zket_delovna_mesta delm
    WHERE
    zap.iid_zaposlenca = razm.iid_zaposlenca and
    razm.iid_delovno_razmerje = razp.iid_delovno_razmerje and
    sistdm.iid_sistemizacija_dm(+) = razp.iid_sistemizacija_dm and
    nvl(sistdm.iid_organizacijske_enote, razp.iid_organizacijske_enote) = oe.iid_organizacijske_enote and
    sistdm.iid_delovno_mesto = delm.iid_delovno_mesto and
    razm.datum_sklenitve <= to_date('31.12.'||:v_datum, 'dd.mm.yyyy') and
    nvl(razm.datum_prenehanja, to_date('31.12.'||:v_datum, 'dd.mm.yyyy')) >= to_date('01.01.'||:v_datum, 'dd.mm.yyyy') and
    razp.datum_nastopa_dela <= to_date('31.12.'||:v_datum, 'dd.mm.yyyy') and
    nvl(razp.datum_prenehanja_dela, to_date('31.12.'||:v_datum, 'dd.mm.yyyy')) >= to_date('01.01.'||:v_datum, 'dd.mm.yyyy') and
    zkep_splosno.zkep_hierarh_oe_n(nvl(sistdm.iid_organizacijske_enote, razp.iid_organizacijske_enote), :v_oe, to_date('31.12.'||:v_datum, 'dd.mm.yyyy')) = 1 and
    to_char(zkep_splosno.zkep_vrni_prvi_dr(razm.iid_delovno_razmerje, to_date('31.12.'||:v_datum, 'dd.mm.yyyy')), 'yyyy') = :v_datum
    and zap.iid_zaposlenca = 1093507
    order by razm.datum_sklenitve DESC
    return this rows:
    datum_sklenitve     datum_prenehanja
    1: 01.10.2010 00:00:00 | 31.12.2010 00:00:00
    2: 01.10.2010 00:00:00 | 31.12.2010 00:00:00
    3: 01.04.2010 00:00:00     | 31.08.2010 00:00:00
    4: 01.02.2010 00:00:00     | 31.02.2010 00:00:00
    how can i get in one select just this result.
    1: : 01.10.2010 00:00:00 | 31.08.2010 00:00:00 (first of A and then the next one of B which is not the same as top of B)
    I try everything but i do not know how. If anyone know how to do this.
    regards,
    MB

    Simma wrote:
    But the above is not documented.Not only that, but it's string aggregation which isn't meeting the OP's requirements.
    Though you could be easily forgiven as the OP hasn't clearly stated the logic behind the requirements.
    It could be something like wrapping the original SQL with something like the following...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select date '2010-10-01' as ds, date '2010-12-31' as dp from dual union all
      2             select date '2010-10-01', date '2010-12-31' from dual union all
      3             select date '2010-04-01', date '2010-08-31' from dual union all
      4             select date '2010-02-01', date '2010-02-28' from dual)
      5  --
      6  -- END OF TEST DATA
      7  --
      8  select max(case when rn = 1 then ds else null end) as ds
      9        ,max(case when rn = 2 then dp else null end) as dp
    10  from (
    11        select ds, dp, row_number() over (order by ds desc nulls last) rn
    12        from (
    13              select case when lag(ds) over (order by ds desc) = ds and lag(dp) over (order by ds desc) = dp then null else ds end as ds
    14                    ,case when lag(ds) over (order by ds desc) = ds and lag(dp) over (order by ds desc) = dp then null else dp end as dp
    15              from t
    16              order by t.ds
    17             )
    18*      )
    SQL> /
    DS                  DP
    01/10/2010 00:00:00 31/08/2010 00:00:00
    SQL>but who knows?

Maybe you are looking for