Using max select query

Hi all,
LOOP AT ITAB.
   IF S_KUNNR IS not INITIAL.
    SELECT MAX( MBLNR ) FROM MSEG INTO MBLNR
                                   WHERE MATNR EQ  ITAB-MATNR
                                   AND   KUNNR EQ ITAB-KUNNR
                                   AND   BWART  eq '951'.
   else.
     SELECT MAX( MBLNR )  FROM MSEG INTO MBLNR
                                   WHERE MATNR EQ  ITAB-MATNR
                                   AND   KUNNR ne ' '
                                   and   sobkz eq 'V'
                                   AND  BWART  eq '951'.
    endif.
i need mjahr also from mseg .Top part is working.
    LOOP AT ITAB.
   IF S_KUNNR IS not INITIAL.
    SELECT MAX( MBLNR ) mjahr  FROM MSEG INTO MBLNR mjahr
                                   WHERE MATNR EQ  ITAB-MATNR
                                   AND   KUNNR EQ ITAB-KUNNR
                                   AND   BWART  eq '951'.
   else.
     SELECT MAX( MBLNR )  mjahr FROM MSEG INTO MBLNR mjahr
                                   WHERE MATNR EQ  ITAB-MATNR
                                   AND   KUNNR ne ' '
                                   and   sobkz eq 'V'
                                   AND  BWART  eq '951'.
    endif.
after adding mjahr its not working.how to write query for that.

Like this:
tables: mseg.
data: mblnr like mseg-mblnr,
      mjahr like mseg-mjahr.
parameter: p_matnr like mseg-matnr.
SELECT MAX( MBLNR ) mseg~mjahr FROM MSEG INTO (MBLNR, mjahr)
where matnr eq p_matnr
GROUP BY mseg~mjahr.
WRITE:/ MBLNR, MJAHR.
ENDSELECT.
Pl. see ur other thread on the same and close either of them.
thanks\
Mahesh

Similar Messages

  • What is the syntax for creating global temporary table using a select query

    hii
    I'm creating a global temporary table using a select query ..how to mention 'on commit preserve rows' that?
    create global temporary table t1 as select * from trn_ordbase on commit preserve rows;
    but this is invalid syntax,so how to mention on commit preserve rows in this???if i dont mention it ,by default its considering as on commit delete rows.
    Please help me out of this problem.

    create global temporary table t1 as select * from trn_ordbase on commit preserve rows;You CANNOT use this syntax.
    http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/sqcmd.htm
    http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/glob_tab.gif
    http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/cre_tabl.gif

  • Update a field based on a max select query

    Hi
    I am just learning SQL so apologies if this is simple to you guys.
    I have a table called [Stock WIP Table] which has duplicates in it.  I have an [autonumber] field, a [palletid] field, and a [number5] field.  I have the select query working to show the most recent record for each palletID:
    SELECT
    PalletID,MAX(autonumber)
    FROMdbo.[Stock
    WIP Table]
    GROUPBYPalletID
    How do I get the query to now delete all rows that are not in the select query?
    Alternatively how could I add update the [number5] field to '1' where the [autonumber] is in the select and I can  then delete all records that do not contain a '1' in [number5]
    Can anyone help?
    Thanks
    AJ

    Hi
    I am just learning SQL so apologies if this is simple to you guys.
    I have a table called [Stock WIP Table] which has duplicates in it.  I have an [autonumber] field, a [palletid] field, and a [number5] field.  I have the select query working to show the most recent record for each palletID:
    SELECT
    PalletID,MAX(autonumber)
    FROMdbo.[Stock
    WIP Table]
    GROUPBYPalletID
    How do I get the query to now delete all rows that are not in the select query?
    Alternatively how could I add update the [number5] field to '1' where the [autonumber] is in the select and I can  then delete all records that do not contain a '1' in [number5]
    Can anyone help?
    Thanks
    AJ
    Simply this for the DELETE
    DELETE t
    FROM dbo.[Stock WIP Table]t
    WHERE EXISTS (SELECT 1
    FROM dbo.[Stock WIP Table]
    WHERE PalletID = t.PalletID
    AND autonumber > t.autonumber)
    No need of UPDATE bit value and then deleting. just above will ensure it gets deleted in single step
    another method you may use is this
    DELETE t
    FROM dbo.[Stock WIP Table] t
    CROSS APPLY (SELECT MAX(autonumber) AS MaxID
    FROM dbo.[Stock WIP Table]
    WHERE PalletID = t.PalletID
    )t1
    WHERE t1.MaxID <> t.autonumber
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to use dynamic select query which queries from 3 different table.

    Hi All,
    I am new to Toplink, i would like to use a named query to select some of the columns from 3 different tables with dynamic where clause.
    I have used the following lines. Please tell me how to get code for the dynamic where clause.
    First try :
    Vector objPersons = (Vector)session.executeQuery("findPersonByGlobalID",Person.class,vQueryArguments);
    The above method is not returning the vector or collection.
    Second Try:
    ReadAllQuery query = new ReadAllQuery(Person.class);
    query.useCollectionClass(LinkedList.class);
    LinkedList person = (LinkedList) session.executeQuery(query);
    The second try is returning the collection but, this fetches all the record from the table.
    1. How to query for range of records?
    2. How to query from multiple tables for some range. How to use dynamic range values ?
    Please reply with your answers or some pointers would help.
    Thanks and Regards,
    Vijay.B

    Hi,
    Did you try using a SQLCall ? It might be able to satisfy you requirements .. :-
    Employee employee = (Employee) session.executeSelectingCall(
        new SQLCall("SELECT * FROM EMPLOYEE WHERE EMP_ID = 44")
    );You can get more information here :-
    http://www.oracle.com/technology/products/ias/toplink/doc/1013/MAIN/_html/qrybas004.htm
    Regards,
    Sandeep

  • Joining two tables having no common fields using one select query

    Hi Experts,
    How to join two tables which are NOT having any field in common using only one select query?
    Your help will be appreciated.
    Thank you.

    Identify a third table (or more tables) with common fields with your two tables, or change your question either removing JOIN or removing NO COMMON FIELDS, else you wont get many responses and will be left alone in outer space, as suggested by Thomas.
    If you acturally require what you written, better execute two select and merge the two internal tables merging every record from first table with every record of second table, til no more memory is available.
    Regards,
    Raymond

  • Index not using in select query

    Hello All,
    I have table CITY with 2 composite primary key CITYCODE, COUNTRYCODE of course they are NOT NULL.
    i created an index.
    CREATE UNIQUE INDEX CITYKEY ON CITY     (CITYCODE, COUNTRYCODE);
    then i run explain plan as follows
    explain plan for
    select CityCode, CityName, CountryCode, LexiconId, UserId, Status
    from City
    where CountryCode = 'IR';
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 803 | 57013 | 112 (2)| 00:00:02 |
    |* 1 | TABLE ACCESS FULL| CITY | 803 | 57013 | 112 (2)| 00:00:02 |
    later i analyze table rebuild indexes but again explain plan gives me the same result.
    Could any 1 please guide me to solve this issue.
    Thanks
    Edited by: mohsin javed on Feb 17, 2011 10:10 AM

    Hello All,
    Again with one more query which having join:
    table SDNMASTER with sdnid pk and SDNMASTER_PK index on SDNID, table SDNALIAS sdnid pk and SDNALIAS_PK index on SDNID.
    explain plan for
    SELECT a.SourceId,
              a.SourceName,
              b.LastName,
              a.Status
    FROM SdnMaster a,
              SdnAlias b
    WHERE a.SdnId = b.SdnId
    AND a.Status in ('1', '3')
    AND b.LastName is not null
    ORDER BY b.LastName;
    PLAN_TABLE_OUTPUT
    Plan hash value: 1153036645
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 2399 | 64773 | 69 (5)| 00:00:01 |
    | 1 | SORT ORDER BY | | 2399 | 64773 | 69 (5)| 00:00:01 |
    |* 2 | HASH JOIN | | 2399 | 64773 | 68 (3)| 00:00:01 |
    |* 3 | TABLE ACCESS FULL| SDNALIAS | 2399 | 21591 | 11 (0)| 00:00:01 |
    |* 4 | TABLE ACCESS FULL| SDNMASTER | 4562 | 82116 | 56 (2)| 00:00:01 |
    Even after analyzing table and rebuilding index its not using the index..
    Thanks

  • How to use a SELECT QUERY using fdmAPI.executeDML in FDMEE Jython Script?

    Hi,
    Just the way we use DELETE and INSERT Statement using fdmAPI.executeDML, is there any way of executing a SELECT Statement and get the output in variables?
    I would like to refrain from using import java.sql and enter the connection details in a script.I would like to rather use SELECT directly if possible. I want to read the TDATASEG.DATAKEY column and use it for some custom process in a custom table
    Thanks!

    Hi SH,
    Somehow after upgrading to FDMEE 11.1.2.3.530, this is not working.
    I get an error saying
         while (RSTxnType.Next()):
    Attribute Error: 'oracle.jdbc.driver.OracleResultSetImpl' object has no attribute 'Next'

  • How to find the lowest grade for each student using a Select query?

    Hey I'm having a bit of trouble here
    I have this table
    Student - Grade
    John - 8
    Richard - 9
    Louis - 9
    Francis - 5
    John - 13
    Richard - 10
    Peter - 12
    Grades can range from 0 to 20.
    Name - varchar(50)
    Grade - integer
    I am trying to generate a SQL search to find each lowest grade for each student.
    So far I have done:
    select s.name,s.grade
    from student s
    where s.grade = (select min(grade) from student)
    The result of that search returns me only the lowest grade of all the grades posted above, which is Francis - 5.
    I want to find the lowest grade, but not only for Francis but also for every other student in the table.
    How do I do that?
    Thank you in advanced.

    ok,
    Now we are moving into Analytic SQL:
    with student as (select 'John' name, 8 grade, 'fail' result from dual union all
                     select 'John' name, 13 grade, 'pass' result from dual union all
                     select 'Francis',     5 grade,  'fail' from dual union all
                     select 'Peter', 12, 'pass' from dual)
    -- End of your test data
    SELECT   name,
             MAX (result) KEEP (DENSE_RANK FIRST ORDER BY grade) result,
             MIN (grade) min_grade
    FROM     student
    GROUP BY namePlease note that I passed ;)
    Regards
    Peter
    To read more:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#SQLRF00641

  • Using case when statement in the select query to create physical table

    Hello,
    I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
    I have a physical table based on a select table with one column.
    SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
    I also have a customer table.
    In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
    In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
    EXECUTION
    When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
    everything works as expected. YE!!
    Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
    SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
    FROM DUAL
    Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
    surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
    SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
    If anyone has any explanation to this error and how we can achieve the same, please help.
    Thanks.

    Hello,
    Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
    I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
    Here is what I am trying to do. the select query of the physical table is as follows.
    SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
    The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
    Thanks.

  • Is it worth using select query on infotype tables

    Hi Experts,
    I might be posting in the wrong column, but i just need to know is it worth using a select query on Infotype tables (PAxxxx)?? or should we prefer using the function modules for data fetching?
    If select is not suggested, what is the reason for that?
    Rgds
    Prateek

    Hi ,
    Its not said that u cant write select on PAXXXX tables . Yes of couse LDB are there to fetch the data but it depends on
    the requirement when to write a select and when to consider using in LDB .
    Generally when you are looking at say 8 to 10 tables of infotypes with free selection , then LDB is suggested to fetch the data .
    if you are looking to fetch the data for say some tables for a restricted selection (where clause) then select is used .
    If i want to write a program using select only then fetching data from infotypes tables for large no of records will lead to
    more time consumption which becomes easier in LDB as they are fetched in hierarchy level based on keys .
    Normally it will be a combination of LDB and select querys  in the development scenario
    Br,
    Vijay.

  • Select Query Using Two Tables

    Friends,
       I need to select some fields from two different tables which doesnot contain any field in common.How to Get this using a select query..
       But there is a field X in table 1 which is equalient to eight char of another field y in table 2.I tried using offset but it is giving error.
    Thanks in Advance
    Regards
    Yamini

    Hello,
    You can use a subquery
    SELECT * FROM SFLIGHT AS F INTO WA_SFLIGHT
        WHERE SEATSOCC < F~SEATSMAX
          AND EXISTS ( SELECT * FROM SPFLI
                        WHERE CARRID = F~CARRID
                           AND CONNID = F~CONNID
                           AND CITYFROM = 'FRANKFURT'
                           AND CITYTO = 'NEW YORK' )
          AND FLDATE BETWEEN '19990101' AND '19990331'.
      WRITE: / WA_SFLIGHT-CARRID, WA_SFLIGHT-CONNID,
               WA_SFLIGHT-FLDATE.
    ENDSELECT.
    or simply query the first table and then the second table with select endselect.
    select * from table a.
       select * from table b into output_table
                     where b-field1 eq a-field2.
    endselect.
    or read the data into one internal table loop at it and then query the other table.
    Regards,
    Shekhar Kulkarni

  • Re querying a table using ONE Select

    Hello Experts,
    In one hit i want to query a Table2 2 times using oNE select query
    Lets say, i have 2 database tables  : Table1 & Table2.
    & internal tables say itab1 & itab2.
    Lets say they have fields:  fld1 fld 2 fld3 fld4.
    1) I used select * from Table1 into table itab1.
    2) Now i want to get entries from table2 based on itab-fld1 and itab2-fld4
    select * from Table2 into itab2
    FOR ALL ENTRIES in itab1
    where fld1 = itab1-fld1
      and fld2 = *itab2-fld4*  (entries based on
    itab2)
    Please suggest.,
    Thanks
    Ricky

    Hi Ricky,
    it is almost impossible to explain anything if you do not have an example of what you are going to do. When I say example, I don not mean Lets say, i have 2 database tables or Lets say they have fields but a case that everyone understands.
    If you are talking about SAP tables, give the names and fields. If you are talking about user-defined tables, decribe the process, give table and field names and characteristics (data element, domain, check table, search help...) of each field.
    For the question Now please tell me how can i write aliasing the answer is a clear F1.
    Shortly: If you write <tabname> AS <alias> or <fieldname> AS <alias> this means that <tabname> and <fieldname> identifie table and field in the database and <alias> is used for it in the query clause and for the data transfer from database to INTO data object.
    Note: It is always better to ask a question for a specific task. Getting the answer you can use it in all tasks that are comparable to the one you asked for. Asking a bla-bla question ( Lets say... ) will rarely get a useful answer.
    Regards,
    Clermens

  • Select query differences between oracle 9 and oracle 8.

    Hi,
    I have a problem using the select query between oracle 7 and oracle 9i I don't have the same result :
    ex:
    With oracle7
    SQL> select 'champ1','champ2' from DUAL;
    'CHAMP 'CHAMP
    champ1 champ2
    With Oracle 9
    SQL> select 'champ1','champ2' from DUAL;
    'CHAMP1' 'CHAMP2'
    champ1 champ2
    So Can someone tell me how to solve this problem ? Is there a parameter in oracle 9 to set?
    Thanx.

    Whenever you are posting anything over internet forums like this - you should be very careful about not just posting the details which requires to solve the problem - also should be sensible about your question.
    This is not at all desired when you are posting such question. It may be because - you may not well aware of the fact.
    My suggestion is -> First Go through the basics Of SQL in general.
    Then go for any specific product like Oracle/ SQL Server/ Sybase etc.
    And, finally learn the advanced commands of that DB.
    You asked it - may be you thought the difference in output in terms of lines. But, that is not your actual output. That is the graphical display part only.
    Anyway,
    You can get the quite familier output by first type the following command ->
    set lin 310Regards.
    Satyaki De.

  • Performance issue with select query and for all entries.

    hi,
    i have a report to be performance tuned.
    the database table has around 20 million entries and 25 fields.
    so, the report fetches the distinct values of two fields using one select query.
    so, the first select query fetches around 150 entries from the table for 2 fields.
    then it applies some logic and eliminates some entries and makes entries around 80-90...
    and then it again applies the select query on the same table using for all entries applied on the internal table with 80-90 entries...
    in short,
    it accesses the same database table twice.
    so, i tried to get the database table in internal table and apply the logic on internal table and delete the unwanted entries.. but it gave me memory dump, and it wont take that huge amount of data into abap memory...
    is around 80-90 entries too much for using "for all entries"?
    the logic that is applied to eliminate the entries from internal table is too long, and hence cannot be converted into where clause to convert it into single select..
    i really cant find the way out...
    please help.

    chinmay kulkarni wrote:Chinmay,
    Even though you tried to ask the question with detailed explanation, unfortunately it is still not clear.
    It is perfectly fine to access the same database twice. If that is working for you, I don't think there is any need to change the logic. As Rob mentioned, 80 or 8000 records is not a problem in "for all entries" clause.
    >
    > so, i tried to get the database table in internal table and apply the logic on internal table and delete the unwanted entries.. but it gave me memory dump, and it wont take that huge amount of data into abap memory...
    >
    It is not clear what you tried to do here. Did you try to bring all 20 million records into an internal table? That will certainly cause the program to short dump with memory shortage.
    > the logic that is applied to eliminate the entries from internal table is too long, and hence cannot be converted into where clause to convert it into single select..
    >
    That is fine. Actually, it is better (performance wise) to do much of the work in ABAP than writing a complex WHERE clause that might bog down the database.

  • Can I retrieve data from a multiple select query?

    I recently have been able to consolidate many queries into one large one, or into large compound queries. I found an instance where I am using a select query in a for loop that may execute up to 400 times. I tried building a huge compound query, and using DB Tools Execute Query vi, followed by the DB Tools Fetch Recordset Data vi. The query executes at the database without an error, but the Fetch Recordset Data vi only returns the first instance. Is there a way to retrieve all the data without having to send up to 400 separate select queries?

    Sorry I didn't replt earlier, I was on vacation. The query I am using is to check serial numbers, and determine if they are all valid. The programs purpose is to define a serial number to a pre-existing part number. Our company makes inclinometers and accelerometers, and this entire series of LabVIEW programs is designed to automate the calibration and testing of these units. The part number definitions can contain 3 or 4 hundred parameters, so the database itself consistes of 44 tables with potentially several hundred columns per table. It is designed to not only provide definitions to every part number, but also to store all potential raw unit data to be calculated and formed into a report at any time. The logistics of getting that much data in and out of the database have forced me to do things more effeciently. The actual query in question is to take each serial number either manually entered, or automatically picked, and see if they already exist with the part number they are being defined as. If there are any duplicates, then the program will alert the operator that serial numbers x, y, and z for instance have already been asigned as the part number in question. Currently I run a simple query once for each serial number. This works, but there may be 200 serial numbers assigned. Also the serial numbers can contain upper or lower case letters. By making all the serial number letters into capitals, then into lower case, it could mean up to 400 individual queries going out over the LAN. This is a bandwidth hog, and time consuming. I started experimenting with compound queries. The actual query used is below.
    SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000005';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000006';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000007';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000008';SELECT SERIALNO FROM "maintable" WHERE PARTNO = '475196-001' AND SERIALNO = '3000009'
    When I execute this query, SQL Server 2000 has no problem with it, but the DB Tools Fetch Recordset Data vi only returns the first match. I think my answer may lie with OR statements. Rather than sending what amounts to potentially dozens of individual queries, I should be able to chain them into one query with a lot of OR statements. As long as the OR statement is not an exclusive OR statement, I think it should work. I haven't tried it yet, and it may take some time to get the syntax right. The query is built in a for loop with the number of iterations equal to the number of serial numbers being defined. Once I get this working I will alter it to include both upper and lower case letters that can be included in the query. Any suggestiona of how the query should be structured would be most helpful, or another way to achieve what I am trying to accomplish.
    SciManStev

Maybe you are looking for

  • Default behaviour of the Escape key while editing a cell in JTable??

    Hi all, i have a Jtable which get its data from an own model object which extends DefaultTableModel. If i overwrite the isCellEditable(row, col) method within the tablemodel object and set a specific column to editable, then i notice while editing a

  • How do you know if the battery is dead? Detailed history. Help needed!!

    So here's the story. Bought the PowerBook G4 1.67GHz used off a trusted friend. Worked fine for 3 months or whatever up till yesterday basically. The charger was in, orange light, but the charger in the corner was going from "Charge" to "15%" and swi

  • Wli async process restarting itself with server restart

    Hello I have an async process calling a sync process in a wli server that calls an ejb in a diff wls server that runs a long operation. When I kick the async process and the long operation starts, if I shut down the wli server before the operation en

  • 802.1x machine auth w/ certificate authority

    Two quick questions ... I am building a lab for 802.1x, I want to use peap w/ mschap v2 and I want to do machine authentication only.  I have AD and CA services running on a test windows 2003 server. I have ACS setup, my AD is connected, my switch is

  • Alpha conversion error

    Hi experts, We have an error coming for the field as 0ref_doc_no as alpha conversion error. We are in BI 7 .Transformations we are using. How to rectify this error ? Regards, Bhadri M.