Top - N rows

Hi to all
Sorry for my english can anyone suggest me how to make a select query which has to display Top - N rows and next -N rows without using rownum.
Ex:
Emp table consists of 100 rows and i have to retrieve records each page consisting of 20 and so on till the end of all records.
Regards,
Naresh Kumar Murali

Am working in Forms and Reports Then try to ask your question under Developer Tools forum.
Nicolas.

Similar Messages

  • Group By Select Top 1 Rows

    Hi Guys,
    I tried Group By from Select Top 1 Records,  As below the script wasn't work.
    Please help.
    SELECT TOP 1 NR.SHGId, NR.Amount
    FROM NEW_DCB_REPORT NR
    WHERE NR.SHGId = ND.SHGId
    AND NR.ShgMemberId = ND.ShgMemberId 
    AND NR.LoanNumber = ND.LoanNumber 
    GROUP BY NR.SHGId,NR.Amount
    ORDER BY NR.LedgerNumber DESC
    SHGId
    SHGMemberId
    LoanNumber
    Amount
    Select Top 1 Row
    1028
    147852
    1
    1000
    Select Top 1 Row
    1028
    147853
    1
    2000
    Select Top 1 Row
    1028
    147854
    1
    1000
    Select Top 1 Row
    1028
    147855
    1
    1000
    5000
    I expected the results are:
    SHGId
    Amount
    1028
    5000

    Yes I did it.
    Select  SHGId,
    SUM(Amount)
    amt  FROM
    SELECT  NR.SHGId,
    NR.Amount,
    , Row_number()
    Over(partition
    by NR.SHGId,
    NR.ShgMemberId , NR.LoanNumber
    Order
    by NR.LedgerNumber
    DESC
    ) rn
    FROM NEW_DCB_REPORT NR
    JOIN
     NEW_DCB_REPORT ND
    ON
    NR.SHGId
    =
    ND.SHGId
    AND NR.ShgMemberId
    = ND.ShgMemberId
    AND NR.LoanNumber
    = ND.LoanNumber
    ) t
    Where rn=1
    GROUP
    BY  SHGId
    I just changed above the script Partition by add one more column and
    [yourNDTable]
    Should come NEW_DCB_REPORT table.
    However, It shows below the error ,. Please help.
    Invalid object name 'NEW_DCB_REPORT'.

  • Selecting top five rows

    hi,
    pls I have this issue that needs to be resolved. I need to select top five rows from a table depending on the values of a column stock_value. pls I will appreciate if someone can help me out, Francois pls try and help
    This is the senerio:
    I have a table stock, on the table I have company_name, stock_value, sector.
    Now I want to get top five rows from this table depending on the five highest value of stock_value and for each sector.
    lets try and write a query that will retrive the records after that I will put it in a procedure.
    I know am suppose to ask this on mssql forum but most mssql forums takes time to reply and this is very urgent cos I have to deliver in 45 minutes time
    thank you very much
    jideofor

    hy,
    try out:
    select s.name, sp. sp_sales total_sales
    from salesperson s,
    (select salesperson_id,sum(tot_sales) sp_sales
    RANK() OVER (ORDER BY SUM(tot_SALES) desc) SALES_RANK
    FROM ORDERS
    WHERE YEAR = 2006
    group by salesperson_id) sp
    where sp.sales_rank <= 5 (...n)
    and sp.salesperson_id = s.salesperson_id
    order by sq.sales_rank;
    give result in:
    name total_sales
    a 100
    b 90
    c 80
    d 70
    e 60
    hope help you
    regards

  • Selecting top x rows for each of the groupby clause

    Hi Gurus (again).
    I have this query to select the top 24 rows:
    select * from
    +(+
    select proid, to_char((next_day(trunc(request_date, 'iw'), 'mon')-7), 'YYYY') as YEAR, to_char(request_date, 'IW') as WEEK,
    next_day(trunc(request_date, 'iw'), 'mon')-7 as weekdate,
    trunc(avg(case when report_date > request_date then ((report_date - request_date) * 24) end),1) as avg_ERT_hr,  '24' as ERT_goal,
    trunc(avg(case when response_date > request_date then ((response_date - request_date) * 24) end),1) as avg_IRT_hr,  '48' as IRT_goal,
    trunc(avg(case when deliver_date > request_date then ((deliver_date - request_date)) end),1) as avg_IDT_day,  '0' as IDT_goal,
    trunc(avg(case when complete_date > request_date then ((complete_date - request_date)) end),1) as avg_ORT_day,  '0' as ORT_goal
    from biw_hda_event
    where request_date is not null
    group by proid, to_char((next_day(trunc(request_date, 'iw'), 'mon')-7), 'YYYY'), to_char(request_date, 'IW'), next_day(trunc(request_date, 'IW'), 'MON')-7
    order by 1 desc, 2 desc
    +) where rownum <=24+
    The problem here is that I have a group by "proid" clause in addition to the dates. My purpose is to be able to select the top 24 rows for each of the "proid" that is already grouped by the date. Essentially, the top 24 is the last 24 weeks pertaining to that "proid". How can I do this effectively without creating heaps of UNION clause for each of the "proid"?
    thanks in advance

    I meant to delete this thread, but I do not know how to. Sorry.

  • How to download data into excel by displaying field names in top 2 rows

    Hi experts,
             i am trying to download an internal table data into excel(open office) by reserving top 2 rows. In 1st row i want to display tech.name where as in 2nd row i want to display discription of tech name of the fields. Well, i tried with FM gui_upload and used fieldnames  parameter and even its working too but i'm unable to work with 2nd col.
    Please help me out....
    with regards.
    Praveen.

    Try this code:
    If IT_FINAL is the internal table containing the data that you want to download :
    IF IT_FINAL[] IS INITIAL.
        WRITE : 'No Data Fetched'.
      ELSE.
      Build header table Field catelague
        PERFORM UR_FIELDCAT_BUILD USING G_F_REPNM
                                        'IT_FINAL'
                                 CHANGING IT_FCAT_001.
        DATA: L_F_LINES TYPE I,
              L_F_FROM TYPE I,
              L_F_TO TYPE I,
              L_F_DIFF TYPE I,
              L_TEMP_FINAL LIKE TABLE OF IT_FINAL.
        DESCRIBE TABLE IT_FINAL LINES L_F_LINES.
        IF L_F_LINES > 16000.
          L_F_FROM = 1.
          L_F_TO = 16000.
          DO.
            APPEND LINES OF IT_FINAL FROM L_F_FROM
                                              TO L_F_TO TO L_TEMP_FINAL.
            PERFORM DOWNLOAD_DATA_HEADER TABLES L_TEMP_FINAL
                                                   IT_FCAT_001
                                             USING GF_FILE1.
            IF L_F_TO >= L_F_LINES.
              EXIT.
            ENDIF.
            L_F_FROM = L_F_TO + 1.
            L_F_DIFF = L_F_LINES - L_F_TO.
            IF L_F_DIFF >= 16000.
              L_F_TO = L_F_TO + 16001.
            ELSE.
              L_F_TO = L_F_LINES.
            ENDIF.
            REFRESH: L_TEMP_FINAL.
         ENDDO.
       ELSE.
         PERFORM DOWNLOAD_DATA_HEADER TABLES IT_FINAL
                                         IT_FCAT_001
                                  USING GF_FILE1.
       ENDIF.
    ENDIF.
    FORM UR_FIELDCAT_BUILD
                 USING P_REPID
                       P_TABNAM
    CHANGING C_T_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
       REFRESH C_T_FIELDCAT.
       CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
            EXPORTING
                 I_PROGRAM_NAME         = P_REPID
                 I_INTERNAL_TABNAME     = P_TABNAM
               I_STRUCTURE_NAME       = 'TY_FLODAT'
                 I_INCLNAME             = P_REPID
                 I_BYPASSING_BUFFER     = 'X'
            CHANGING
                 CT_FIELDCAT            = C_T_FIELDCAT
            EXCEPTIONS
                 INCONSISTENT_INTERFACE = 1
                 PROGRAM_ERROR          = 2
                 OTHERS                 = 3.
    ENDFORM.
    FORM DOWNLOAD_DATA_HEADER
                  TABLES T_OUTTAB
                  T_FIELDCAT
                  USING P_FILENAM.
       TYPE-POOLS: SLIS.
       REPLACE 'YYYYMMDD' WITH SY-DATUM INTO P_FILENAM.
       DATA: T_GXXLT_O    TYPE TABLE OF GXXLT_O WITH HEADER LINE,
             T_GXXLT_H    TYPE TABLE OF GXXLT_H WITH HEADER LINE,
             T_GXXLT_H_TEMP TYPE TABLE OF GXXLT_H WITH HEADER LINE,
             T_GXXLT_P    TYPE TABLE OF GXXLT_P WITH HEADER LINE,
             T_GXXLT_S    TYPE TABLE OF GXXLT_S WITH HEADER LINE,
             T_GXXLT_V    TYPE TABLE OF GXXLT_V WITH HEADER LINE,
             L_S_FIELDCAT TYPE SLIS_FIELDCAT_ALV..
       DATA: F_FILE       TYPE GXXLT_F-FILE,
             F_ATT_COLS   TYPE I,
             F_HRZ_KEYS   TYPE I,
             F_VRT_KEYS   TYPE I.
    TYPES: BEGIN OF TY_FLDNM,
               FIELDNAME(30) TYPE C,
            END   OF TY_FLDNM.
    DATA : L_T_FLDNAME TYPE TABLE OF TY_FLDNM,
            L_R_FLDNAME TYPE TY_FLDNM.
    REFRESH:  L_T_FLDNAME.
    LOOP AT T_FIELDCAT INTO L_S_FIELDCAT .
       CLEAR L_R_FLDNAME.
       CONCATENATE  L_S_FIELDCAT-SELTEXT_S  '_'
                    L_S_FIELDCAT-FIELDNAME
        INTO L_R_FLDNAME-FIELDNAME.
       APPEND L_R_FLDNAME TO L_T_FLDNAME.
    ENDLOOP.
    IF T_OUTTAB[] IS INITIAL.
       WRITE : /'No data Fetch !'.
    ELSE.
       DESCRIBE TABLE T_FIELDCAT LINES F_ATT_COLS.
       LOOP AT T_FIELDCAT INTO L_S_FIELDCAT.
         T_GXXLT_H-COL_NO        = L_S_FIELDCAT-COL_POS.
         T_GXXLT_H-ROW_NO        = 1.
         T_GXXLT_H-COL_NAME      = L_S_FIELDCAT-FIELDNAME.
         APPEND T_GXXLT_H.
         CLEAR T_GXXLT_S.
         T_GXXLT_S-COL_NO        = L_S_FIELDCAT-COL_POS.
         T_GXXLT_S-COL_OPS       = 'NOP'.
         T_GXXLT_S-COL_SRC       = L_S_FIELDCAT-COL_POS.
         CASE L_S_FIELDCAT-INTTYPE.
           WHEN 'Z' OR 'P' OR 'I' OR 'F'.
             T_GXXLT_S-COL_TYP   = 'NUM'.
           WHEN 'C' OR 'X' OR 'T' OR 'N'.
             T_GXXLT_S-COL_TYP   = 'STR'.
           WHEN 'D'.
             T_GXXLT_S-COL_TYP   = 'DAT'.
          WHEN OTHERS.
            T_GXXLT_S-COL_TYP   = 'STR'.
        ENDCASE.
        APPEND T_GXXLT_S.
        IF L_S_FIELDCAT-KEY EQ 'X'.
          CLEAR T_GXXLT_H.
          T_GXXLT_H-COL_NO     = L_S_FIELDCAT-COL_POS.
          T_GXXLT_H-ROW_NO     = 1.
          T_GXXLT_H-COL_NAME   = L_S_FIELDCAT-FIELDNAME.
          APPEND T_GXXLT_H.
        ENDIF.
      ENDLOOP.
      MOVE P_FILENAM TO F_FILE.
      LOOP AT T_FIELDCAT INTO L_S_FIELDCAT.
        READ TABLE T_GXXLT_H WITH KEY COL_NAME = L_S_FIELDCAT-FIELDNAME.
        IF SY-SUBRC EQ 0.
          T_GXXLT_H_TEMP-COL_NO = T_GXXLT_H-COL_NO.
          T_GXXLT_H_TEMP-ROW_NO = 2.
          T_GXXLT_H_TEMP-COL_NAME =  L_S_FIELDCAT-SELTEXT_S.
          APPEND T_GXXLT_H_TEMP TO T_GXXLT_H.
          CLEAR T_GXXLT_H.
          ENDIF.
        ENDLOOP.
        CALL FUNCTION 'XXL_FULL_API'
             EXPORTING
                  FILENAME          = F_FILE
                  NO_START          = ''
                  N_ATT_COLS        = F_ATT_COLS
                  N_HRZ_KEYS        = F_HRZ_KEYS
                  N_VRT_KEYS        = F_VRT_KEYS
                  SEMA_TYPE         = ''
                  SO_TITLE          = ''
             TABLES
                  DATA              = T_OUTTAB
                  HKEY              = T_GXXLT_H
                  ONLINE_TEXT       = T_GXXLT_O
                  PRINT_TEXT        = T_GXXLT_P
                  SEMA              = T_GXXLT_S
                  VKEY              = T_GXXLT_V
             EXCEPTIONS
                  CANCELLED_BY_USER = 1
                  DATA_TOO_BIG      = 2
                  DIM_MISMATCH_DATA = 3
                  DIM_MISMATCH_SEMA = 4
                  DIM_MISMATCH_VKEY = 5
                ERROR_IN_HKEY     = 6
                ERROR_IN_SEMA     = 7
                FILE_OPEN_ERROR   = 8
                FILE_WRITE_ERROR  = 9
                INV_DATA_RANGE    = 10
                INV_WINSYS        = 11
                INV_XXL           = 12
                OTHERS            = 13.
      ENDIF.
    ENDFORM.

  • Top 10 rows in a pivot table

    Hello,
    I have an requirement to retrieve top 10 rows from the output pivot table.
    Data on pivot table is structured as follows
    Rows                                                                                  Measures
    "Name" "Grade" "Value North" "Value South" "Total Value"
    ABC A 100 200 300
    BCD B 200 300 500
    In the output there are 50 such rows and I want to display the top 10 rows with max Total Value.
    Is it possible to achieve in OBIEE?
    Any help is appreciated.
    Regards,
    Mukesh
    Edited by: user612929 on May 18, 2010 12:38 AM
    Edited by: user612929 on May 18, 2010 12:39 AM
    Edited by: user612929 on May 18, 2010 12:41 AM

    Thanks or your reply.
    I think I have not explained the issue correctly.
    Source data is like this
    "Name" "Grade" "Region" "Value "
    ABC A North 100
    ABC A South 200
    BCD B North 200
    BCD B South 300
    So there is no Total value column in the report criteria.
    I am using Pivot table to pivot the rows for ABC and BCD into a single row and then using the measure agreegation to generate Total Value column in pivot table only. The filter "is Top 10" available in report criteria so can not be applied on the pivot table.
    Will try to implement the solution provided by BTK.
    Regards,
    Mukesh

  • Top N rows based on aggregate function

    Hi,
    I need to get a report in the following format
    F_A F_B F_C Count_comb
    100 200 300 5
    100 201 300 5
    100 202 301 4
    100 203 111 3
    Here, the Count_comb column gives the count of records which have the same (F_A + F_B + F_C) combination. The requirement is to show the Top-N combinations where the tied rows would have the same rank --that is if (100,200,300) combo and (100,201,300) combo have the same count, they should both be ranked the same.
    I think this involves performing the rank() function on the count(), for which i was not able to write an SQL. Any help in this direction is appreciated.
    Thanks,
    <cfdoubt>
    PS : Pardon the alignment--i hope you can make out what i mean

    Hi Joseph
    Hope this query satisfies your Need
    Select f_a,f_b,f_c,cnt,     dense_rank() over(order by cnt desc) rk
    From (
    select f_a,
         f_b,
         f_c,
         count(*) cnt
    from test
    group by f_a,f_b,f_c
    F_A F_B F_C CNT RK
    100 200 300 5 1
    100 201 300 5 1
    100 202 301 4 2
    100 203 111 3 3
    Regards
    Shajesh Nair
    [email protected]

  • ImageIcons in a JLabel get their top-most row of pixels cut off

    Hello. I've added a "Loading..." screen to my application that basically consists of a JWindow with a background image. However, I've noticed something odd when adding an ImageIcon to a JLabel. It seems that any image I add gets its top row of pixels cut off. The small program below illustrates this.
    import javax.swing.*;
    import java.awt.*;
    public class JLabelTest {
       static final String WINDOW_IMAGE = "my_image.jpg";
        * Create and display the loading screen
       private static void displayLoadingScreen()
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                JWindow winLoadingScreen = new JWindow();
                ImageIcon loadingImg = new ImageIcon(WINDOW_IMAGE);
                JLabel lblImage = new JLabel(loadingImg);
                winLoadingScreen.getContentPane().add(lblImage);
                winLoadingScreen.pack();
                System.out.println("label dimensions:      " + lblImage.getSize().width + " x " + lblImage.getSize().height);
                System.out.println("image icon dimensions: " + loadingImg.getIconWidth() + " x " + loadingImg.getIconHeight());
                winLoadingScreen.setLocation(100, 100);
                winLoadingScreen.setVisible(true);
        * Entry point
        * @param args
       public static void main(String[] args)
          SwingUtilities.invokeLater(new Runnable() {
             public void run()
                displayLoadingScreen();
    }(Obviously, replace my_image.jpg with something else if you'd like to run the code on your own machine.)
    When I run the program with, say, an 800 x 600 resolution image, the output I get is:
    label dimensions:      800 x 599
    image icon dimensions: 800 x 600As you can see, the image gets "shortened" by one pixel when added to the JLabel. If you look at the image that is displayed, you can see that it's the top row that gets cut off. I've tried changing the layout of the JWindow that the JLabel is being added to, but that didn't change anything. I've tried using a number of different images in varying formats, and the same thing happens to all of them.
    Any ideas? Thanks in advance.

    Have you tried to set label's preferredSize manually?
    What happens when you add empty border t the label with some insets?
    regards,
    Stas

  • How to put title or labels on top of row headding of Cross Tab

    Hello..
                 I created one cross tab.I put labels in the top row,these labels are coming only in first page only,I tried repeat row label also,But not working....I need all the labels in all pages..

    Hi,
    In XI r3 - on a  cross-tab table under the page layout properties there is a  "Repeat header on every page" option. Is this checked?
    Regards
    Alan

  • Top 3 rows

    emp table :
    emp_id salary
    101 100000
    102 90000
    103 90000
    104 80000
    105 65000
    106 75000
    1] how to retrieve the highest top 3 salaries [i.e. 100000, 90000, 80000]
    2] If i want to display the o/p with 2 columns : salary & rank , how should i write the query ? [i.e for salary 100000, the rank should be 1, for salary 90000, the rank should be 2...and so on.]
    3] query to display the highest top 3 salaries, o/p should be emp_id & salary..

    user10412263 wrote:
    emp table :
    emp_id salary
    101 100000
    102 90000
    103 90000
    104 80000
    105 65000
    106 75000
    1] how to retrieve the highest top 3 salaries [i.e. 100000, 90000, 80000]
    2] If i want to display the o/p with 2 columns : salary & rank , how should i write the query ? [i.e for salary 100000, the rank should be 1, for salary 90000, the rank should be 2...and so on.]
    3] query to display the highest top 3 salaries, o/p should be emp_id & salary..Have you attempted your homework at all?
    Come back when you have, show us what you've tried and where you're stuck.

  • Select top 10 rows for each product

    Hi,
    I have to build a query in the following way:
    I have a query like,
    select dept, product, product_sold
    from products
    order by 1, 2 desc
    But I have a requirement, not to show all the products in dept . I need to show only first 10 rows of each 'dept' column.
    Please help me in acheiving this.

    You would do something like this (untested):
    select dept
    , product
    , product_sold
    from (
       select dept
       , product
       , product_sold
       , row_number() over (
          partition by dept
          order by product
          , product_sold
       ) rn
       from products
    where rn <= 10;Assuming that I have correctly guessed the ordering you require...
    cheers,
    Anthony

  • Top N rows with group by

    Greetings,
    VERSION :- Oracle8i Enterprise Edition Release 8.1.7.4.0 - 64bit Production
    I have table test with following structure
    customer_id  called_number  call_cnt 
    10052         98256564666    52       
    10025         5985565652     30
    10052         655465464645   60
    10025         54654646465    40
    10052         56546546646    58
    10025         87422121212    35
    I want top 3 called_number based on  per call_cnt FOR EACH CUSTOMER (GROUP BY customer_id)
    Expected output:-
    10052         655465464645   60
    10052         56546546646    58
    10052         98256564666    52
    10025         54654646465    40
    10025         87422121212    35
    10025         5985565652     30
    I want top 3 called_number as per call_cnt
    KINDLY HELP
    Thanks

    i don't remember about row_number function in 8i, but you can check it :
    with t(customer_id ,called_number ,call_cnt) as (
    select 10052,         '98256564666',    52 from dual union all
    select 10025,         '5985565652',   30 from dual union all
    select 10025,         '59855656521',   40 from dual union all
    select 10052,         '655465464645',   60 from dual union all
    select 10052,         '655465464641',   10 from dual union all
    select 10025,         '54654646465',    40 from dual union all
    select 10052,         '56546546646',    58 from dual union all
    select 10025,         '87422121212',    35 from dual )
    select q.* from (
    select t.*, row_number() over (partition by customer_id order by call_cnt desc) rn from t
    ) q
    where q.rn <= 3
    Ramin Hashimzadeh

  • Selecting top 10 rows based on one column.

    Hi,
    I need to display the top 10 records for each distinct value of a column. 

    USE Northwind;
    -- Solution 1
    SELECT S.SupplierID, S.CompanyName, CA.ProductID, CA.UnitPrice
    FROM dbo.Suppliers AS S
      CROSS APPLY
        (SELECT TOP (10) *
         FROM dbo.Products AS P
         WHERE P.SupplierID = S.SupplierID
         ORDER BY UnitPrice DESC, ProductID DESC) AS CA
    ORDER BY S.SupplierID, CA.UnitPrice DESC, CA.ProductID DESC;
    -- Solution 2
    WITH C AS
      SELECT S.SupplierID, S.CompanyName, P.ProductID, P.UnitPrice,
        ROW_NUMBER() OVER(
          PARTITION BY P.SupplierID
          ORDER BY P.UnitPrice DESC, P.ProductID DESC) AS RowNum
      FROM dbo.Suppliers AS S
        JOIN dbo.Products AS P
          ON P.SupplierID = S.SupplierID
    SELECT SupplierID, CompanyName, ProductID, UnitPrice
    FROM C
    WHERE RowNum <= 10
    ORDER BY SupplierID, ProductID DESC, UnitPrice DESC;
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to get top most rows col into a vriable.

    hi,
    i want to get the smallest
    "Tid" greater than v_Id1
    into v_b
    which method i should use both are working.
    1)
    SELECT *
    INTO v_b
    FROM ( SELECT TId
    FROM tt_T
    WHERE TId > v_Id1
    ORDER BY TId )
    WHERE ROWNUM <= 1;
    2)
    SELECT *
    INTO v_b
    FROM (SELECT Tid
    FROM ( SELECT TId
    FROM tt_T
    WHERE TId > v_Id1
    ORDER BY TId )
    WHERE ROWNUM <= 1);
    yours sinicerely
    Edited by: 944768 on Apr 28, 2013 9:35 AM

    Hi,
    944768 wrote:
    i wanted to know about the correctness of my sql.
    so please tel me which one is correct . or both are correct.You said both give the results you want, so both are correct.
    The ways Solomon posted are also correct, and they are better, because they are faster and/or easier to maintain.
    and specially about one extra bracket which i have in my 2) stmt.What do you see as the advantages and disadvantages of the 2 ways you posted?
    If 2) is better than 1), wouldn't this be even better han 2)?
    SELECT  *
    INTO      v_b
    FROM      (
             SELECT  TId
             FROM    (
                          SELECT    TId
                   FROM       (
                              SELECT  TId
                              FROM    (
                                             SELECT    TId
                                    FROM         tt_T
                                    WHERE         TId > v_Id1
                                    ORDER BY  TId
                                  WHERE   ROWNUM <= 1
    ;Why stop at 4 sub-queries?

  • How to show 10 bottom rows instead of showing 10 top rows of table as defau

    A table was created with JTable class and JScrollPane class. If you run the following code, it always shows you the top 10 rows, the scrollbar stays at the right-top by default. However, what I want the program to show is the bottom 10 rows (The table has 30 rows totally) and the scrollbar should stay at the right-bottom as default. I should greatly appreciate it if anybody could tell me how to implement this scenario.
    import javax.swing.JTable;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JComponent;
    import java.awt.*;
    import java.awt.event.*;
    //import java.lang.string.*;
    public class SimpleTableDemo extends JFrame {
    private boolean DEBUG = true;
    public SimpleTableDemo() {
    super("SimpleTableDemo");
    Object[][] data = {
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Mary", "Campione",
    "Snowboarding", new Integer(5), new Boolean(false)},
    {"Alison", "Huml",
    "Rowing", new Integer(3), new Boolean(true)},
    {"Kathy", "Walrath",
    "Chasing toddlers", new Integer(2), new Boolean(false)},
    {"Mark", "Andrews",
    "Speed reading", new Integer(20), new Boolean(true)},
    {"Angela", "Lih",
    "Teaching high school", new Integer(4), new Boolean(false)}
    String[] columnNames = {"First Name",
    "Last Name",
    "Sport",
    "# of Years",
    "Vegetarian"};
    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(650, 150));
    if (DEBUG) {
    table.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    printDebugData(table);
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);
    //Add the scroll pane to this window.
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    private void printDebugData(JTable table) {
    int numRows = table.getRowCount();
    int numCols = table.getColumnCount();
    javax.swing.table.TableModel model = table.getModel();
    System.out.println("Value of data: ");
    for (int i=0; i < numRows; i++) {
    System.out.print(" row " + i + ":");
    for (int j=0; j < numCols; j++) {
    System.out.print(" " + model.getValueAt(i, j));
    System.out.println();
    System.out.println("--------------------------");
    public static void main(String[] args) {
    SimpleTableDemo frame = new SimpleTableDemo();
    frame.pack();
    frame.setVisible(true);

    Dimension d = table.getSize();
    scrollpane.getViewport().setViewPosition( new Point( 0,
              (int)d.getHeight() ) );This sets it to the end of the table, you might need to edit it to do as you require.
    Mike

Maybe you are looking for

  • My ipod mini won't update the songs anymore.

    After updating the software per a prompt when I plugged in, my ipod won't transfer the songs over form itunes. I have done a lot to try to fix this problem. What was happening is that when I connected the ipod (with the same USB that had always worke

  • How do I create a check box?

    Hey there, I want to create a check box beside a text, like "keep me logged in". Anyone who can explain me how to do that? Thanks, alewei

  • DVD-RW can't burn

    Hello. I have a problem with my dvd-rw. I have HP Probook 4525s. When start burning a dvd, at 2% progress stop and receive error "Burning Process Failed". I can't burn files to disc. I have windows 8, any solutions? I use nero for burning, but i try

  • Cancel 3rd party service error message

    Signed up for 7 day free trial of Mobile TV and have been trying to cancel it before trial period is over. When I try to access Mobile TV under my listed purchases I continue to get Error 500 message telling me I cannot access at this time. I called

  • Help about text frame

    Hi,everybody, I create a text frame in indesign and there are some texts, tables and pictures in it. I want to write some codes to treat each item in the text frame, can anybody tell me how to do that? especially to get the picture item in the text f