Select the records from the first n distinct values of column

I need to write a query in plsql to select records for the first 3 values distinct from a single column (example below, ID) and all lines for the next 3 distinct values of column and so on until the end of the number of distinct values in a column.
for example:
Age of name ID
1 abc 10
1 def 20
2 IA 10
2 20 JKL
2 mno 60
3 10 pqr
4 the RST 10
4 10 TÜV
5 vwx 10
6 10 XYZ
hij 6 10
7 lmn 10
.
.
.
so now... (up to a few County)
Result must be
1 the application should result->
Age of name ID
1 abc 10
1 def 20
2 IA 10
2 20 JKL
2 mno 60
3 10 pqr

Query 2 should lead to->
4 the RST 10
4 10 TÜV
5 vwx 10
6 10 XYZ
hij 6 10

Query 3 should lead to->
7 lmn 10
.
.
9... ..
so now...
How to write a query for this inside a loop.

Hello

Thus, a panel will consist of the lowest id value, the 2nd lowest and the 3rd lower, reggardless of how many lines is involved. The next group will include the 4th lowest id, the 5th lowest and the 6th lowest. To do that, you must assign the numbers 1, 2, 3, 4, 5, 6,... for the rows in order by id, with all lines with the same id, getting the same number and without jumping all the numbers.
This sounds like a job for the analytical DENSE_RANK function:

WITH     got_grp_id     AS
(
     SELECT     id, name, age
     ,     CEIL ( DENSE_RANK () OVER (ORDER BY id)
               / 3
               )          AS grp_id
     FROM     table_x
)
SELECT     id, name, age
FROM     got_grp_id
WHERE     id     = 1     -- or whatever number you want
;

If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
See the FAQ forum {message identifier: = 9360002}

Tags: Database

Similar Questions

  • Select the first 20 days of the month for a given date

    Hi gurus,

    Please let me know to select the first 20 dates for a date supplied by using a parameter.

    For ex -.

    Suppose I provided a date by using a parameter - January 13, 2012

    The query should return me - 01-Jan-2012, January 2, 2012... January 20, 2012

    or if I get home on 23 March 2012

    The query should return me - 01-Mar-2012, March 2, 2012... 20 March 2012

    Thanks in advance.

    Hello

    Here's one way:

    VARIABLE  target_date     VARCHAR2 (11)
    EXEC     :target_date := '13-Jan-2012';
    
    SELECT      TO_DATE ( SUBSTR (:target_date, 4)
                , 'Mon-YYYY'
               ) + LEVEL - 1     AS a_date
    FROM     dual
    CONNECT BY     LEVEL     <= 20
    ;
    

    Note that this does not take account the part of: target_date that represents the day of the month. When you use TO_DATE without giving a date of the month, it defaults to the 1st of the month.

  • It is possible to select more than one layer by selecting the first and the last layers?

    I frequently get Illustrator files which are individual objects such as the paths on separate layers that can be grouped together to make the shorter layers. I find that I have to change click on each individual layer to select it to finally bring together them. Sometimes, I have 25 or so layers that I need to select. Is there a way to select the first and the last layer in the layers palette and have Illustrator select all the layers in the meantime?

    Don't know about the selection of layers of first/last, but to select several consecutive layers in the layers palette...

    Option-click on (Mac) on a layer to select it and, with the option always pressed key, drag to the bottom of the list - DO NOT slip on as logical target icons you like.  When you drag to the bottom of the list of layers, the icons of the target will become selected.

    Shift + Option-click allows you to ignore the layers and continue to select consecutive additional layers on the bottom of the palette.

    Note: Drag slowly or some layers would not choose.

    Strange, I know.  It took me years to come across this.

  • How can I select the first date (at least) and the corresponding date of two different tables source?

    I have 2 tables, INVOICE and INVOICE_REFNUM similar to below. I'm writing a query that withdrawing the first date (less) for a single record remaining the two tables. Each table records the receipt of invoices, but according to the source of the received invoice some may reside in INVOICE_REFNUM and others the INVOICE table. I also use DECODE on the topic of INVOICE_SOURCE, when the date less is taken from the INVOICE table that contains that field, so if the date is in the field INVOICE_REFNUM DECODE is not necessary. Example:

    INVOICE (table)

    INVOICE_GID INVOICE_SOURCE INSERT_DATE

    ABC. AVRT123 I HAVE 11/03/2012-15:49:32

    ABC. CNWY234 G 12/03/2012 14:07:30

    ABC. UPGF678 M 15/03/2012 20:21:54

    INVOICE_REFNUM (table)

    INVOICE_GID INVOICE_REFNUM_QUAL INVOICE_REFNUM_VALUE INSERT_DATE

    ABC. AVRT123 SOURCE ACS 11/03/2012 14:49:32

    ABC. CNWY234 SOURCE CSA 12/03/2012 13:07:30

    ABC. UPGF678 SOURCE ACS 15/03/2012 21:21:54

    My decode statement is: DECODE ("INVOICE_SOURCE, 'I', 'IDE', 'G', ' Auto-Pay", ', 'Manual')

    If I were to write a query on these two tables, the output will resemble the following:

    INVOICE_GID INVOICE_SOURCE INSERT_DATE

    ABC. AVRT123 ACS 11/03/2012 14:49:32

    ABC. CNWY234 ACS 12/03/2012 13:07:30

    ABC. UPGF678 Manual 15/03/2012 20:21:54

    I worked on it for 2 days, and I was able to pull a date by using the following SQL code, but cannot determine how to extract the corresponding source:

    SELECT THE LEAST (GLOGOWNER. INVOICE_REFNUM. INSERT_DATE, GLOGOWNER. THE INVOICE. INSERT_DATE) AS 'DATE OF THE INVOICE.

    GLOGOWNER. THE INVOICE. INVOICE_GID,

    DECODE (GLOGOWNER. THE INVOICE. INVOICE_SOURCE, 'I', 'IDE', 'G', 'Auto-Pay', ', 'Manual').

    GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE

    OF GLOGOWNER. INVOICE

    LEFT JOIN GLOGOWNER. INVOICE_REFNUM

    ON GLOGOWNER. THE INVOICE. INVOICE_GID = GLOGOWNER. INVOICE_REFNUM. INVOICE_GID

    WHERE GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE = "ACS."

    AND GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_QUAL_GID = 'SOURCE '.

    AND GLOGOWNER. THE INVOICE. INVOICE_TYPE = 'I '.

    ORDER OF GLOGOWNER. THE INVOICE. INVOICE_GID

    Any help is greatly appreciated!

    Best regards

    -Adam

    Hello

    Is that what you want?

    SELECT i.invoice_gid

    CASE

    WHEN ir.insert_date<= i.insert_date ="" then ="">

    WHEN i.invoice_souce = 'I' and THEN 'EDI '.

    WHEN i.invoice_souce = 'G' THEN 'debit '.

    WHEN i.invoice_souce = am' THEN 'manual '.

    END AS invoice_source

    , The LEAST (i.insert_date, ir.insert_date) AS insert_date

    I have the Bill OF

    JOIN invoice_refnum ON i.invoice_gid = ir.invoice_gid ir

    WHERE ir.invoice_refnum_value = 'ACS '.

    AND ir.invoice_refnum_qual_gid = 'SOURCE '.

    AND i.invoice_type = 'I '.

    ORDER BY i.invoice_gid

    ;

    There is no reason to do an outer join if you only want the lines with specific values anyway.

    What happens if the 2 tables have exactly the same invoice_date?  The query above takes the invoice_source of invoice_refnum in this case, but that can easily be changed.

    Or the other of the insert_dates can be NULL?

    I hope that answers your question.

    Otherwise, thank you post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also after the results you want from these data (otherwise what you have already posted).

    Explain, using specific examples, how you get these results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

  • SQL: How to select the first telephone number for the Service to find?

    Hi, can someone help me difficulty this query?

    The problem is - ID is left with no phone number, a phone number and phone number. My requirement is to choose only first phone numbers and NULL values if a party who has more than one phone number. The query below is correctly picking of NULL values and unique phone numbers but also by selecting all the phone numbers for the id of group 13909 which has 3 phone numbers

    It involves choosing the first phone number 416-890-0089 and limited to 416-960-8686, 647-456-3040

    How to select only the first group id for the parties who have more than one phone number

    Thanks in advance!

    Select distinct p.party_id,
    p.family_name,
    p.given_name,
    p.Status,
    p.type,
    above. Value,
    PR. Role,
    ad.unit_number,
    ad.unit_identifier,
    ad.street_number,
    ad.street_number_suffix,
    ad.street_name,
    ad.street_type,
    ad.street_direction,
    ad. City,
    ad.province_id,
    ad.Postal_Code,
    ad. Country_ID,
    ad.postal_box_number,
    ad.station_type,
    ad.station_identifier,
    ad.address_line_1,
    ad.address_line_2,
    ad.address_line_3,
    ad.rural_route_identifier,
    ad.rural_route_type,
    ad. Status,
    ad. ADDRESS_ID,
    ad.attention

    part p, ad address this contact_information, party_contact pc, pd, pr SR1CUST1.party_role party_address

    Where
    P.party_id = PD. Party_id
    AND AD.address_id = PD.address_id
    and p.party_id = pr.party_id
    And (CI. Contact_Mode 't ='
    Or CI. Contact_Mode is null)
    AND P.party_id = PC. Party_id (+)
    And PC. CONTACT_INFO_ID = this .contact_info_id (+)
    AND AD.address_type = 'P '.
    and P.type = 'B'
    and pr.role = 'C '.
    and p.family_name like '% Lew '.
    and as p.given_name ' J %
    Order by p.party_id ASC


    Response


    1478 null
    1479 null
    1577 null
    1712 null
    1822 null
    6346-647-345-7384
    6347 416-890-9384
    6348 519-897-0089
    6832 416-930-1265
    8733 647-976-8923
    13909 416-890-0089
    13909 416-960-8686
    13909 647-456-3040

    You can simply use one group from all other columns that the phone number and then choose the min (phone_number):

    select col1,
             col2,
             ...
             coln,
             min(phone_number_column)
    from yourtables
    where yourconditions
    group by col1,
                 col2,
                 ...
                 coln;
    
  • Remove duplicates by selecting the first line of each subgroup in a Recordset

    Oracle: 11 GR 2

    Client Windows 7

    I have a set of data (the result of a query) which is similar to:

    D_rank product_id product_desc category income price cost

    1 112233 prod1-iphone phone 200 500 100

    1 112233 prod1-iphone4 phone 200 500 100

    2 223344 prod2-300 1000 400 windows7 OS

    2 223344 prod2-300 1000 400 software windows7

    3 334455 prod3-Mac 1000 computer 1500-1000

    4 445566 prod4-xyz 2000 800 1200 misc

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    As you can see the first 4 rows, these rows are repeated due to the product_desc and category material. I need these fields but the first value in each subgroup, so that each row is unique. I.e.

    D_rank product_id product_desc category income price cost

    1 112233 prod1-iphone phone 200 500 100

    2 223344 prod2-300 1000 400 windows7 OS

    3 334455 prod3-Mac 1000 computer 1500-1000

    4 445566 prod4-xyz 2000 800 1200 misc

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    You will enjoy the little help.

    Hello

    user13667036 wrote:

    Oracle: 11 GR 2

    Client Windows 7

    I have a set of data (the result of a query) which is similar to:

    D_rank product_id product_desc category income price cost

    1 112233 prod1-iphone phone 200 500 100

    1 112233 prod1-iphone4 phone 200 500 100

    2 223344 prod2-300 1000 400 windows7 OS

    2 223344 prod2-300 1000 400 software windows7

    3 334455 prod3-Mac 1000 computer 1500-1000

    4 445566 prod4-xyz 2000 800 1200 misc

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    :                    :               :                                   :               :             :              :

    As you can see the first 4 rows, these rows are repeated due to the product_desc and category material. I have need of these fields but the first value of each subgroup...

    ...

    What does "first" here?  What "subgroup"?

    Depending on your needs, maybe you want to Request Top - N, something like this:

    WITH got_r_num AS

    (

    SELECT d_rank, product_id, product_desc, category, price, income, cost

    ROW_NUMBER () OVER (PARTITION BY d_rank

    ORDER BY product_desc, category

    ) AS r_num

    OF data_set

    )

    SELECT d_rank, product_id, product_desc, category, price, income, cost

    OF got_r_num

    WHERE r_num = 1

    ;

    I hope that answers your question.

    If this isn't the case, please post a small example of data (CREATE TABLE and INSERT statements) and the results desired from these data.  (I know you said this is a game of results from a query, and not an array, but the solution is the same as the entry is a single table, or a query, perhaps involvoing several tables.)

    Report when the request above is erroneous results and explain, using specific examples, how find you the correct results from these data.

    As said ground, see the FAQ in the forum: Re: 2. How can I ask a question on the forums?

  • Select the first or last row of sql Command

    Hello

    I am trying to get the fist or the last line of output from sql below

    SQL> set head off;
    SQL> select  r.actual_start_date,r.phase_code,r.status_code,r.actual_completion_date from
      2      apps.fnd_concurrent_programs_vl p, apps.fnd_concurrent_requests r
       where p.concurrent_program_id = r.concurrent_program_id
      3    4      and p.application_id = r.program_application_id
      5      and p.user_concurrent_program_name like 'Purge Concurrent Request and/or Manager Data'
      6  and  r.actual_start_date >= sysdate-7
    --and rownum<1
      7    8  --order by r.requested_start_date
      9  ORDER BY r.actual_completion_date DESC;
    
    
    29-DEC-13          C C 29-DEC-13
    28-DEC-13          C C 28-DEC-13
    27-DEC-13          C C 27-DEC-13
    26-DEC-13          C C 26-DEC-13
    25-DEC-13          C C 25-DEC-13
    24-DEC-13          C C 24-DEC-13
    23-DEC-13          C C 23-DEC-13
    
    
    7 rows selected.
    
    
    SQL> select  r.actual_start_date,r.phase_code,r.status_code,r.actual_completion_date from
      2      apps.fnd_concurrent_programs_vl p, apps.fnd_concurrent_requests r
       where p.concurrent_program_id = r.concurrent_program_id
      3    4      and p.application_id = r.program_application_id
      5      and p.user_concurrent_program_name like 'Purge Concurrent Request and/or Manager Data'
      6  and  r.actual_start_date >= sysdate-7
      7  and rownum<2
      8  --order by r.requested_start_date
      9  ORDER BY r.actual_completion_date DESC;
    
    
    
    27-DEC-13          C C 27-DEC-13
    
    
    
    
    
    
    SQL> select  r.actual_start_date,r.phase_code,r.status_code,r.actual_completion_date from
      2      apps.fnd_concurrent_programs_vl p, apps.fnd_concurrent_requests r
       where p.concurrent_program_id = r.concurrent_program_id
      3    4      and p.application_id = r.program_application_id
      5      and p.user_concurrent_program_name like 'Purge Concurrent Request and/or Manager Data'
      6  and  r.actual_start_date >= sysdate-7
      7  and rownum<1
      8  --order by r.requested_start_date
      9  ORDER BY r.actual_completion_date DESC;
    
    
    no rows selected
    
    
    SQL>
    
    

    I want to get the last run of the program which is the first line of the top of the output of sql. (which is December 29, 13, first row on top of the exit)

    But if I use the condition as ' where rownum < 1, its not giving any output, if I use where rownum < 2, if you do not have out. "

    Grateful if someone can help.

    -Thank you

    Vijay...

    Hi Vijay,

    The following should work...

    ROWNUM< 1="" will="" not="" retrieve="" any="" rows="" as="" rownum="" values="" start="" with="" 1.="" to="" get="" the="" desired="" result="" you="" have="" to="" build="" the="" result="" set,="" order="" it="" and="" then="" select="" the="" desired="">

    SELECT *.

    Of

    (SELECT r.actual_start_date,

    r.phase_code,

    r.status_code,

    r.actual_completion_date

    Apps.fnd_concurrent_programs_vl p,

    Apps.FND_CONCURRENT_REQUESTS r

    WHERE p.concurrent_program_id = r.concurrent_program_id

    AND p.application_id = r.program_application_id

    AND p.user_concurrent_program_name LIKE 'Serve concurrent demand and/or Manager Data'

    AND r.actual_start_date > = sysdate-7

    -Order of r.requested_start_date

    ORDER BY r.actual_completion_date DESC

    )

    WHERE rownum< 2="">

    I hope this helps.

    VR,

    Sudhakar

  • How to select the first item in an array in a loop FOR

    Hello.

    I use Labview 8.5

    In my application, I have a loop 'for' where I read some values from a device. (strings and digital)

    As you know Labview 'stores' values up to what the loop 'for' is completed, and then they come out in a table.

    Ok.

    I want to just use the first element for strings and digital and then do something with it, like to compare (for digital)

    For strings, I used successfully 'Split 1 D Array' and choose 'everything first subarray'. Good.

    Now for digital that works too, but the result is an element, array type too. I need to be digital (decimal) is not no table type, because I need to compare (greater than 0). Then use a case...

    So, table of an element in a digital decimal? Or the table at the first decimal element. How?

    I tried also with the subset of the table. Even think. Labview won't let me compare a table 1 d with 0. It makes sense, but how to convert, to have the first decimal element?

    Try to use the 'Index Array' function. If you do not connect to the terminal of the index value, it will return the first elelemnt in the table. If wire you an index, it will return the value specified.

    Ben

  • Select the first choice to join

    Hello

    I have a problem in the application.

    Requirement:

    the user has options to select the brand and the user interface element.

    mark and point are from two different tables.

    Neither brand nor element is mandatory.

    ELEMENTS
    item_idNom_elementitem_brandbrand_id
    1bikeHonda11
    1bikeTVS12
    1bikeBailey13
    2carHonda11
    2carToyota14
    3carSuzuki15
    1bikeSuzuki15

    BRAND
    brand_idbrand_name
    11Honda
    12TVS
    13Bailey
    14Toyota
    15

    Suzuki

    MY_TABLE
    IDbrand_iditem_id
    21(null)(null)
    2211(null)
    23(null)1
    24111

    SQL

    Select my.id, my.brand_id, b.brand_name, my.item_id, i.item_name

    from my_table my, mark b, points i

    where my.brand_id = b.brand_id (+)

    and

    My.item_id = i.item_id (+)

    order of my.id;

    I want to display the details of my table with id brand and name brand... and item id, item name...

    but the sql above gives double for the name of the item while that no brand is selected... and also a few duplicates while giving the id brand and point.

    If anyone can help with the query.

    This issue will be triggered in the sql forum.

  • Close the startpoint and endpoint without selecting the first

    Hi guys,.

    I'm working on an image of polygon which means I have created hundreds and hundreds of small triangles with the path tool.

    Now, I have wondered if it is possible to create two lines of the triangle in hand and close the last third line of triangles in shortcut.

    There may be a shortcut (or almost) to connect the beginning and the end of a path? I know I can do it by clicking on cmd + J, but I first select the points which is more work than simply connecting them to the hand. It would be perfect if there's any way to close it without selecting it first, so that I can do an 'action' out of it with a shortcut!

    Any ideas?

    Pathfinder unite.

  • Select the first line of the annotations in a vm

    Hello

    I want to extract only the first line of the note annotations of a vm.

    I tried WAT

    $vm = get - vm host1

    $vm.description | Select - 1 first

    But I continue to receive the full description, which looks like this

    Microsoft exchange server

    support: 24 x 7

    OTAP: Production

    Status: In Production

    Try something like this

    $vm = get-vm windows1$vm.description -split '["\n\r"|"\r\n"|\n|\r]' | select -First 1
    
  • GREP string to select the first 2 characters of an e-mail address

    I want o apply the formatting for the first two characters of an e-mail address:

    [email protected]

    the party @domain.com never changes so I can simply place in parentesis.

    So far I managed to isolate the word in front of the e-mail domain:

    \w+([email protected])

    I want to select only the first two characters of the word before the @domain.com

    any ideas?

    Thank you!

    No, sorry. Try this:

    \b[-\w]{2}(?=[-\w]+@domain\.com)

  • Select the first value in the list of values when running in poplist

    Hello
    IM using forms 6i. I filled the poplist by request. But what I need, I want to choose the first value in the list of values when running...

    That is to say, during execution, the poplist should have chosen its first value in the list of values.

    What to do...

    Concerning
    Sery

    WHEN CREATING a RECORD of the block of the list item or after the filling of the list item, you can write

    : my_list_item: = GET_GROUP_CHAR_CELL ('my_list_item', 1); / * If the data type of the item in your list is character.*.

    Here, instead of 1, you can give any clue... 1: value to the first list item position

  • When I select the first line number I get a little gray box pop up. This looks like a changer of glyph. How to disable this option?

    Screen Shot 2016-01-06 at 10.39.30 AM copy.png

    Above is the example. I don't know what happened (my fault or updated) to activate it, but I can't find anything to delete it. It's not useful because it does not work for numbers later in the line, only the first issue.

    You can disable this option in advanced Type preferences, the bottom section.

  • the index of the first/last numerical value

    Hello

    I want to find the index of the first numeric value in a string. I can do this by using built-in features of Oracle? (Without creating a new function)

    I want to find the index of the first numeric value in a string

    regexp_instr could do (for the first digit):

    SQL> select regexp_instr('abc1def2', '\d') first_digit from dual
    /
    FIRST_DIGIT
    -----------
              4
    1 row selected.
    

Maybe you are looking for