Row_number() issue - exponentially increases the record count

Hello all -
I'm trying to use the row_number to split the amount by the number of months between the dates and create one new row for the each month.
The use of row_number() is causing the data to be exponentially increase based -i.e currently there are 3 distinct records and each record should only return 2 rows but the this returns 12 rows and so on if the data increases which is causing a performance issue . I could put the entire query below again as a derived table and add a where condition as where rn < = tot_months but I just want to know if there is any way to limit the rows to the number of months reason being - for 48 distinct rows it is creating apprx 12000 rows and the performance has become a killer. You can try by adding more rows and see what I mean especially when the span between months is more -
Any help is greatly appreciated -
WITH testdata AS
(SELECT 1 id, 1001 acct_no, 2001 cnt_id, to_date('01/17/2007','mm/dd/yyyy') start_date, to_date('03/16/2007', 'mm/dd/yyyy') end_date, 8000 amount
FROM dual
UNION ALL
SELECT 2 id, 1002 acct_no, 2002 cnt_id, to_date('01/18/2007','mm/dd/yyyy') start_date, to_date('03/19/2007', 'mm/dd/yyyy') end_date, 90000 amount
FROM dual
UNION ALL
SELECT 3 id, 1003 acct_no, 2003 cnt_id ,to_date('02/08/2007','mm/dd/yyyy') start_date, to_date('04/11/2007', 'mm/dd/yyyy') end_date, 125000 amount
FROM dual)
SELECT DISTINCT id,
acct_no,
cnt_id,
start_date,
end_date,
amount,
row_number() over(PARTITION BY id , start_date ORDER BY cnt_id) rn,
ROUND(months_between(end_date, start_date)) tot_months
FROM
(SELECT id,
acct_no,
cnt_id,
start_date,
end_date,
amount
FROM testdata t
where ROUND(months_between(end_date, start_date)) > 1 )
t CONNECT BY LEVEL <= ROUND(months_between(end_date, start_date))
order by acct_no,cnt_id, start_date
Thanks.

Then why is the query returning multiple rows instead
of 2 per account. Because you have in no way specified that the query must return 2 rows per account. You need to fully understand the implications of what you are doing.
CONNECT BY <= SOME_INTEGER is not just a generic method of "multiplying" rows by an integer value. True, it works in certain circumstances when used in a certain way, but there is still debate about whether it is even a valid SQL construct. See Multi-Rows from DUAL (and many others) for different approaches to generating rows.
But let's understand exactly what your query is doing. In essence, you are doing something similar to the following,where the ID signifies the number of months between start_date and end_date in your example:
  1  with testdata as (
  2     select 1 id from dual union all
  3     select 2 id from dual
  4  )
  5  select lpad(t.id, 2*level, ' ') as id
  6  , level
  7  from testdata t
  8* connect by level <= t.id
[email protected](162)> /
ID              LEVEL
1                  1
   2                2
2                  1
   2                2I've included the LEVEL pseudocolumn in the output and formatted by the depth of the tree so we can better see what's going on. The hierarchical query is processed in the following order:
1. Select the root rows of the hierarchy. In our case this includes all rows in the test table. So, for the both ID = 1 and ID = 2, we descend to LEVEL = 1, i.e. the root level, and we return both rows with LEVEL = 1.
2. Increment the LEVEL pseudocolumn and select the child rows of each root row, i.e. those rows which satisfy the CONNECT BY condition.
So we increment LEVEL to 2. For the both the rows, we descend to the CONNECT BY condition LEVEL = 2. Only ID = 2 satisfies the LEVEL <= ID condition, so this row is returned as a child of both ID = 1 and ID = 2.
So we see, the row with ID = 2 is both the parent AND the child for one of the returned rows. Note that this is, strictly speaking, a LOOP in the data and should raise the ORA-01436: CONNECT BY loop in user data error, but it does not. In my opinion this is a BUG, hence the "debate" mentioned above..
3. We return the rows with parents followed directly by their children.
So, in your general case of:
with t as (
   select N mnths -- average no of months
   from T -- T is a table with R rows
select *
from t
connect by level <= t.mnths, you'll return:
- R rows for level = 1
- R*R rows for level = 2
- R*R*R rows for level = 3
So your general number of rows is Rtot = R + R^2 + R^3 + ... + R^N
For 48 rows with 2 months per row, this gives 48 + 48^2 = 2352 rows.
For 48 rows with 3 months per row, this gives 48 + 48^2 + 48^3 = 112944 rows.
  1  with testdata as (
  2     select 3 id from dual union all
48     select 3 id from dual union all
49     select 3 id from dual
50  )
51  select lpad(t.id, 2*level, ' ') as id
52  , level
53  from testdata t
54* connect by level <= t.id
[email protected](162)> /
112944 rows selected.So you can see, your number of rows returned will grow extemely quickly if you have 3 or more months between start date and end date.
cheers,
Anthony

Similar Messages

  • Issue while Creating the records in OAF by disabling one field.

    Hi Experts,
    I am having the scenario like this.
    From the OAF page I want to create the record by omitting one field(it is disable for OAF).
    Consider the folowing scenario..
    I am having the following fields Empno,Empname,Salary,Job in the OAF page where I made the Job field alone as disable and the style i am using for job field is message text input and i have set the initial value as 'Manger'.
    I am facing issue while creating the records in OAF page without the job filed value ie 'Manager' then the records were not inserted into my tables. Whereas if I enable the job field in OAF page(meaning I gave the job as 'Manager' in OAF page) I am able to create the records from OAF page and able get inserted in the database.
    Could Anyone can suggest where I fail as it is my Priority issue.
    Any Suggestion will be a great help for me.
    Thanks,
    Murugesh.

    or you can default it in the controller by handling add row event
    if(vo.hasNext())
    vorow = vo.next();
    vorow.setAttribute("xxxx",3838);
    --Prasanna                                                                                                                                                                                                                                                                                                                                   

  • What event should I use to grab the record count of a DataGrid?

    I have a "Go" button on a search form.  It works correctly, fetching records from a CF data service into a data bound DataGrid.  However, using the code shown below, the record count always shows zero.  I've tried putting the "..Provider.length" line shown below, into various events on the grid, so that it will always update the count at the correct time, but I get "null" object errors at run time. There seems to be no event that fires "AfterDataLoad".
                protected function btnGo_clickHandler(event:MouseEvent):void
                     getSBJsResult.token = baa_data_svc.getSBJs(cmbSrch.text);
                     tabSbjs.label = 'Subjects: ' + grdSBJs.dataProvider.length;    //  This always returns zero.  Why?

    For example, this doesn't work.  The grid is told to loaded data on line 1, but this must just be only a message send to the Flash Player 10, because the processing doesn't stop and wait for the loading of data to complete, it just continues on to line 2, at which point '...Provider.length' is always zero the first time through.   So... the NEXT time the user clicks the Go button, line 2 is processed for the DataGrid which is holding data from the PREVIOUS search, so it updates the tabSbjs.label with the count from the previous search, not the current search.  The count of records is always for the previous data set.
                protected function btnGo_clickHandler(event:MouseEvent):void
    1               getSBJsResult.token = baa_data_svc.getSBJs(cmbSrch.text);  // edtSrch.text
    2               tabSbjs.label = 'Subjects: ' + grdSBJs.dataProvider.length;                                                                            
    All I'm trying to do is (which seems like a very difficult thing to do in FB4):
    -Display a DataGrid popluated with data records
    -Display a count of how many records are in the data grid
    If I rely on CF9 functions, which can only return a single value, then I have to make two CF9  functions, with the exact same SQL, one that returns data, and one that returns a count of the records.... which is non-sensicle.
    There must be a simple way to do this, but I'm going around in circles.

  • Getting the record count from result set

    i'm retreiving the result set using the executeQuery method, now i want to know how many records are there in the result set, that is the record count of the result set.
    one solution to that is to first use the executeUpdate and then use the executeQuery but i think that is not the right way.
    so please tell me is there any method in jdbc to get that thing done
    Tanx

    Hi
    Do you know if your DB supports "insensitive scrolling"?
    SQL generally do, but some don't - I had the same problem with the
    open source version of Interbase from Phoenix...
    Anyway - try creating your statement this way:
    public Statement createStatement(int resultSetType, int resultSetConcurrency)
    throws SQLException
    ...where resultset type should be:
    ResultSet.TYPE_SCROLL_INSENSITIVE
    Then you can do this:
    ResultSet rs = stm.executeQuery(q);
    int size = rs.last(); //this what you looking for?
    rs.beforeFirst();
    while(rs.next()){
    }

  • How to caculate the records count for a measurement with a filter?

    I have a fact table which add relationship to a datetime dimension. I create a measurement based on the fact table, like [MeasureA]. I want to calculate the record counts of [measureA] of today, how to write the mdx query? Thanks!

    Hi Lavandula,
    According to your description, you want to calculate the numbers of the record of a measure for a specific day, right? In this case, you can use
    Count function which returns the number of cells in a set to achieve you requirement. Here is a sample query on Adventure Works for your reference.
    WITH
    MEMBER [Measures].[SetCount] AS
    COUNT([Product].[Product].[Product].Members)
    SELECT
    {[Measures].[SetCount]} ON COLUMNS
    FROM
    [Adventure Works]
    Besides, if you want to count the cell numbers with a condition, you can use
    Filter funtion inside the Count function.
    WITH MEMBER [Measures].[High Volume Products Count] AS
    Count(Filter([Product].[Product].[Product], [Measures].[Internet Order Quantity] > 5))
    SELECT [Customer].[Customer Geography].[Country] ON 0
    , [Date].[Calendar].[Date].MEMBERS ON 1
    FROM [Adventure Works]
    WHERE [Measures].[High Volume Products Count]
    Regards,
    Charlie Liao
    TechNet Community Support

  • How to increase the thread count for JDBC adapter

    Hi ALL,
                Could someone advice me on how to increase the thread count for JDBC Adapter in Visual Admin:
    JDBC_http://sap.com/xi/XI/System.Call.maxConsumers 5
    JDBC_http://sap.com/xi/XI/System.Recv.maxConsumers 5
    JDBC_http://sap.com/xi/XI/System.Rqst.maxConsumers 5
    JDBC_http://sap.com/xi/XI/System.Send.maxConsumers 5
    I need to increase them to 10.
    Regards,
    Xier

    hi check the below links:
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/806e75a0-0e01-0010-2587-fc518de8ac1a
    admin manual:
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/fdb09490-0201-0010-e09e-a76388646ad0
    note:reward points if solution found helpfull.....
    regards
    chandrakanth.k

  • How to return the record count of a query to a number variable

    How would I get the record count from a query without using a cursor to step through the query and count the records individually?
    query: select * from table1 where column1 = column2
    How would I get the record count of the query above?
    thanks,
    michelle

    I figured it out
    select count(*) into theCount
    from ( select * from table1 where column1=column2 );
    thanks,
    michelle

  • Get the record count

    The Query below is part of a huge query. The snipet is just the part that I need to make a change.
    For the query below, how can I get the recordcount in the select inside the case statement? I want to only output the r_text when the record count of the select is zero (0) else make the output to r_text null
    select
    distinct a.style_id, b.sty, b.season, a.cust, a.ad_d, b.code,
    (case when b.code = 'MAKE' then
    (select min(t.value) from sn.age r, sn.text t
    where r.t_id = '18' and r.name = 'MTGNSL'
         and r.t_id = t.t_id
         and r.name = t.name and r.text_id = t.text_id
         and r.id_1 = a.id
    ) end) r_text,
    (case when b.code is null then 'Blank Code'
    when b.code = 'MAKE' then 'MAKE' end) err
         

    If you are not using a scrollable result set then the following is the best way to do it.
    there are several key words in SQL that can be used, the one you are interested in is count();
    so if your query at the moment is
    "select col1, col2, col3 from my_table where col2=? and col3=?"you can work out how many rows will be returned by executing this command first
    "select count(col1) from my_table where col2=? and col3=?"this will return a result set with one row and one column, you can get the row count as follows:
    ResultSet rs = ps.executeQuery();
    int rowCount = rs.getInt(1);I hope this helps :)

  • How to get the record count printed for the report in the Dashboards

    Hi,
    I would like to get the record count printed at the bottom of every dashboard report like:
    < 1 - 25 of 6300 > instead of < 1 - 25 >
    Any help is appreciated
    Regards
    B

    I have tried the following formula which identifies the lowest grain but it does not seem to give me correct result.. I am not getting the correct count. I am getting as 3.A work order can be updated only once at one point of time.hence the formula
    MAX(RCOUNT(CAST("SR Wo Fact"."Crm Wo Number" AS CHAR) ||CAST("SR Wo Fact"."PSC Timestamp" as char)))
    I tried only MAX(RCOUNT(1)), but I was able to get the record count as corect for Administrator but not for other users.
    Has anyone come across this scenario.
    Thanks Shravan
    Edited by: 786443 on Aug 19, 2010 10:22 AM

  • Get the record count from a query

    Hi,
    does anyone know how to get the record count from a sql query?
    e.g: I've got a ResultSet from the Statement.excuteQuery(), and I want to know how many records this ResultSet contains, how to get this?
    I'd read thoughout the documents of Statement and ResultSet, but couldn't find a solution, do I have to use another seperate query such as "select count(*)" to do this?
    thanks.
    Yang Liu

    If you are not using a scrollable result set then the following is the best way to do it.
    there are several key words in SQL that can be used, the one you are interested in is count();
    so if your query at the moment is
    "select col1, col2, col3 from my_table where col2=? and col3=?"you can work out how many rows will be returned by executing this command first
    "select count(col1) from my_table where col2=? and col3=?"this will return a result set with one row and one column, you can get the row count as follows:
    ResultSet rs = ps.executeQuery();
    int rowCount = rs.getInt(1);I hope this helps :)

  • How can I increase the pixel count (tolerance) that will invoke sizing handles to make them easier to select and use?

    When working with Word tables, re sizing a column's width requires placing the mouse exactly on the line, waiting for the mouse pointer to change to the sizing control, and then you click and drag.  The problem is that the tolerance is so unforgiving
    (a few pixels one way or the other) that is rather difficult to get and keep the mouse in exactly the right spot to invoke the sizing handle.  Because the tolerance is so narrow, by the time you click to drag, the handle control often reverts to a regular
    mouse pointer because you moved the mouse a pixel, and instead you find yourself highlighting cell content instead of dragging the column width.  And you have to keep repeating this process over and over trial-and-error fashion until you finally get the
    sizing handle to display long enough to actually invoke it when clicking.   It is rather frustrating.  My question is this:  Is there a way to increase the tolerance to invoke a sizing handle?  In other words, increase the pixel count slightly,
    either side of the line, that will invoke the control for the sizing handle?   Instead of a few pixels, to something much more realistic/functional, like maybe 5 to 7 pixels either side of the line.  This is also a problem when dealing with columns
    in Windows Explorer - you find yourself dragging a column instead of re-sizing it because by the time you click the mouse, the sizing control has reverted to a regular mouse pointer - this has long been a source of wasted time and frustration to me.  I'm
    hoping there might be a way to change this in the Windows registry.  Thank you.   

    Cool article, but not relevant.
    I did not import from iPhoto nor Aperture.
    I have my photos as JPEG files in a folder on my hard drive.
    Photos, the app, did not make any duplicates. Rather, it made a giant Resources folder, almost as big as my folder of Image files.
    FInder Info confirms the size increase and lost capacity on my hard drive.
    But I appreciate the link.  That could certainly give someone the same impression.

  • How to increase the thread count in jive forum RSS feed

    Dear All,
    We are using Enterprise portal and we have installed jive forums sp14, we have a requirement of reading forums rss feeds and integrating in to a web page, currently we are doing that but the problem is rss feed in forums has maximum feed of 10, and we don't have the option of giving the user to read entire RSS feed of a particular forum.
    i want increase the RSS feed count how can i do this.
    Please help
    Regards
    Aflal

    Hi Aflal,
    Try this link & see whether it has a solutionr to our issue
    http://docs.jivesoftware.com/latest/documentation/rss.html
    Cheers
    Sukitha

  • How to implement the record count on the page

    Dear,
    I have a basic search page, results region with a simple table.
    I need to show the total number of records returned by a search query.
    Suppose I create messageStyledText "counter" bean to show the result.
    The result will be calculated in the ProcessFormRequest, how would I set the value of the the "counter" bean then? I can not do that in PFR.
    Thank you
    Anatoliy

    In the PFR method of controller, get the row count of VO when user click on search button and then set its value in the counter bean.
    Here the dummy code.. check syntax error & other compile time error
    if(pageContext.getParameter("SearchBtn") != null)
    OAViewObject vo = (OAViewObject)am.findViewObject("SearchVO1")
    if (vo!= null)
    String fetchedRowCount = vo.getFetchedRowCount();
    OAMessageStyleTextBean msb = (OAMessageStyleTextBean)webBean.findChildRecursive("CounterBean");
    if(msb != null)
    msb.setvalue(pageContext, fetchedRowCount);
    }Thanks
    --Anil
    http://oracleanil.blogspot.com

  • Goods Issue to increase the stock levels in a plant

    Folks,
    I am not a MM guy and I am trying to increase the stock levels in a plant using a Goods issue.
    1.     Can I do the Goods Issues without reference to a Purchase Order? If so, how can I do this
    2.     What are the relevant movement types when GI is made w/o reference to a Purchase Order?
    3.     Can GI be made with reference to any other entity/object other than PO?
    Thanks

    1. Can I do the Goods Issues without reference to a Purchase Order? If so, how can I do this
    No u can't ..u have to do Goods receipt for a PO
    Raise a PO for the same material and do GR
    2. What are the relevant movement types when GI is made w/o reference to a Purchase Order?
    u cant do GI for PO
    3. Can GI be made with reference to any other entity/object other than PO?
    GI aganist cost centre, production order, reservations ....
    the easier way to bring in stock of a material is to do intial entry thru MB1C with mvt type 561.

  • Increase the resultset count in SSXA application

    Hi,
    I'm using UCM 11g + Site Studio, I'm coding in JSTL for my web application. I am calling many services like GET_SEARCH_RESULT etc. All these services are returning a resultset of 20 values.
    I have changed the setting by clicking the weblogic user in UCM product to 50, then it was showing 50 results in UCM for only 'weblogic' user but it doesn't work for all users. Also, it doesn't work for my SSXA site.
    Is there any pointer to set the resultset with more than 20 values?
    Yes, we can set a parameter in each of the service call but I am looking for permanent solution for my existing application.
    Thanks in advance,
    MAK

    thanks buddy,
    I got a definition of MaxResult in bex huff's book, which states:
    MaxResults: Maximum number of results that are returned for a Verity search. The search results page will never show more than this number. The default is 200, and you should use caution when increasing this number.....
    But I was getting 20 results in the result count for GET_SEARCH_RESULT, which was my concern. Now I have set a service parameter "ResultCount" as "50". This is giving 50 results at a time.
    One more thing, custom service is not bounded with restriction of 20 results.
    Thanks,
    MAK

Maybe you are looking for