Max function help

I am trying to pull 1 row for each document_id, it should be the max version but im getting duplicate rows for some reason.
create table max_test
"PROJECT" VARCHAR2(50 BYTE),
"DOCUMENT_ID" VARCHAR2(50 BYTE),
"VERSION" VARCHAR2(5 BYTE),
"CREATE_DATE" VARCHAR2(DATE),
"UPDATE_DATE" VARCHAR2(DATE),
"MANAGER_NAME" VARCAHR2(100 BYTE)
INSERT ALL
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (500,1, 18, '01-MAR-12','01-MAR-12','BOB JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (500,2, 6, '01-MAR-12','01-MAR-12','STEVE JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (500,3, 2, '01-MAR-12','01-MAR-12','MIKE JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (501,4, 5, '01-MAR-12','01-MAR-12','MARK JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (501,5, 1, '01-MAR-12','01-MAR-12','TOM JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (502,6, 20, '01-MAR-12','01-MAR-12','EVE JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (502,7, 3, '01-MAR-12','01-MAR-12','CHRIS JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (503,8, 1, '01-MAR-12','01-MAR-12','LARRY JONES')
INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UDPATE_DATE,MANAGER_NAME) VALUES (503,9, 2, '01-MAR-12','01-MAR-12','JOE JONES')
select distinct(project),max(version),document_id,create_date,update_date,manager_name
from max_test
group by project,document_id,create_date,update_date,manager_name
I get multiple rows for each project and only want 1 row per project, the 1 row that has the max version

Hi Jay,
first of all when you post statements I suggest you to test them before posting.
Your statements had some syntax error.
Additionally when you post your code put it between two lines starting with {noformat}{noformat} to have your code properly formatted.
I have modified your statements in the way I suppose they should be:CREATE TABLE max_test
project VARCHAR2 (50 BYTE)
, document_id VARCHAR2 (50 BYTE)
, version VARCHAR2 (5 BYTE)
, create_date DATE
, update_date DATE
, manager_name VARCHAR2 (100 BYTE)
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (500,1, 18, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'BOB JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (500,2, 6, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'STEVE JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (500,3, 2, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'MIKE JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (501,4, 5, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'MARK JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (501,5, 1, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'TOM JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (502,6, 20, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'EVE JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (502,7, 3, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'CHRIS JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (503,8, 1, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'LARRY JONES');
INSERT INTO max_test (PROJECT,DOCUMENT_ID, VERSION, CREATE_DATE, UPDATE_DATE,MANAGER_NAME) VALUES (503,9, 2, TO_DATE('01-MAR-12','DD-MON-YYYY'), TO_DATE('01-MAR-12','DD-MON-YYYY'),'JOE JONES');
Now, coming to your problem, it looks that you have no idea of how the group by clause and distinct keywords are affecting your query.
Using DISTINCT before the list of your column will ensure that the output does not have duplicate rows (having the same value). Note that distinct does not affect a single column but all the columns of the query. So writingselect distinct(project),max(version),document_id,create_date,update_date,manager_name
ORselect distinct project,max(version),document_id,create_date,update_date,manager_name
has no difference.
Group by is listing the columns you want to use to group your output.
I.e.:SELECT project, MAX (version) version
FROM max_test
GROUP BY project;
PROJECT VERSION
503 2
500 6
501 5
502 3
If you add more colums in your group by then the grouping functions (MAX, MIN, AVG, SUM etc.) will be applied to groups represented by unique combination of all columns in your GROUP BY clause.
In your case your are grouping by  project,document_id,create_date,update_date,manager_name
So you will get the maximum version of each combination of project,document_id,create_date,update_date,manager_name. If document_id, create_date, update_date and manager_name are different for a specific project, then you will have several rows for the same project.SELECT DISTINCT (project), MAX (version), document_id, create_date
, update_date, manager_name
FROM max_test
GROUP BY project, document_id, create_date, update_date
, manager_name;
PROJECT MAX(VERSION) DOCUMENT_ID CREATE_DATE UPDATE_DATE MANAGER_NAME
503 1 8 01-MAR-12 01-MAR-12 LARRY JONES
503 2 9 01-MAR-12 01-MAR-12 JOE JONES
500 18 1 01-MAR-12 01-MAR-12 BOB JONES
502 20 6 01-MAR-12 01-MAR-12 EVE JONES
502 3 7 01-MAR-12 01-MAR-12 CHRIS JONES
500 6 2 01-MAR-12 01-MAR-12 STEVE JONES
501 1 5 01-MAR-12 01-MAR-12 TOM JONES
501 5 4 01-MAR-12 01-MAR-12 MARK JONES
500 2 3 01-MAR-12 01-MAR-12 MIKE JONES
I hope it is clear.
Regards.
Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • Join/max function help

    Currently I have 3 tables I am working with.
    A meeting table which holds information on:
    •     meeting_id
    •     meeting_date
    •     meeting_time
    •     meeting_reference_number
    A customer table which holds:
    •     Customer_id
    •     Forename
    •     Surname
    A customer_booking table which hold:
    •     customer_id
    •     booking_id
    What I am trying to do is bring back all the customers along with the last meeting date they had. This is done and works fine using the sql below. However, I also would like to see the meeting reference number that goes along with the last meeting date. But if I simply add bkg.meeting_reference_number to the select statement it brings me back multiple meeting dates per customer and not just the last one they had.
    SELECT cust.surname, cust.forenames,
           max(bkg.meeting_date)last_meeting_date
    FROM   customer cust,
           booking bkg,
           customer_booking cbk
    WHERE bkg.id = cbk.bkg_id
    AND   cust.id = cbk.vis_id
    GROUP BY cust.surname, cust.forenames  Ive tried a number of things but cant seem to arrive at the solution, any help would be greatly appreciated.
    Thank you.

    Thanks alot, that has helped me a great deal however the next part of my query which i expected to be easy is also causing similar problems. As well as the tables etc mentioned above i also have a few additional tables:
    Staff table which consist of:
    •     id
    •     personell_id
    Personell table which consists of:
    •     id
    •     Forename
    •     Surname
    And a staff_booking table which consist of:
    •     Bkg_id
    •     Staff_id
    What I want to be able to do is get a list of all the staff that will also be attending the customers LAST meeting. Again the below code seems to bring me back details of every meeting and not just the latest, I tried messing around with using what you told me before (keep and dense rank etc) but it didn’t seem to work, possibly because the data isn’t from the booking table I would imagine.          
    SELECT cust.surname, cust.forenames,
                max(bkg.meeting_date)last_meeting_date,
                max(meeting_ref_num) keep (dense_rank first order by bkg.meeting_date desc)  last_meeting_ref_num,
                staff_info.staff_name
    FROM   (SELECT staff.id staff_id,per.surname
                || DECODE (per.forenames, NULL, NULL, ', ' || per.forenames)  staff_name
                  FROM staff staff,
                  personel per
                  WHERE per.ID = staff.per_id) staff_info,
               Staff_booking sbk
               customer cust,
               booking bkg,
               customer_booking cbk
    WHERE bkg.id = cbk.bkg_id
    AND   sbk.bkg_id = cbk.bkg_id
    AND   staff_info.staff_id = sbk.staff_id
    AND   cust.id = cbk.vis_id
    GROUP BY cust.surname, cust.forenames;Again i would greatly appreciate your help and explaination of how the code works so as I can learn from it.
    Thank you.
    Edited by: user13390506 on 13-Jan-2011 08:59

  • Using max function in PL/SQL

    VERY URGENT...
    Am new to oracle...
    I've written a package that display gif images in form of histogram/bar chart. using html,
    I need to use max function to display values proportionately.
    please help. i need to complete this assignment by 2/9/00 by 10.00 am at the latest. I've half written a function but I don't know if there's a simpler way fo doing this. html enabled

    First of all Thanks to all gentlemen who replied ..many thanks ...
    Tried the ROW_NUMBER() option but still it is taking time...have given output for the query and tkprof results as well. Even when it doesn't fetch any record ( this is a valid cased because the input header id doesn't have any workflow request submitted & hence no entry in the wf_items table)..then also see the time it has taken.
    Looked at the RANK & DENSE_RANK options which were suggested..but it is still taking time..
    Any further suggestions or ideas as to how this could be resolved..
    SELECT 'Y', 'Y', ITEM_KEY
    FROM
    ( SELECT ITEM_KEY, ROW_NUMBER() OVER(ORDER BY BEGIN_DATE DESC) RN FROM
    WF_ITEMS WHERE ITEM_TYPE = 'xxxxzzzz' AND ROOT_ACTIVITY = 'START_REQUESTS'
    AND SUBSTR(ITEM_KEY,1,INSTR(ITEM_KEY,'-') - 1) = :B1
    ) T WHERE RN <= 1
    call count cpu elapsed disk query current rows
    Parse 0 0.00 0.00 0 0 0 0
    Execute 1 0.00 1.57 0 0 0 0
    Fetch 1 8700.00 544968.73 8180 8185 0 0
    total 2 8700.00 544970.30 8180 8185 0 0
    many thanks

  • Regarding MAX() function

    Hi,
    I am having table A. I have few records in the table A. Database version : 9.2
    ID name deleted
    2 XYZ N
    3 ABD N
    4 GJK N
    5 GJK N
    6 HGY N
    7 YJG N
    8 PIN N
    9 BMF N
    10 OLG N
    I used the following query...
    SELECT MAX(ID) FROM A WHERE DELETED='N';
    It was worked fine until now....
    Now i used the same query...
    But the result is
    MAX(ID)
    9
    Please help me... What is the reason?
    I want the correct result...
    Thanks in advance....                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Kamran Agayev A. wrote:
    Yes you can use TO_NUMBER inside MAX function and it will not give any error in the futureHave you tested it?
    SQL> create table a (id,name,deleted)
      2  as
      3  select '2', 'XYZ', 'N' from dual union all
      4  select '3', 'ABD', 'N' from dual union all
      5  select '4', 'GJK', 'N' from dual union all
      6  select '5', 'GJK', 'N' from dual union all
      7  select '6', 'HGY', 'N' from dual union all
      8  select '7', 'YJG', 'N' from dual union all
      9  select '8', 'PIN', 'N' from dual union all
    10  select '9', 'BMF', 'N' from dual union all
    11  select '10', 'OLG', 'N' from dual
    12  /
    Tabel is aangemaakt.
    SQL> select max(id)
      2    from a
      3   where deleted = 'N'
      4  /
    MA
    9
    1 rij is geselecteerd.
    SQL> select max(to_number(id))
      2    from a
      3   where deleted = 'N'
      4  /
                        MAX(TO_NUMBER(ID))
                                        10
    1 rij is geselecteerd.
    SQL> insert into a values ('2A', 'ABC', 'N')
      2  /
    1 rij is aangemaakt.
    SQL> select max(to_number(id))
      2    from a
      3   where deleted = 'N'
      4  /
    select max(to_number(id))
    FOUT in regel 1:
    .ORA-01722: invalid numberRegards,
    Rob.

  • Use of MAX function in Query Designer

    Dear all,
    i want to create a report that gives ONE line per period that holds the MAX of several lines per period. So NO addition of the KF but just one line item.
    I tried to use the MAX function and/or MAX drop-down boxes via properties on KF,
    but they all provide the wrong result (mostly the MAX after summaction of the KF).
    Any ideas ??
    Greetz,
    Herman

    Hi Herman
        Please check the KF infoobject property which might have been set to Summation.  You need to change the property of the KF to MAX if you wish to have MAX value. 
        Hope this might be helpful to you
    Thanks
    Kishore Kusupati

  • How to use MAX function in SSAS MDX Query

    I want to run this Query with MAX Condition on LAST_DATA_UPDATE Column .

    Hi Ashishsingh,
    According to your description, you want to know how to use MAX function in SQL Server Analysis Services MDX Query, right? In this case, please refer to the link below which describe the syntax and sample of MDX function.
    http://technet.microsoft.com/en-us/library/ms145601.aspx
    http://www.mdxpert.com/Functions/MDXFunction.aspx?f=64
    Hope this helps.
    Regards,
    Charlie Liao
    TechNet Community Support

  • Oracle:how to use max() function in case expression

    how to use max() function in case expression, Please explain with any example

    Hope this helps and should be self explanatory
    with t as
    (select 1 col,100 col2 from dual union
    select 2 ,100 from dual union
    select 2 ,200 from dual union
    select 3,100  from dual union
    select 3,200  from dual  )
    select col, case when max(col2)=100 then 'with 100 range'
    when  max(col2)=200 then 'with 200 range' end  from t group by col

  • How to use MAX() function with date field

    Hi Frzz,
    I have created a Graphical calculation view in which i have multiple records for each employee with different dates. But my requirement is to take the records which have maximum date.
    I have converted the date into Integer and applied the MAX() function. But still am getting multiple records.
    Is there is any other way we can achieve this requirement in Graphical Calculation view??  Your suggestion will really help me.
    Thank  you.
    Krishna.

    Hmm... what have you tried out so far?
    Look, I took the effort and created a little example, just for you
    Assume we have a table that contains the logon dates of users.
    Every line contains the logon date and some info on the user.
    Very much like a not-normalized log file could look like.
    Now, the output we want is: one line per user with the most current logon time.
    Not too difficult:
    1. Have a aggregation node that gives you the distinct user details - one line for every user:
    2. Have another aggregation node that gives you the last (MAX) logon date per user:
    Finally you do a 1:1 join on the USER_ID and map the result to the output.
    Easy as pie
    - Lars

  • Using Max Function in View Criteria

    Hi
    I am having a requirement where by i need to make use of max function in view criteria but not able to see any such option. Can someone please help me over it. Here is the requirement.
    In table i will be having multiple rows for an employee and i need to pick latest row based on a column say request_id For e.g.
    Emp RequestId
    A 1
    A 2
    A 3
    A 4
    So if i pass the employee id i should get Row Number 4. In simple SQL language I want something like this
    select * from emp where empid=A and requestid=( select max(requestid) from emp where empid=A)
    Just wanted to know is there any approach that i can use to do all this as part of View Criteria or any other way.
    Any help is appreciated!!!
    Thanks
    AJ

    One way is this -
    1)https://blogs.oracle.com/adf/entry/using_groovy_aggregate_functions_in (You might need to create a self-referencing VL for this , try if it works using a ViewAccessor too)
    OR
    Order by RequestId descending in your SQL for the VO if thats ok , then have the ViewCriteria for the EmpId and programmatically pickup the first row...

  • Problem with max function in air

    Hello ,how are you,I write this query : [b]SELECT *, max(trackerId) FROM  sessiontracker [/b]in
    sqlite manager and it works ,but when I use it in Air application  ,it send me back this error:
    ReferenceError: Error #1056: Cannot create property max(trackerId) on
    com.afarinesh.models.SessionTracke
    ,I guess that Actionscript 3 can not pars max function.
    I appreciate your help

    Hello,
    AIR does support max function. Please see Page 1067 of the actionscript developer's guide, which can be download from http://help.adobe.com/en_US/as3/dev/as3_devguide.pdf. And I tried the max function in my AIR application: create a table, insert some records into the table, and use the "select *, max(column) from table". It works fine.
    I noticed your error is the ReferenceError and related to some AS class. So I am not sure if you use any ORM tool. If so, the issue may be caused by the use of the tool. Please check the configuration and usage of the tool.
    Thanks,
    Yang

  • Improve built in SQL MAX function

    Hi
    I am trying to improve the performance of the built in SQL max function. right now searching through over 500,000 rows takes lot of time which can be reduced. Can anyone suggest me something which would be helpful in this or may be give me a link if it has been discussed before?
    Anything is appreciated.
    Thanks

    Tolls wrote:
    Um...considering you were planning on improving on the MAX function, I sort of thought you might actually know how to use it. Otherwise how would you know it was running slowly?thanks Tolls i'd figured it out right after i posted it. i guess i was too excited after reading (you got it in milliseconds) that posted right away without doing anything myself..but yeah it did make it super fast..thanks a lot for your help and time..
    cotton.m wrote:
    d_khakh wrote:
    hi tolls thanks for getting back
    could u explain how did u do that? i found out how to create index..i have the following statement:
    create index col on table2 (col2);
    cud u tell me how to go from here and find the max in col2..thanks again for ur help?
    sigh
    Where to go from here? Just execute your damn query. If you really did create the index on the right column and your database isn't stupid and there isn't a lot more to this problem then you told us about like some where clauses then that's it. She's as fast as she's going to get.
    And remember, it's impolite to converse in a public forum while chewing your cud. Unless of course you actually meant "could" but were just too lazy to type it. Don't be lazy, use full words. Thanks.like i said above..i figured it out after some time.
    i wasn't being lazy ..thats msn talk..i thought people understood that..anyways your point noted too.

  • Can I use Count, Max function in PLSQL BLOCK.

    Can U help me to use count, max function in PLSQL BLOCK.
    Because it is giving me error "It is a SQL function"

    SELECT COUNT(*)
    INTO l_variable
    FROM dual;
    Will work inside PL/SQL

  • Max() function in Data services

    Hi Experts,
         i'm using Data services (etl), and working on budget table(ms sql table),
    1. the table has a field [budget amount] and [version_no],
    2. before i extract the budget amount i need to know the highest version in [version_no] fields
    3. can any body help me on this? i tried to use the script:  ifthenelse(max(version-no),budget_amount, 0) but it retuns error when validation.
    please help me on this:
    thanks in advance
    Archie72

    Hi,
    Connect a Query transform say Query1 from the source table
    Inside Query1
    select MAX(version no) into the output. Do not select the other budget field into the output.
    Connect another Query transform say Query2 from the source table. Connect the output of Query1 also into Query2.
    Inside Query2:
    Join the source table and Query 1 in where
    Query1.version no = SourceTable.version no
    Select version no and budget into Query 2 output and connect it to target table.
    Assuming that the source table has only version number and budget. You might have to tweak and make use of Group By as required where you use the MAX() function. SourceTable mentioned in the where condition will be replaced with your exact source table name
    Regards,
    Suneer

  • Max Function in @Prompt

    Hi,
    We have a week dimension in our Cal table. The user wants to use a condition where if he will enter week number (for eg: 13-2009) then data of that particular week should be displayed and if he will enter ' * ' then data of last week in the table will be displayed.
    To fetch the last week we can use max(cal.week). But how to use it in prompt? Because when I am using max function then getting an error that group functions are not supported in prompt. Below is the prompt condition I am using:
    CAL.WEEK IN (decode(@Prompt('Enter week (or * for last week)', 'A',, MONO,FREE),'*',(MAX(CAL.WEEK)),@Prompt('Enter week (or * for last week)', 'A',, MONO,FREE)))
    I tried it by creating an object for MAX(CAL.WEEK) and then tried to use it in prompt, but since prompt doesnot support it, this object was not visible in the object list while creating the prompt.
    Please help.
    Thanks,
    Kirti

    The problem you are facing is that a condition containing a grouping function belongs in the having close, not in the where...
    There is no way to create something in the universe that will be put in the having clause.
    So what you need is to put it in as subselect, which can go in the where.
    Try the predefined filter like this...
    (CAL.WEEK = @Prompt('Enter week (or * for last week)', 'A',, MONO,FREE)
    OR (@Prompt('Enter week (or * for last week)', 'A',, MONO,FREE)='*'
       AND CAL.WEEK=(SELECT max(C2.WEEK) FROM CAL C2)))
    Do not forget the brackets around the two conditions with OR
    Good luck,
    Marianne

  • Max function in select statemnt

    Hi,
       Can someone help me with what is wrong in the following query...How to use the max function
    in ABAP program select statement??
    Thanks..
    LOOP AT IT_ANLA_ANLZ INTO WRK_ANLA_ANLZ.
    SELECT KANSW
                FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN ( SELECT MAX(GJAHR) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1
                AND BUKRS = WRK_ANLA_ANLZ-BUKRS .
    APPEND IT_ANLC.
    ENDLOOP.

    Hi,
    You have write your select query once,
    and then loop on the internal table into
    which data will get populated for its display.
    SELECT KANSW
    FROM ANLC INTO TABLE IT_ANLC WHERE GJAHR IN
    ( SELECT MAX(GJAHR) FROM ANLC ) and ANLN1 = WRK_ANLA_ANLZ-ANLN1
    AND BUKRS = WRK_ANLA_ANLZ-BUKRS .
    Then loop at internal table:
    LOOP AT IT_ANLA_ANLZ INTO WRK_ANLA_ANLZ.
    write: / wrk_anla_anlz-fields.
    Endloop.
    Hope it helps
    Regards
    Mansi

Maybe you are looking for