Display last two rows for every group

Hi,
I want to display only last 2 rows for every group.
My table look like
ACN DATE COST APPL
A6ERA 14-JUN 150 B777
A6ERA 14-JUN 180 B787
A6ERA 15-JUN 120 B677
A6ERA 14-JUN 155 B777
A6ERB 13-JUN 166 A777
A6ERB 14-JUN 157 B777
A6ERB 11-JUN 159 B787
A6ERC 14-JUN 099 B777
A6ERC 11-JUN 102 B788
In the above table, I want to display only the last two rows group by ACN
Like the below table
ACN DATE COST APPL
A6ERA 15-JUN 120 B677
A6ERA 14-JUN 155 B777
A6ERB 14-JUN 157 B777
A6ERB 11-JUN 159 B787
A6ERC 14-JUN 099 B777
A6ERC 11-JUN 102 B788
Please reply

Nihar
To get the first two rows from each group, you could do this:
Use the ROW_NUMBER() analytic function (see http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions137.htm#i86310)
Now, your question isn't clear what you mean by "last" two rows. Last in what order? Whatever it is, you can use the ORDER BY part of the ROW_NUMBER() function to specify the ordering, and reverse it using DESC - for example:
select acn, adate, cost, appl from (
    select acn, adate, cost, appl,
              row_number() over (partition by acn order by adate desc) as rn
    from  acntab
where rn <= 2
order by acn, rnHTH
Regards Nigel

Similar Messages

  • Breakable parse lock, two rows for every sid in x$kgllk

    Hi,
    I've got query like that
    select t.* , (select sid from v$session where saddr = t.KGLLKUSE) sid from x$kgllk t where
      2      kglnahsh in (select /*+ no_unnest */ kglnahsh
      3                   from x$kglob
      4                   where
      5                        upper(kglnaown) like upper('USER')
      6                   and  upper(kglnaobj) like upper('PACKAGE')
      7*                 )and I'm getting 2 rows for every session having this lock .
    So why is that:
    I can see difference in output in KGLHDNSP column .
    Is that because Oracle is locking package body and spec separately ?
    DB is 9.2.0.8 .
    Regards
    GregG

    GregG wrote:
    Thanks Jonathan, additionaly I've notice that KGLLKSNM is equal to sid taken from v$session so looks like my scalar subquery is redundant .
    Is that good assumption that KGLLKSNM = SID or just coincidence ?GregG
    That's correct. If fact, if you query v$fixed_view_definitions you'll find that the definiition of gv$open_cursor confirms this:
    select
            inst_id,kgllkuse, kgllksnm, user_name, kglhdpar, kglnahsh, kgllksqlid, kglnaobj 
    from
            x$kgllk
    where
            kglhdnsp = 0
    and     kglhdpar != kgllkhdl
    SQL> desc gv$open_cursor
    Name                                      Null?    Type
    INST_ID                                            NUMBER
    SADDR                                              RAW(4)
    SID                                                NUMBER
    USER_NAME                                          VARCHAR2(30)
    ADDRESS                                            RAW(4)
    HASH_VALUE                                         NUMBER
    SQL_ID                                             VARCHAR2(13)
    SQL_TEXT                                           VARCHAR2(60)Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Author: <b><em>Oracle Core</em></b>

  • Report two rows for a given group item

    All,
    I need to generate a report that will list credits on one line and related debits on the next line. Kind of like the following:
    Account Name_____Amount
    Cash ................. 1000.00 ...... (this line would reflect positive entries, or deposits)
    Cash .................... 20.00 - ...... (this line would reflect the negative entries, or withdrawls)
    Is there an easy way to do something like this?
    We're running the desktop edition of Discoverer 10g.
    Thanks,
    Gary F.

    Hi Rod,
    The plot thickens...
    The report I'm working on will read through a table of invoiced items, much like the following:
    Bill_Charges Table
    Company
    Year
    Month
    Week
    Bill_Code
    Retail_Amount
    Discount_Amount
    Based on the Bill_Code, I need to aggregate the Retail_Amount, and the Discount_Amount.
    The data can look like this:
    ABC Company, 2009,08,01,102,100.00,10
    ABC Company, 2009,08,01,103,90.00,15
    ABC Company, 2009,08,01,104,100.00,5
    ABC Company, 2009,08,01,105,100.00,5
    The report will then need to report two rows for all Bill_Codes in the 100s range, first the Retail_Amount, and then the Discount_Amount directly below it, in the next row, as seen below:
    Company Name . . . . . . .Billed Amounts
    ABC Company................$390.00 (This would be the sum of the Retail_Amount column)
    .................................. $35.00 (this would be the sum of the Discount_Amount column)
    My gut feeling is that this will need to be a pivot table, but I'm so rusty on pivot tables that I'd need more than just a squirt can to limber me up.
    Any help you can throw my way would be greatly appreciated.
    Thanks for all you do for us,
    Gary F.

  • Blank rows for every combination

    I've got a dimension with this hierarchy of levels A | B | C where all of them are a detail of the previous one.
    When I look into the dimension table I get:
    A1
    A2
    A1 B1
    A1 B2
    A1 B1 C1
    A1 B1 C2
    etc.
    If, in Oracle Answers, I pick up attributes from B and C in the same report, then a record is showed for every combination of B and C with blank attributes for C.
    Table B
    B1
    B2
    Table C
    C1
    C2
    C3
    The output in Oracle Answers is:
    B1 C1
    B1 Null
    B1 C2
    B1 Null
    B1 C3
    B1 Null
    and so on ...
    Every row of table B is combined with a row of C and another one with all of its atributtes null.
    Any hint?

    Nihar
    To get the first two rows from each group, you could do this:
    Use the ROW_NUMBER() analytic function (see http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions137.htm#i86310)
    Now, your question isn't clear what you mean by "last" two rows. Last in what order? Whatever it is, you can use the ORDER BY part of the ROW_NUMBER() function to specify the ordering, and reverse it using DESC - for example:
    select acn, adate, cost, appl from (
        select acn, adate, cost, appl,
                  row_number() over (partition by acn order by adate desc) as rn
        from  acntab
    where rn <= 2
    order by acn, rnHTH
    Regards Nigel

  • GridControl show the last two rows in blank

    I'm using JDeveloper 3.2.2 with jdk 1.3 developing a swing
    application.
    I have a form with a gridControl binded to one view of my bc4j
    application module.
    When the number of records from the view excedes the maximum
    number of rows visibles in the area of the gridControl the last
    two rows of the gridControl are showed blank.
    There is no problem when I scroll through the grid control.
    This problem doesn't appear using JDK 1.2.2, provided with
    Jdeveloper, but I would like to use 1.3.1.
    Thanks in advance
    Alex.

    You don't need to do Sum for this, you should use Sum for all rows purpose only..
    $.rawValue = subform_Hidden.sub_SupportTotals.tbl_SupportSubtotals.DataRow[0]. SUBTOTAL +  subform_Hidden.sub_SupportTotals.tbl_SupportSubtotals.DataRow[1].SUBT OTAL

  • How to add a row for every 5 records using logic:iterate

    Hi,
    In my application, using logic:iterate iam displaying 20 records per page.
    I want to insert a row for every 5 records.
    Please tell how to insert that row.
    Thanks in advance.
    Mohan

    I think this could work for you
    for (int x=1 ; x<=20 ; x++) {
    addRowWithRecord
    if (x%5==0)
    addEmptyRow
    }

  • Getting the first row for each group

    Hi Everyone,
    I have a query which returns a number of rows, all of which are valid. What I need to do is to get the first row for each group and work with those records.
    For example ...
    client flight startairport destairport stops
    A fl123 LGW BKK 2
    A fl124 LHR BKK 5
    B fl432 LGW XYZ 7
    B fl432 MAN ABC 8
    .... etc.
    I would need to return one row for Client A and one row for Client B (etc.) but find that I can't use the MIN function because it would return the MIN value for each column (i.e. mix up the rows). I also can use the rownum=1 because this would only return one row rather than one row per group (i.e. per client).
    I have been investigating and most postings seem to say that it needs a second query to look up the first row for each grouping. This is a solution which would not really be practical because my query is already quite complex and incorporating duplicate subqueries would just make the whole thing much to cumbersome.
    So what I really new is a "MIN by group" or a "TOP by group" or a "ROWNUM=1 by group" function.
    Can anyone help me with this? I'm sure that there must be a command to handle this.
    Regards and any thanks,
    Alan Searle
    Cologne, Germany

    Something like this:
    select *
    from (
       select table1.*
       row_number() over (partition by col1, col2 order by col3, col4) rn
       from table1
    where rn = 1In the "partition by" clause you place what you normally would "group by".
    In the "order by" clause you define which will have row_number = 1.
    Edit:
    PS. The [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#i81407]docs have more examples on using analytical functions ;-)
    Edited by: Kim Berg Hansen on Sep 16, 2011 10:46 AM

  • How to get difference between two rows for a column field?

    hi, all,
    Could anyone show me what query statement is to get the difference betweem two rows for two column fields?
    The tables and its records are like this:
    id,      begin,      end
    p1         21          30
    p2          45          60
    p3          120          150
    I would like to have the query result like this
    id,    diff
    p1     15    --- which is 45 minus 30
    p2     60    --- which is 120 minus 60
    and so on...
    thank you in advance.
    Raffy

    You can use the LAG function to access values from previous rows:
    with q as (select 'p1' id, 21 v_start, 30 v_end from dual
    union all
    select 'p2', 45, 60 from dual
    union all
    select 'p3', 120, 150 from dual)
    select id, v_start, v_end, v_start - lag (v_end, 1, 0)
      over (order by id) v_diff from q
    ID,V_START,V_END,V_DIFF
    p1,21,30,21
    p2,45,60,15
    p3,120,150,60
    See the SQL Language doc
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions075.htm

  • Query to find the latest two transactions for each Group

    Hi All,
    Consider the following sets of records in a table test.
    Group---Tran_Dt---SlNo
    c1 10/10/2003 1
    c1 10/10/2003 2
    c1 10/10/2003 3
    c1 11/10/2003 1
    c2 10/10/2003 1
    c2 10/10/2003 2
    c2 11/10/2003 1
    c2 11/10/2003 2
    c2 11/10/2003 3
    c3 10/10/2003 1
    c3 10/10/2003 2
    c3 10/10/2003 3
    I want to list out the latest two transactions for each group irrespective of the slno and trans_dt as below:
    group tran_dt SlNo
    c1 11/10/2003 1
    c1 10/10/2003 3
    c2 11/10/2003 3
    c2 11/10/2003 2
    c3 10/10/2003 3
    c3 10/10/2003 2
    Any help on this would be appreciated.
    Thanks
    Walter Nicholas T

    Hi Walter,
    Please try following query.
    select Group,tran_dt,sino from
    select Group,tran_dt,sino,(DENSE_RANK()OVER (PARTITION BY Group ORDER BY tran_dt desc,sino desc)) rank from test
    ) where rank between 1 and 2
    Thanks,
    Samir

  • Display only one row for distinct columns and with multiple rows their valu

    Hi,
    I have a table having some similar rows for some columns and multiple different rows for some other columns
    i.e
    o_mobile_no o_doc_date o_status d_mobile_no d_doc_date d_status
    9825000111 01-jan-06 'a' 980515464 01-feb-06 c
    9825000111 01-jan-06 'a' 991543154 02-feb-06 d
    9825000111 01-jan-06 'a' 154845545 10-mar-06 a
    What i want is to display only one row for above distinct row along with multiple non distinct colums
    ie
    o_mobile_no o_doc_date o_status d_mobile_no d_doc_date d_status
    9825000111 01-jan-06 'a' 980515464 01-feb-06 c
    991543154 02-feb-06 d
    154845545 10-mar-06 a
    regards,
    Kumar

    Re: SQL Help

  • Input ready query on Multiprovider displays two row for two Infoproviders

    Hello experts,
    We are using below design to develop an input ready query.
                       Input ready query
                           Multiprovider
    Aggregation level 1      Aggegation level 2
    Realtime cube 1            Real time cube 2
    As we are developing Input ready query on multiprovider, it is automatically creating characteristic named 0INFOPROV at multiprovider and query level. 0INFOPROV contains values of Infoproviders on which multiprovider is created.
    char1  cha2 Infoprovider                    key1     Key2
    R1       #       Aggregation level 1        10          #
            P1     Aggregation level 2         #              20
    During planning, we have to plan on separate row for each infoprovider.
    is there any way we can avoid this issue and perform planning on single row for both infoproviders?
    Thanks,
    Mitesh

    Hi,
    Please refer following document.
    [http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/70dcd7b5-6a3d-2d10-c984-e835e37071a2?QuickLink=index&overridelayout=true|http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/70dcd7b5-6a3d-2d10-c984-e835e37071a2?QuickLink=index&overridelayout=true]
    This Documents gives step by step procedure to combine thw rows with # iand with char value nto a single row.
    Regards,
    Geetanjali

  • How To Restrict Number Of Rows For Multiple Group In Report Output

    Hi ,
    I have a requirement to restrict number of rows in report output.I have three different group , if i use same no of rows to restrict then output is as expected but if i want Deduction group should have 7 rows , earning should have 5 rows and Tax group have 3 rows in report output then XML tag is not working.
    Below is the XML tag i am using -
    First i have declare the variable to restrict the rows -
    <xsl:variable name="lpp" select="number(7)"/>
    <xsl:variable name="lpp1" select="number(5)"/>
    <xsl:variable name="lpp2" select="number(3)"/>
    For Each -
    <?for-each:PAYSLIP?>
    <xsl:variable xdofo:ctx="incontext" name="DedLines" select=".//AC_DEDUCTIONS"/>
    <xsl:variable xdofo:ctx="incontext" name="EarLines" select=".//AC_EARNINGS[ELEMENT_CLASSIFICATION!='Taxable Benefits']"/>
    <xsl:variable xdofo:ctx="incontext" name="EarTaxLines" select=".//AC_EARNINGS[ELEMENT_CLASSIFICATION='Taxable Benefits']>
    <?for-each:$DedLines?><?if:(position()-1) mod $lpp=0?> <xsl:variable name="start" xdofo:ctx="incontext" select="position()"/>
    <?if:(position()-1) mod $lpp1=0?><xsl:variable name="start1" xdofo:ctx="incontext" select="position()"/
    <?if:(position()-1) mod $lpp2=0?><xsl:variable name="start2" xdofo:ctx="incontext" select="position()"/>
    Report output is tabular form (one page has two column - Earning and Deduction ) . Tax group comes below earning group.
    Deduction Group -
    <?for-each-group:$DedLines;./REPORTING_NAME?><?if:position()>=$start and position()<$start+$lpp?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Earning Group -
    <?for-each-group:$EarLines;./REPORTING_NAME?><?if:position()>=$start1 and position()<$start1+$lpp1?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Tax Group -
    <?for-each-group:$EarTaxLines;./REPORTING_NAME?><?if:position()>=$start2 and position()<$start2+$lpp2?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Please let me know in case additional detail is require.
    Thanks in Advance.
    Thanks,
    Harsh
    Edited by: Harsh.rkg on Jan 14, 2013 9:43 PM

    variable lpp2 is declare to restrict EarTaxLines -
    <xsl:variable name="lpp2" select="number(2)"/>
    This will help to restrict the no of rows on one page , if we have more then two tax benefits line then layout will roll over to continuation page.
    As part of report output my expectation is if i restrict Earning , Deduction and Tax benefits to same no of line for example - variable lpp ,lpp1 and lpp2 have same value "number(2)" , we can see the layout is continue on next page (restrict every group can have max two lines) .This is the reason we have 4 header grid , deduction and Tax Benefit lines are rolled over to continuation page .But if we restrict different value for each variable then continuation page layout is missing .
    When we tried for <xsl:variable name="lpp2" select="number(3)"/> value continuation page layout is not getting generate for both employee number .

  • How to: Display Last n Rows in a table

    I am currently running Crystal Reports XI and have been tasked with developing a report that displays only the last 30 rows in a table in ascending order (the records are used for showing data trends over time in a line graph). I have created a Command to extract the records, and by using the TOP 30 statement I can get the first 30 rows in proper order; however, in order to get the last 30 rows I have to sort the records in descending order. This gives me the last 30 rows that I need, but the data is in descending order. Is there another way to get the last 30 rows in a table so that the records come out in ascending order?
    The Command I am using looks like this:
    SELECT TOP 30 ROWID FROM MYTABLE ORDER BY ROWID DESC
    (The ROWID field is alpha numeric)

    Hi,
    Create a Group based on Rowid in the order-Ascending. This Group should contain all the fields that you require to print along with Rowid.  Therafter insert Summary like sum,count etc for for any field within the group of the type numbervar(numeric). This would activate the Group Sort Expert in the Report Menu. Click on it and select Bottom N (with N as 30) within nature of Sort and suppy the fieldname as Rowid.
    Hope this solves.....
    Thanks,
    Amogh.

  • BI Report is showing two rows for a single record

    Dear Experts
    This is Raja
    I am Facing one issue in BI Report,
    I have created report based on a Infocube which is getting data from two DSO. one is Standard [ Billing Condition Data]and another one is ZDSO[Targets}. these two requests go in the Infocube
    1. Budget- zdbudget [DSO]
    2.Actuals -0sd_o06 [DSO]
    I need to make report  Like this :For a specific period
    EX:1
    Material    Sold to party    Actual   Budget/Target     Variance    Actual YTD  Budget YTD
    M100        S100                 21         30                         10               21                  30
    here budget{ Budget DSO} values i am getting Material wise sold to party is not coming through file. But for Actual values sold to party is coming, that's why my report is showing like this..
    Ex:2
    Material    Sold to party    Actual   Budget/Target     Variance    Actual YTD  Budget YTD
    M100        S100                 21                                      10               21                 
                                      30                       -30                                        30
    Since the sold to party is not available in Budget DSO in the report For Every Material it's showing Two Rows.
    How can bring this in to one Line..Like EX:1
    Please give Valuable Suggestions..

    Say the data is like this.
    Material Sold to Party Act Bud
    M1          S1               100   0
    M1          #                    0    50
    M1          S2               200   0
    And you want to see like below.
    Material Sold to Party Act Bud
    M1          S1               100   50
    M1          S2               200   50
    If this is your requirement, let the sold to party be in Rows.
    In columns, for budget, create a local selection, drag budget kf, sold to party (restrict this by # and right click and choose constant selection). This makes the budget kf independent of what is coming in the rows and always shows data corresponding to sold to party #. Hope this is clear.

  • DataTable single row coloumn display as two rows..

    I need to display dataTable single Row columns in 2 rows.
    For Eg :
    dataTable each rows has 4 columns...
    by defalut it displays col1 Col2 Col3 Col4.
    But i need to displays
    row1 : Col1 Col2
    Col3 Col4
    row2 : Col1 Col2
    Col3 Col4
    I tried with inner and outer table combination but still having problems.
    Does anyone has any code or sample code for this..
    Thanks in Advance.

    It is hard to follow what you are asking. Do you mean that you want col3 and col4 to show up below col1 and col2 (new row?).
    I am going to assume that you want to "split" or "wrap" your columns. To do this you need to provide the dataTable the proper datamodel.
    <h:dataTable var="foo" model="bar">
    <h:column>
    <h:panelGrid columns="1">
    <h:outputText value="col1">
    <h:outputText value="col3"
    </h:panelGrid>
    </h:column>
    <h:column>
    <h:panelGrid columns="1">
    <h:outputText value="col2">
    <h:outputText value="col4"
    </h:panelGrid>
    </h:column>
    <h:dataTable

Maybe you are looking for

  • Intermittent Problems with Digital Imaging Monitor in Taskbar - J6480

    I am utilizing Vista and have the J6480 wireless all-in-one unit.  On occasion, it takes forever for it to initialize and at times the program becomes non-responsive.  On the last startup, I did not get the "check mark" until about 5 minutes after st

  • Node manager runs on 5559 port instead of 5556

    As I found out recently, even though in Silent.xml NODEMGR_PORT value was "5556", installed as windows service, it runs on 5559. For example, if I specify it in Admin console, I get "Undefined" node manager status. But if I use 5559, I get "Reachable

  • My phone is completely messed up!

    So I downloaded the new facebook update to my phone, and it downloaded successfully, but now it doesn't reboot, it will fill the loading bar halfway, and then it turns off, and starts the process again. Any help on anything I can do to totally reset

  • Can't start up from Install disk

    I'm using a MacBook purchased 3/07. System 10.4.11 I can't start up from the Install Disk for Leopard. I only get a screen with the apple logo and the spinning thing. I wait 10 minutes. Nothing happens. I've tried both using the Disk for the start up

  • ORA-20001: ...English preferences

    Freshly installed HTMLDB on 10g as Companion product. While trying to install Presidential Inaugural Addresses demo, getting this error: ORA-20001: This demonstration can only be installed into an Oracle database which has the English preferences loa