Getting a fixed number of rows from various items

Hello,
I am new to Oracle and I am having a hard time resolving this problem. I will try to explain as best as possible.
The application requires that I build a list of items that have been shipped. As they may have various shipping companies, there may be various tracking numbers for each item shipped. The application requires that I get a list of the items that have been shipped that meet certain criteria, then for each item, list the first three tracking numbers that are found. If the shipped item has more than three tracking numbers, the others beyond the third are ignored. I built a SELECT statement using the rownum "column" to limit my result set to a maximum of three. My intention was to build a list of
item ids, then pass those to a stored procedure, then loop for each item id, getting the first three tracking numbers. The tracking numbers returned would be appended to a cursor, which would return the entire set of tracking numbers for the list of item ids.
Any recommendations as to how to approach this requirement would be greatly appreciated. I have gotten through the SELECT that returns the list of item ids. I am having trouble finding the correct approach to getting the three tracking numbers for each item id. One solution was to have the application cycle through each
item id, but I am hoping that this can be done within the stored procedure so that it is called only once for the returned list of item ids.
I wish to thank you in advance. This is all the information I can provide, as I don't have any DDL statements. I hope it will suffice.
Salvador
null

Please see if you can use something like this:
SELECT a.item, a.tracking_number
FROM table_name a
WHERE 3 >=
(SELECT COUNT (*) + 1
FROM table_name b
WHERE a.item = b.item
AND b.ROWID < a.ROWID
AND b.criteria = 'whatever')
AND a.criteria = 'whatever'
ORDER BY a.item, a.tracking_number
For example, if you have a table named items_shipped that has columns named item, tracking_num, and shipped, and you want all the items that meet the criteria that shipped = 'Y' and the first three tracking_nums for those items, then your query would look something like this:
SELECT a.item, a.tracking_num
FROM items_shipped a
WHERE 3 >=
(SELECT COUNT (*) + 1
FROM items_shipped b
WHERE a.item = b.item
AND b.ROWID < a.ROWID
AND b.shipped = 'Y')
AND a.shipped = 'Y'
ORDER BY a.item, a.tracking_num
You have given very limited information. If the above suggestions are not sufficient, please provide your table structure, including table names, column names, data types and lengths, some sample data, what criteria must be met, and a sample of what you would like the output to be.
null

Similar Messages

  • How can I get a fixed number of rows on a SELECT?

    I'm interested on get the last xx records of one specific query that returns an higher number of rows than xx. How can I do this?

    you can use "where rownum < xx" if you
    write "select * from (select ... from ... order by ...) where rownum < 10"
    but you can't write "rownum > 10" because no record will be returned.
    therefor you have to do like this:
    select * from (select ..., rownum as nummer from (select ... from ... order by ...)) where nummer > 10
    ~
    pascal

  • Getting a specified number of rows froma query

    How can I get the minimum 3 row from a query after a order by in the select
    Eg;
    SELECT ROWNUM,ROWID, connection_id, GROUP_ID
    FROM prov_pending_commands
    ORDER BY group_id
    4     AAAZWTAAYAAANMiAAA     680932     32702947
    3      AAAZWTAAYAAANMgAAB     644610     32703643
    2 AAAZWTAAYAAANLrAAB     51925942 32704602
    1 AAAZWTAAYAAANLrAAA     61247803 32704613
    I need to get only the group ids 32702947,32703643, 32704602 for some other processing.
    I checked by getting rownum or row id, but after ordering it gives wrong data
    Please need help

    Hi,
    Try this,
    select * from (select name, row_number() over (order by name) as row_num from test) row_num
    where row_num <= 3
    If you are looking for something else explain us more with sample output.
    Thanks!
    M T

  • Resukt:Get number of rows from 3 different table...?

    Hi All,
    I need to find out number of rows from 3 different table for the same conditions. Instead of writing 3 queries, is it possible to get it thru one query?
    For example, i need to find out number of rows in tables where name = 'Ameet' from 3 different table, i will end writing 3 queries.
    1. select count(1) from table_a where name = 'Ameet';
    1. select count(1) from table_b where name = 'Ameet';
    1. select count(1) from table_c where name = 'Ameet';
    Is it possible to write a single query to get result of all above 3 queries?
    result:
    table_a table_b table_c
    34 44 2

    One way:
    SELECT      (SELECT     COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet') AS TABLE_A
    ,     (SELECT     COUNT(*) AS CNT FROM TABLE_B WHERE name='Ameet') AS TABLE_B
    ,     (SELECT     COUNT(*) AS CNT FROM TABLE_C WHERE name='Ameet') AS TABLE_C
    FROM DUALIf you want the total:
    SELECT     SUM(CNT)
    FROM
         SELECT     COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet'
         UNION ALL
         SELECT     COUNT(*) FROM TABLE_B WHERE name='Ameet'
         UNION ALL
         SELECT     COUNT(*) FROM TABLE_C WHERE name='Ameet'
    )

  • Create spreadsheet file with a fixed number of rows

    What is the most straight forward way to create a series of spreadsheet files each with a new file name and fixed number of rows.  We have a data acquisition process that creates a new 1D array every 2 seconds.  We'd like to build a series of spreadsheet files each having two hours or 3600 rows of data.  Is there a best way to do this in LV9?
    john

    Use the low-level FileI/O Vis with Write to Text File.vi where you open a new file with every N iteration like this:
    You just have to convert your 1D Array to string before.
    If you would like to have a new file every N hours you should create a FGV which checks the elapsed time using Get Date/Time in Seconds.vi, which is more appropriate for longtime applications.
    Christian

  • How to display a fixed number of rows in a page when using CL_GUI_ALV_GRID

    Hy experts
    How to display a fixed number of rows in a page when using CL_GUI_ALV_GRID?? lets say 500 ?? because my display table it may contain in some cases 10.000 and evidently I can t see all of them..
    I have a button in my toolbar witch triggers this event
    (display 500 records ) but I don t have the logic to do this only with methods of CL_GUI_ALV_GRID.
    can you tell me a standard method of CL_GUI_ALV_GRID witch can help me do this?? any hint will be good..
    Till now I was used to add a column to my structure witch represents a flag that is a number corresponding to every 500 records (a batch containing 500 records )
    first       500 - flag -> 1
    second  500 - flag -> 2
    etc..but I m convinced that exists a way of doing this more easy..without damaging my structure..
    thanx in advance..don t be shy..reply if you have any hints..

    Hi,
    if method SET_FILTER_CRITERIA doesn´t help, I think that you must work with 2 internal tables, a counter and a loop for filtering the records to be displayed:
    case counter.
      when 1.
         loop at int_table1 from 1 to 500.      "<-- your table with all records
           move int_table1 to int_table2
        endloop.
      when 2.
         loop at int_table1 from 501 to 1000.     
           move int_table1 to int_table2
        endloop.
    etc, etc.
    Call grid-->SET_TABLE_FOR_FIRST_DISPLAY
       exporting
         IT_OUTTAB = int_table2                "<-- instead of your currently table int_table1

  • RDL report(2008).Want to display different number of rows from second page onwards than the first page.

    I have used pagination to display the report data.I have used page break.I want to display 10 records on first page and from second page onwards I want to display 25 records on all remaining pages.
    I followed this link to show 25 records for all page. "http://www.sqlchick.com/entries/2010/9/11/displaying-fixed-number-of-rows-per-ssrs-report-page.html". Now suggest me how display 10 records only on first page having 25 from second page onwards.

    Hi mukesh_harkhani,
    According to your description, you want to insert page break for different number of rows, display 10 rows on the first page and 25 rows on the following pages. After testing the issue in my SQL Server Reporting Services 2008 environment, we can use the
    method below to achieve your requirement:
    In your scenario, right-click the group which contains the expression: =CEILING(RowNumber(Nothing)/25) to open the Group Properties dialog box.
    Modify the original expression to the following in the Group on textbox:
    =Floor((RowNumber(Nothing)+14)/25)
    If there are any other questions, please feel free to ask.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • How to show  a fixed number of rows in JTable

    Hi,
    I have to show only a fixed number of rows in the table .
    After scrolling number of rows must not be changed.

    I don't understand the question.
    The number of visible rows is dependent on the size of the scroll pane.
    Scrolling does not change the number of rows that are visible.

  • Getting a specific number of rows

    Hi,
    I would like to know how to get a specific number of rows. For example, I want to page data in my web application, so I'd like to be able to retrieve just 25 rows in my SELECT statement. I searched and I couldn't see anything under the obvious keywords of LIMIT and TOP (as used by other database vendors) so I'm wondering if possible, what do I need to do - a pointer to the relevant documentation would be helpful also.

    You can achieve this with rownum
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#i1006297
    Best regards
    Maxim

  • Fixed number of rows in ADF table

    Can we specify fixed number of rows for ADF table so even no rows displayed it will still show 10 empty rows.

    the rangesize property determines how many rows will be displayed and controls paging. If there are no rows to begin with (if I understand the question correctly) rangesize won't have any effect.

  • Fastest way to check total number of rows from View ?

    Hello Everyone,
    Good Morning !!!!
    I am trying to know the exact number of rows from a view.
    I have total 262 columns in a view name vw_sample. First column name is col_1 and I do not have permission to view that. but I do have permission to view all other 261 columns. I want to know fastest way to know exact number of rows in this view vw_sample.
    How can i know that ?
    I tried below things.
    1) Tried using COUNT(*) & COUNT(1) but I don't have permission to first column col_1 so that didn't worked.
    2) I do not have permission to view DMV "sys.dm_db_partition_stats"
    3) I tried to execute below code.
    select col_2, col_3,...col_262
    into #TempTable
    from vw_sample
    and it gave me below error
    Msg 1105, Level 17, State 2, Line 1
    Could not allocate space for object '<temporary system object: 9830433781072176840>'
    in database 'tempdb' because the 'PRIMARY' filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.
    4) Accidentaly I run below code
    select col_2, col_3,...col_262
    from vw_sample
    It gave me 11 million rows in 40 minutes. As we know this method is not good to test just number of rows from a view.
    So what would be my option here ?
    Thanks in advance.
    Thanks
    Fredyy

    Fredy, did you try with other column names instead of col1?
    SELECT COUNT(col2) FROM View
    What difference does that make, than using your column 1.
    Please mark as answer, if this has helped you solve the issue.
    Good Luck :) .. visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
    IMO, sqlsaga's answer is the way to go, but you may need to make a minor change.  When you do COUNT(<some expression>), SQL counts the number of rows where <some expression> is not NULL.  So if col2 can be NULL you need to make a change
    so you know what you are counting can't be NULL.  If there is some other column in the view that can't be NULL, just count it.  Otherwise just use the ISNULL function to convert NULL's to a value.  For example, do
    Select COUNT(IsNull(col2, 0)) From vw_sample
    Note the above assumes that col2 is a type that contains a number (int, numeric, float, etc).  If col2 is of a different type, then just replace the 0 in the above with any valid value of the same type as col2 (so '' for varchar, '19000101' for datetime,
    etc).
    Tom

  • Deleting a row from the item table

    Hi All,
    I have a requirement where I need to put a button to delete the selected row from the item table and for this I have written the following code:
    DATA lo_nd_t_bseg TYPE REF TO if_wd_context_node.
        DATA lo_el_t_bseg TYPE REF TO if_wd_context_element.
        DATA ls_t_bseg TYPE wd_this->element_t_bseg.
        data: it_tab type table of wd_this->element_t_bseg.
      lo_nd_t_bseg = wd_context->path_get_node( path = `Z.T_BSEG` ).
      lo_el_t_bseg = lo_nd_t_bseg->get_element( ).
      lo_el_t_bseg->get_static_attributes(
        IMPORTING
          static_attributes = ls_t_bseg ).
        lo_nd_t_bseg = wd_context->path_get_node( path = `Z.T_BSEG` ).
        lo_el_t_bseg = lo_nd_t_bseg->get_element( ).
        IF lo_el_t_bseg IS not INITIAL.
          lo_nd_t_bseg->remove_element( lo_el_t_bseg ).
        ENDIF.
    Now the problem is although it's deleteing the selected line correctly but because of this I am losing one line on the screen for the user to enter... my form has a fixed number of lines and in my case it's 10... so everytime I am using deleting a line item I am losing one line to enter..... can you please tell me how can I avoid this?
    Edited by: rajatg on Aug 4, 2011 3:12 PM

    lets say...
    i have 1,2,3 documents
    i have cleared , document 2....
    1. when the user selects this record.... you can read the context_element....using the context element you can get the values and clear those values and set the blank values...
    when the user click on save ...you can have only those two records in the table.
    2. conitnue with your logic .... remove_element. once it is done ,,, create_element at the deleted index...
    3. when clicked on delete ...remove_element ( current code).... bind the table to the node... and you will have ur values

  • Get Number of rows from a sql query.

    I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
    Is there an easy way to do this without looping through the table?
    Thanks Jasper

    Hi Jasper,
    You can use the XPATH count function similar to  this:
    GetTagList.Results{count(/Rowsets/Rowset/Row)}
    Kind Regards,
    Diana Hoppe

  • Determine number of rows from javascript so I can hide if zero

    Hi All,
    Found a great article which I adapted to use the link on a report to delete a row in db and remove from table (without a refresh).
    It works great except I want the region to not display when there are no rows left.
    Currently it shows "No Data Found" but because the page does not submit the region does not hide.
    Here is the Java code:
    function ackMsg(p_this, p_empno) {
    // get the table row on which the user clicked
    var tr = $(p_this).closest('tr');
    // perform an asynchronous HTTP AJAX request using jQuery
    $.ajax({
    type: "POST",
    url: "wwv_flow.show",
    data: {
    p_flow_id: $('#pFlowId').val(),
    p_flow_step_id: $('#pFlowStepId').val(),
    p_instance: $('#pInstance').val(),
    x01: p_empno, // assign p_empno to the g_x01 global variable
    p_request: "APPLICATION_PROCESS=ack_message" // refer to the application process
    beforeSend: // executes while the AJAX call is being processed
    function() {
    // delete following HTML classes from the table row element
    // could be possibly theme dependent
    tr.removeClass('even');
    tr.removeClass('odd');
    // use jQuery's animate function to give the table row, and its children, a red background
    tr.children().hover(function() {
    tr.children().animate({'backgroundColor': '#00cc00'}, 300);
    }, function() {
    tr.children().animate({'backgroundColor': '#00cc00'}, 300);
    tr.children().animate({'backgroundColor': '#00cc00'}, 300);
    success: // to be called if the request succeeds
    function() {
    jQuery(p_this).trigger('apexrefresh');
    // jQuery has difficulties animating inline elements
    // that's why we wrap them in a div, which is a block element
    tr.children().wrapInner('<div>').children().fadeOut(400, function() {
    tr.remove(); // visually remove the row from the report
    I did some Googling to add the line ' jQuery(p_this).trigger('apexrefresh');' which refreshes just that report.
    What I think I need to do is add a check for number of rows left after the refresh then if = 0 do full page refresh to allow the region condition to hide it.
    Unfortunately after hours on Google I can't find how to check how many rows are in the table.
    PS the function is called from the link in the report like this onclick="ackMsg(this, #ACK#)"
    Please help
    AT
    Edited by: user1678248 on May 13, 2013 9:39 PM
    Edited by: user1678248 on May 13, 2013 9:40 PM

    1)
    When you set a column to hidden in a classic report using the method I described, it creates hidden form elements for that column.
    <tt><input type="hidden" name="f01" value="" id="f01_0002"></tt>
    You should inspect the source to figure out the name value.
    Since this belongs to the wwv_flow form, we can therefore get this data with: wwv_flow.f01
    Alternatively, you could have used the apex_item API to achieve the same result, which probably gives a bit better control.
    http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/apex_item.htm#CHDBFHGA
    select apex_item.hidden(1, col) || col col
    from table
    Set that column to a standard report column so it doesn't escape the html.
    You can also reference the data in these fields using PL/SQL using the apex_application API:
    http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/apex_app.htm#CHDGJBAB
    2)
    As above, setting the column to hidden creates hidden elements on the page. It is simply f01 because that is the only hidden field I would have on my page. With four reports on the page, you would need to be careful not to do the same on the other reports, as you would get inaccurate data. For each report, setting columns to hidden always starts with f01. In that, you would be best to use the apex_item API.
    I actually initially ran into this issue when i had a tabular form on the same page as a report with hidden fields, which was causing conflicts in the page processes.
    Hope it helps.

  • How to Efficiently Sample a Fixed Number of Rows

    Good afternoon. I need to select a specific number of random rows from a table, and while I believe my logic is right it's taking too long, 30 minutes for a routine data size. Hopefully someone here can show me a more efficient query. I've seen the SAMPLE function, but it just randomly selects rows on a one-by-one basis, without a guaranteed total count.
    This is the idea:
    INSERT INTO Tmp_Table (Value, Sequence) SELECT Value FROM Perm_Table, DBMS_RANDOM.VALUE;
    SELECT Value FROM Tmp_Table WHERE ROWNUM <= 1234 ORDER BY Sequence;I'd need to put the ORDER BY in a subselect for ROWNUM to work correctly, but anyway that's just an illustration. My actual need is a little more complicated. I have many sets of data; each set has many rows; and for each set I need to return a specific (different) number of rows. Perhaps project A has three rows in this table, and I want to keep two of them; project B has two rows, and I want to keep one of them. So I need to identify, for each row, whether it's valid for that project. This is what my data looks like:
    Project Person  Sequence Position Keeper
    A       Bill    1234     1        Yes
    A       Fred    5678     3        No
    A       George  1927     2        Yes
    B       April   5784     2        No
    B       Janice  2691     1        YesI populate Sequence with random values, then calculate the position of each person within their project, and finally discard people who's Position is greater than Max_Targets for the Project. Fred and April have the highest random numbers, so they're cut. It's not the case that I'm just trimming one person from each project; the actual percentage kept will range from zero to 100.
    Populating the list with random values is not time-consuming, but calculating Position is. This is my code:
    UPDATE Tmp_Targets T1
    SET Position =
      SELECT
       COUNT(*)
      FROM
       Perm_Targets PT1
       INNER JOIN Perm_Targets PT2 ON PT1.Project = PT2.Project
       INNER JOIN Tmp_Targets T2 ON PT2.Target = T2.Target
      WHERE
           T1.Target = PT1.Target
       AND T2.Sequence <= T1.Sequence
      );The Target fields are PKs, and the Project and Sequence fields are indexed. Is there a better way to approach this? I could write a cursor that pulls out project codes and performs the above operations for each project in turn; that would be logically simpler and possibly faster. Has anyone here addressed a similar problem before? I'd appreciate any ideas.
    This is on 9.2, in case it matters. Thank you,
    Jonathan

    You've not given any indication of how max targets for a given project is determined, so for my example I'm using the ceiling of 1/2 of the number of records in each project which gives the same number of yes and no responses per project as you had:
    with dta as (
      select 'A' project, 'Bill' person from dual union all
      select 'A', 'Fred' from dual union all
      select 'A', 'George' from dual union all
      select 'B', 'April' from dual union all
      select 'B', 'Janice' from dual
    ), t1 as (
      select project
           , person
           , row_number() over (partition by project order by dbms_random.value) ord
           , count(*) over (partition by project) cnt
           , rownum rn
        from dta
    select project
         , person
         , ord
         , cnt
         , case when ord <= ceil(cnt/2) then 'Yes' else 'No' end keep
      from t1
      order by rn
    PROJECT PERSON ORD                    CNT                    KEEP
    A       Bill   2                      3                      Yes 
    A       Fred   3                      3                      No  
    A       George 1                      3                      Yes 
    B       April  1                      2                      Yes 
    B       Janice 2                      2                      No  
    5 rows selectedIn this example I use an analytic function to assign a random ordering for each record within a project in the middle query, in the final output query I am determining the yes no status based on the order within a project and the count of records in the project. If you had a table of projects indicating the thresh hold you could join to that and use the thresh hold in place of the ceil(cnt/2) portion of my inequality in the case statement.

Maybe you are looking for

  • BI-IP Characteristic Rel. Part 2

    Hi I have edited the code under method Derive with no syntax errors. Now when I activate the code, I get the following error message: Class ZCL_RSPLS_CR_EXIT_0FISCPER,Public Section     26     @0A\QError@ Implementation missing for method "CONSTRUCTO

  • Dojo drag-and-drop functionality - how to record?

    Hello, I am preparing some e-Tester scripts for our project. On one of the pages, the user has to choose some people as team members. This process is dynamic, and is developed and written using dojo (dojotoolkit.org) drag-and drop capabilities. Basic

  • Misconfigured rcap on Solaris 10 with zones - SMF fails to start rcap

    Hi, I have just installed Solaris 10 on a brand new T5220 server (Niagara processor, 16GB of RAM). After creating first non-global zone with capped memory and cpu, SMF fails to start rcap service: +root@bs10 [~]$ svcs -xv+ svc:/system/rcap:default (r

  • How to invoke Java file into Mobile phone

    hi I have one java file (as servlet to display WML content on mobile phone) but i don't know how to invoke it into mobile phone . Can you help me ? Give me your answer asap Thanks .

  • Best equivalent to Photoshop's Color Balance dialog

    I'm trying to understand what's the most equivalent function to Photoshop's Color Balance dialog in Lightroom 2.  e.g. suppose I move the Cyan/Red a bit toward the red to make the image more 'redish'.  What LR2 action will produce similar results? th