Select records based on first n distinct values of column

I need to write a query in plsql to select records for first 3 distinct values of a single column (below example, ID )and all the rows for next 3 distinct values of the column and so on till the end of count of distinct values of a column.
eg:
ID name age
1 abc 10
1 def 20
2 ghi 10
2 jkl 20
2 mno 60
3 pqr 10
4 rst 10
4 tuv 10
5 vwx 10
6 xyz 10
6 hij 10
7 lmn 10
so on... (till some count)
Result should be
Query 1 should result --->
ID name age
1 abc 10
1 def 20
2 ghi 10
2 jkl 20
2 mno 60
3 pqr 10
query 2 should result -->
4 rst 10
4 tuv 10
5 vwx 10
6 xyz 10
6 hij 10
query 3 should result -->
7 lmn 10
9 .. ..
so on..
How to write a query for this inside a loop.

Hi,
So, one group will consist of the lowest id value, the 2nd lowest and the 3rd lowest, reggardless of how many rows are involved. The next group will consist of the 4th lowest id, the 5th lowest and the 6th lowest. To do that, you need to assign numbers 1, 2, 3, 4, 5, 6, ... to the rows in order by id, with all rows having the same id getting the same number, and without skipping any numbers.
That sounds like a job for the analytic 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'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
See the forum FAQ {message:id=9360002}

Similar Messages

  • Select records based on the closest given time

    Dear SQL gurus,
    I have a table T1:
    Name Null? Type
    ID NOT NULL NUMBER(5)
    MOMENT NOT NULL DATE [DD.MM.YYYY HH24:MI]
    MEASUREMENT NOT NULL NUMBER(8,3)
    Example (ID, MOMENT, MEASUREMENT)
    -- START OF EXAMPLE --
    9380 18.11.2000 03:45 17.6
    9380 18.11.2000 04:30 17.3
    9380 18.11.2000 05:45 16.8
    9380 18.11.2000 06:15 16.8
    9380 18.11.2000 07:00 16.2
    9380 18.11.2000 07:30 16.2
    9380 18.11.2000 08:15 16
    9380 18.11.2000 08:45 15.7
    9380 18.11.2000 09:30 15.4
    9380 18.11.2000 10:00 15.4
    9380 18.11.2000 11:15 15.4
    9380 18.11.2000 11:45 15.4
    9380 18.11.2000 12:30 15.4
    9380 18.11.2000 13:00 15.4
    9380 18.11.2000 13:45 15.4
    --- END OF EXAMPLE --
    How to select records based on the:
    - time period specified by the day only [DD.MM.YYYY] - CONDITION 1
    - with values for 6AM only, and if not available, with values closest to 6AM - CONDITION 2
    (if the time gap in MOMENT field is too big, lets say > 5h then choose the average between the value before 6AM (ex. 4:15AM) and the value after the 6AM (ex. 9:45AM))
    CONDITION 1 (something like): moment between '01.01.2005' and '31.12.2004' - this is OK
    CONDITION 2: I do not know how to formulate, especially if 6AM value is not availabe, and I have to find the closest available value, or get the avergae from the two adjacent values.
    Maybe cursor magic??? Thanks a lot for your help.
    Rado

    About condition two, would the following select be of use to you? Picking the first record could be achived by rownum, analytic function, etc.
    WITH t1 AS (SELECT 9380 id, TO_DATE('18.11.2000 03:45', 'dd.mm.yyyy hh24:mi') moment,  17.6 measurement
                  FROM dual
                 UNION 
                SELECT 9380 id, TO_DATE('18.11.2000 04:30', 'dd.mm.yyyy hh24:mi') moment,  17.3 measurement
                  FROM dual
                 UNION
                SELECT 9380 id, TO_DATE('18.11.2000 05:45', 'dd.mm.yyyy hh24:mi') moment,  16.8 measurement
                  FROM dual
                 UNION
                SELECT 9380 id, TO_DATE('18.11.2000 06:15', 'dd.mm.yyyy hh24:mi') moment,  16.8 measurement
                  FROM dual
    SELECT id, moment, measurement, diff
      FROM (SELECT id, moment, measurement,
                   moment - TO_DATE(TO_CHAR(moment, 'dd.mm.yyyy ') || '06:00', 'dd.mm.yyyy hh24:mi') diff
              FROM t1
    ORDER BY abs(diff) asc, SIGN(diff) desc;
      C.

  • How to select records based on Max/Min on different columns and group by

    I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
    a b c d e
    1 1 1 2 1
    1 1 1 6 4
    1 1 1 6 3
    when i group by a,b i am expecting the record 1 6 3
    Please help me with this.. Thanks in advance....

    Hi,
    Welcome to the forum!
    962163 wrote:
    I have a table with 5 columns(a,b,c,d,e), i need to select records based on MAX(c),Max(D) and Min(e) group by a,b. i am trying using : select max(c),max(d),min(e) from table group by a,b. this is not working. its giving me 1 6 1
    a b c d e
    1 1 1 2 1
    1 1 1 6 4
    1 1 1 6 3
    when i group by a,b i am expecting the record 1 6 3It looks to me like "1 6 1" is the correct answer. You're asking for the lowest value of e, and 1 is lower than 3.
    Maybe you don't want MIN (e). Explain why you want 3 (that is, how you decided that 3 is the correct value for the last column) and someone will help you code it.
    Edited by: Frank Kulash on Sep 28, 2012 6:17 PM
    Whenever you have a problem, you should psot CREATE TABLE and INSERT statements for your sample data. That way, the people who want to help you can re-create the problem and test their ideas. It often helps to clarify the problem, too. since this is your first message, I'll do it for you:
    CREATE TABLE     table_x
    (       a     NUMBER
    ,     b     NUMBER
    ,     c     NUMBER
    ,     d     NUMBER
    ,     e     NUMBER
    INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 2, 1);
    INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 4);
    INSERT INTO table_x (a, b, c, d, e) VALUES (1, 1, 1, 6, 3);
    COMMIT;

  • Select records based on monthly anniversary date

    Hi,
    I have a table with a date_added field and I want to select records based on the monthly anniversary date of this field.
    eg. ID, Date_added
    1, 10-DEC-2012
    2, 11-NOV-2012
    3, 10-MAR-2012
    4, 28-FEB-2012
    5, 30-DEC-2012
    So For the 10th of Jan 2013, I would want to return records 1 and 3 only
    I started looking at the extract function, but this soon falls down for records at the end of the month. For example, on the 28th Feb, I would also want to include records where the date_added day is the 29th, 30th or 31st. So, in the table above I would want to return records 4 and 5, but extract would only return 4.
    Is there a simple function to do this month anniversary query - am I missing something very obvious? Or, do I need to write a query to explicitly cope with dates at the end of the month? So far I haven't found a sensible simple solution!
    I'm using 11g
    thanks

    I didn't look into leap year, but this should give you a starting point:
    select  *
      from  t
      where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
                  when to_date(:target_date,'mmddyyyy')
                    then case
                           when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
                             then 1
                         end
                  else case
                           when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
                             then 1
                         end
                end
    /For example, target date is 1/10/2013:
    SQL> variable target_date varchar2(8)
    SQL> exec :target_date := '01102013';
    PL/SQL procedure successfully completed.
    SQL> with t as (
      2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
      3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
      4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
      5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
      6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
      7            )
      8  select  *
      9    from  t
    10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
    11                when to_date(:target_date,'mmddyyyy')
    12                  then case
    13                         when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
    14                           then 1
    15                       end
    16                else case
    17                         when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
    18                           then 1
    19                       end
    20              end
    21  /
            ID DATE_ADDE
             1 10-DEC-12
             3 10-MAR-12
    SQL> And target date is 2/28/2013:
    SQL> exec :target_date := '02282013';
    PL/SQL procedure successfully completed.
    SQL> with t as (
      2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
      3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
      4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
      5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
      6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
      7            )
      8  select  *
      9    from  t
    10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
    11                when to_date(:target_date,'mmddyyyy')
    12                  then case
    13                         when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
    14                           then 1
    15                       end
    16                else case
    17                         when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
    18                           then 1
    19                       end
    20              end
    21  /
            ID DATE_ADDE
             4 28-FEB-12
             5 30-DEC-12
    SQL> SY.

  • CE function to get distinct values from Column table

    Hi All,
    Could you please let me know the appropriate CE function to get the distinct values from column table.
    IT_WORK = SELECT DISTINCT AUFNR FROM :IT_WO_DETAILS;
    Thank you.

    Hi,
    If you have 10g, you can use Model( with model performance is better than connect by )
    Solution
    ========================================================================
    WITH t AS
    (SELECT '0989.726332, 1234.567432, 3453.736379, 3453.736379, 0989.726332, 3453.736379, 1234.567432, 1234.567432, 0989.726332'
    txt
    FROM DUAL)
    SELECT DISTINCT TRIM(CHAINE)
    FROM T
    MODEL
    RETURN UPDATED ROWS
    DIMENSION BY (0 POSITION)
    MEASURES (CAST( ' ' AS VARCHAR2(50)) AS CHAINE ,txt ,LENGTH(REGEXP_REPLACE(txt,'[^,]+',''))+1 NB_MOT)
    RULES
    (CHAINE[FOR POSITION FROM  1 TO NVL(NB_MOT[0],1) INCREMENT 1] =
    CASE WHEN NB_MOT[0] IS NULL THEN TXT[0] ELSE REGEXP_SUBSTR(txt[0],'[^,]+',1,CV(POSITION)) END );
    =========================================================================
    Demo
    =======================================================================
    SQL> WITH t AS
    2 (SELECT '0989.726332, 1234.567432, 3453.736379, 3453.736379, 0989.726332, 3453.736379, 123
    4.567432, 1234.567432, 0989.726332'
    3 txt
    4 FROM DUAL)
    5 SELECT DISTINCT TRIM(CHAINE)
    6 FROM T
    7 MODEL
    8 RETURN UPDATED ROWS
    9 DIMENSION BY (0 POSITION)
    10 MEASURES (CAST( ' ' AS VARCHAR2(50)) AS CHAINE ,txt ,LENGTH(REGEXP_REPLACE(txt,'[^,]+',''))+1 NB_MOT)
    11 RULES
    12 (CHAINE[FOR POSITION FROM  1 TO NVL(NB_MOT[0],1) INCREMENT 1] =
    13 CASE WHEN NB_MOT[0] IS NULL THEN TXT[0] ELSE REGEXP_SUBSTR(txt[0],'[^,]+',1,CV(POSITION)) END );
    TRIM(CHAINE)
    3453.736379
    1234.567432
    0989.726332
    SQL>
    ========================================================================

  • Selecting Records based on multiple values in one field

    Excuse the basic request.
    I want to select all records from one table depending on the value in 2 specific fields. Each of these fields has a single value in it.
    What I have written is as below
    1.select nm_idno "EZAID", nm_fname, nm_lname, nm_et_tt "Position", nm_mt_mt "Status", nm_type, nm_wemal, nm_mailshot from MNME
    2.where nm_type = 'M' and where nm_mailshot = 'Y' and nm_wemal > ' '
    3.and where nm_mt_mt = (1 or 6 or 3 or 10) *** these are the bits that I don't understand why they will not work.
    4. and where nm_et_tt = (408 or 329 or 283)
    Basically every record should have one of the values I have highlighted in it.
    Any help greatly appreciated.

    select nm_idno "EZAID"
          ,nm_fname
          ,nm_lname
          ,nm_et_tt "Position"
          ,nm_mt_mt "Status"
          ,nm_type
          ,nm_wemal
          ,nm_mailshot
    from   MNME
    where  nm_type = 'M'
    and    nm_mailshot = 'Y'
    and    nm_wemal > ' ' --< not sure what you mean here
    and    nm_mt_mt IN (1, 6, 3, 10)
    and    nm_et_tt IN (408, 329, 283)
    ;

  • Selecting records based on user formula

    Post Author: Josh@RTA
    CA Forum: Formula
    I'm writing reports for a company that stores all of their dates as 8 digit numerical fields rather than a date or datetime datatype. I want to convert this field to a date type, then compare it to a parameter that stores user input as a date type.
    However, due to the way that Crystal does it's passes over the data, I can't use a selection formula based off of another formula. So I'm wondering , has anyone ever used a selection formula that references another formula and how have you been able to do it? Maybe use group selection instead of record selection? Just not sure.
    I'm including the formula I'm using to convert the date, as well as the selection formula so you get an Idea of what I'm doing.
    //This converts the numeric 'date' field to a dateshared stringvar DateString := totext({wotrans.ROP_TRAN_DATE}, 0, '');shared datevar ConvertedDate :=If {wotrans.ROP_TRAN_DATE} < 19590101 then Date (1959, 01, 01) else Date ( Val (DateString &#91;1 to 4&#93;),            Val (DateString &#91;5 to 6&#93;),            Val (DateString &#91;7 to 8&#93;) );
    //This is the select statement I'm using to compare the above formula to my parameter using record selection{@ConvertedTransDate} = {?TransDateRange}

    Post Author: SKodidine
    CA Forum: Formula
    Replace your formula with this and then equate it to your parameter value in your selection criteria and see if it will work.
    If {wotrans.ROP_TRAN_DATE} <= 19590101 then Date (1959, 01, 01)
    else
    date(
    tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')&#91;1 to 4&#93;),
    tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')&#91;5 to 6&#93;),
    tonumber(totext({wotrans.ROP_TRAN_DATE},0,'','')&#91;7 to 8&#93;));
    The process might be faster if you convert or change the data type of your parameter to numeric and then compare to the numeric date.

  • Select records based on criteria and update those records once read

    hi,
    I am very new to bpel and DB adapters.
    I have a requirement where in I need to query two tables to fetch some records and update these selected records with a new value for field to indicate that bpel has processed these records.
    Once I select these I needs the output to be mapped to the output variable.
    I am able to select the records based on criteria , but how will i lock these records so that these records do not get processed again. This should be a very simple usecase just that I am not aware.
    Thanks,
    Robin

    Once you have finished reading the records fire an update query , update some field in the table so that it does not get picked up next time.
    if you are using polling for picking up the records, then use logical delete scenario, refer....http://docs.oracle.com/cd/E15523_01/integration.1111/e10231/adptr_db.htm#BABEEBIH

  • Sum rows of Column A based on distinct values of Column B

    Hello everyone,
    below is my xml
    <ROWSET>
    <ROW>
    <ORDER_NO>103-4385509</ORDER_NO>
    <SITE_NO>103</SITE_NO>
    <ORDER_ID>4385509</ORDER_ID>
    <CUSTOMER_ID>2676832</CUSTOMER_ID>
    <TAX_AMOUNT>.33</TAX_AMOUNT>
    <CREATED_DATE>08/20/2010</CREATED_DATE>
    <USER_CREATED>TSDAL671</USER_CREATED>
    <Delivery_Method>CARRY OUT STORE</Delivery_Method>
    <Entered_By>TSDAL671</Entered_By>
    <SKU_NO>321182</SKU_NO>
    <NAME_TEXT>MGR_OVERRIDE</NAME_TEXT>
    <ATTRIBUTE_ID>319</ATTRIBUTE_ID>
    <ATTRIBUTE_VALUE>Override Done</ATTRIBUTE_VALUE>
    <DETAIL_SEQ_NO>1</DETAIL_SEQ_NO>
    </ROW>
    <ROW>
    <ORDER_NO>103-4385509</ORDER_NO>
    <SITE_NO>103</SITE_NO>
    <ORDER_ID>4385509</ORDER_ID>
    <CUSTOMER_ID>2676832</CUSTOMER_ID>
    <TAX_AMOUNT>.33</TAX_AMOUNT>
    <CREATED_DATE>08/20/2010</CREATED_DATE>
    <USER_CREATED>TSDAL671</USER_CREATED>
    <Delivery_Method>CARRY OUT STORE</Delivery_Method>
    <Entered_By>TSDAL671</Entered_By>
    <SKU_NO>321182</SKU_NO>
    <NAME_TEXT>OEDTL_TAX_INFO</NAME_TEXT>
    <ATTRIBUTE_ID>314</ATTRIBUTE_ID>
    <ATTRIBUTE_VALUE>441130760|441130760|441130760|1|1|1|20100820|2676832|2|SPARTS|</ATTRIBUTE_VALUE>
    <DETAIL_SEQ_NO>1</DETAIL_SEQ_NO>
    </ROW>
    <ROW>
    <ORDER_NO>103-4385509</ORDER_NO>
    <SITE_NO>103</SITE_NO>
    <ORDER_ID>4385509</ORDER_ID>
    <CUSTOMER_ID>2676832</CUSTOMER_ID>
    <TAX_AMOUNT>.18</TAX_AMOUNT>
    <CREATED_DATE>08/20/2010</CREATED_DATE>
    <USER_CREATED>TSDAL671</USER_CREATED>
    <Delivery_Method>CARRY OUT STORE</Delivery_Method>
    <Entered_By>TSDAL671</Entered_By>
    <SKU_NO>412679</SKU_NO>
    <NAME_TEXT>OEDTL_TAX_INFO</NAME_TEXT>
    <ATTRIBUTE_ID>314</ATTRIBUTE_ID>
    <ATTRIBUTE_VALUE>441130760|441130760|441130760|1|1|1|20100820|2676832|2|0035|</ATTRIBUTE_VALUE>
    <DETAIL_SEQ_NO>2</DETAIL_SEQ_NO>
    </ROW>
    i have to display 3 rows but while doing sum of TAX_AMOUNT grouped by ORDER_ID, i need to sum only for 2 records based on the distinct value of DETAIL_SEQ
    so my output should be some thing like below
    ORDER_ID|TAX_AMOUNT|DETAIL_SEQ|ATTRIBUTE_ID
    4385509|0.33|1|319
    4385509|0.33|1|314
    4385509|0.18|2|314
    SUM = 0.51
    Note : i cannot do distinct in the sql becuase attribute_id is different for same DETAIL_SEQ_NO.
    I tired doing <?sum([xdoxslt:distinct_values(current-group()/DETAIL_SEQ_NO)]/TAX_AMOUNT)?>
    it didn't work.
    Can anyone please help me!!
    Thanks in Advance!!

    That syntax wont work.
    One method of doing.
    <?for-each-group:/ROWSET/ROW;ORDER_NO?>
    <?for-each:current-group()?>
    <?ORDER_NO?>  <?TAX_AMOUNT?>  <?DETAIL_SEQ?>  <?ATTRIBUTE_ID?>
    <?end for-each?> <?xdoxslt:set_variable($_XDOCTX,'sum_attr',0)?>
    <?for-each-group:current-group();DETAIL_SEQ_NO?>
    <?xdoxslt:set_variable($_XDOCTX,'sum_attr', xdoxslt:get_variable($_XDOCTX,'sum_attr')+ TAX_AMOUNT)?>
    <?end for-each?><?xdoxslt:get_variable($_XDOCTX,'sum_attr')?>
    <?end for-each?>

  • Selecting records based on formula fields

    Post Author: Mike Kennedy
    CA Forum: Formula
    I have created a field (called "Shortage") that is the result of subtracting two running totals and have inserted all three fields into the group footer.  I only want to select those records that have a negative value as a result, but the field is not shown in the Select Expert.  None of my formula fields show up in the Select Expert for some reason (normally they do).  Does anyone know why and how can I select these records only?  Thanks.

    Post Author: SKodidine
    CA Forum: Formula
    Running Totals are only available once the records are being read and processed.  You are calculating the difference between two Running Totals and then want to use that to select records?  I don't think that is possible.  One way of accomplishing the display of groups which have a negative value is to use sum functions instead of the running totals and then in group selection criteria, display only those groups which have a negative value as a result of subtracting the two sums.

  • Selecting records based on different fields

    Post Author: timg
    CA Forum: General
    I have three groups which I study quite often, the blues (as identified in field 1with codes), the reds (identified in field 2 with codes), and the greens (identified in field 3 with certain codes).
    I want to be able to run one report which, through a parameter field, I can select the group (red, green, or blue) for which I can select records from the database and analyze. Stated another way, if I select "Blue" in the parameter field, how can I get CR to pull certain records in the correct field with the correct codes.

    Post Author: yangster
    CA Forum: General
    dunno what your codes are but something like this will work if you put it in the section expertif ?para = "BLUE" then field1 in &#91;a, b, c&#93; elseif ?para = "RED" then field2 in &#91;d, e, f&#93; elseif ?para = "GREEN" then field3 in &#91;g, h, i&#93; 

  • Distinct Values on column ov NVARCHAR2 Type

    Hi,
    I have a Table with a Nvarchar2(255) Column on a 10.2.0.4 Database.
    I collected statistics on the table on different ways, with and without histograms. I know the table has about 65.000 distinct values, but dba_tab_col_statistics always shows 10 Rows.
    For a test I created a new column of type varchar2(255) and copied the data from the nvarchar2 in the varchar2 column. Again i collected statistics, and for the varchar2 column it shows the correct value.
    any ideas ?
    Regards
    PK
    Here some more data:
    select count(distinct(REPOSID_XMETA)) from tmp;COUNT(DISTINCT(REPOSID_XMETA))
    63933
    alter table tmp add tmp_col varchar2(255);Table altered.
    >desc tmp
    Name Null? Type
    REPOSID_XMETA NVARCHAR2(255)
    TMP_COL VARCHAR2(255)
    update tmp set tmp_col = REPOSID_XMETA;63933 rows updated.
    commit;Commit complete.
    create index tmp_index1 on tmp (REPOSID_XMETA);Index created.
    create index tmp_index2 on tmp (tmp_col);Index created.
    begin
    dbms_stats.gather_table_stats(
    ownname=> 'SYS',
    tabname=> 'TMP' ,
    estimate_percent=> null,
    cascade=> TRUE,
    degree=> 4,
    no_invalidate=> FALSE,
    granularity=> 'AUTO',
    method_opt=> 'FOR ALL INDEXED COLUMNS');
    end; /
    select COLUMN_NAME, NUM_DISTINCT, DENSITY, NUM_NULLS, NUM_BUCKETS from dba_tab_col_statistics where table_name = 'TMP2';Column Distinct
    Name Values Density NUM_NULLS NUM_BUCKETS
    REPOSID_XMETA 10 0 0 10
    TMP_COL 63,933 0 0 75
    Edited by: user1131374 on Jul 1, 2011 5:27 AM

    Sorry, wrong cut and paste. TMP2 is a copy of TMP. Nethertheless, same query on TMP shows same results:
    select COLUMN_NAME, NUM_DISTINCT, DENSITY, NUM_NULLS, NUM_BUCKETS from dba_tab_col_statistics where table_name = 'TMP';COLUMN_NAME NUM_DISTINCT DENSITY NUM_NULLS NUM_BUCKETS
    REPOSID_XMETA 10 7.8207E-06 0 10
    TMP_COL 63933 .000015641 0 75
    regards
    Paolo

  • Can a subreport select records based on values in the main report record?

    Post Author: calvin
    CA Forum: General
    Perhaps my understanding of a subreport is incorrect as this is the first time I've used one, but it seems to me that the subreport should be able to use the values from the main report record in its (the subreport's) operations-but my subreport doesn't seem to be working that way.In my main report, I select a set of records from a 'request' table. I have a subreport in the detail section so the subreport is processed for each of the request records. That works, but I'm simply getting the same data reported multiple times. It's as if the subreport only uses the last request record rather than the current one. Stating it this way I can see that the problem might be evaluation time-it's processing the request records first, then processing the subreport, and only then printing everything. If this is correct then putting WhilePrintingRecords on the subreport should work-but the only way I know of to do that is in a formula. Can I call the subreport from a formula? Or am I totally off-track?Thanks.

    Post Author: foghat
    CA Forum: General
    Have you established a link(s) between your main report and subreport?When viewing the main report, click edit --> subreport links and link the 2 based on whatever values from the main report you want.

  • Select records based on 1 field have either of 2 values

    Good afternoon,
    II have 2 tables, Account and Order, it's a one to many where 1 account can have many orders. I'm trying to pull the records where the account has orders that have a status of Completed or Quoted. I know I'm not explaining well so hopefully the example below will help.
    Account A has 3 orders, all of them are completed.
    Account B has 5 orders, 2 of them are completed, and 3 of them are quoted.
    Account C has 2 orders, all of them are quoted.
    I only want Account B to show up in my report. That is, I only want accounts that have both completed and quoted orders
    I've tried various record selections but because the status field on a single order can only have one value I'm not sure how to get the results that I need.
    Any help would be greatly appreciated.
    Jeannette

    hi Jeanette,
    for this you may either have to write a report with a Command based object with a subselect and a WHERE IN clause.
    there's an easier way using a Group Selection formula though...the disadvantage of this method is that this doesn't filter those records out at the database which could be a problem if you're dealing with 100's of thousands of records. if that's the case, then use the method mentioned at the top.
    For a Group Selection method try the following:
    1) create a Group on the Accountfield
    2) create a formula called Completed that reads something like
    if = 'Completed' then 1
    3) create another formula called 'Quoted' that reads something like
    if = 'Quoted' then 1
    4) put these fields on the report and put a Summary on them at the Account Group level
    5) go into the Group Selection Formula and find your group level summaries and create a formula similar to
    Sum(AccountGroup, @Completed) > 0 and Sum(AccountGroup,@Quoted) > 1
    This will bring back only Accounts with both completed and quoted records.
    cheers,
    jamie

  • Selecting records based on a max value of a field?

    I want to do a select on a table and only get those records that
    have the maximum cum order lead time within a supplier_id group.
    I want to do this within SQL, not a procedure.
    create table bamb.ta_test (
    FO_ID NUMBER(10),
    CC_ID NUMBER(10),
    SUPPLIER_ID NUMBER(10),
    CUM_ORDER_LEAD_TIME NUMBER(10),
    ORDER_LEAD_TIME NUMBER(10));
    insert into bamb.ta_test values(886,550,6,256,13);
    insert into bamb.ta_test values(886,550,6,410,10);
    insert into bamb.ta_test values(886,550,12,190,10);
    insert into bamb.ta_test values(886,550,12,168,48);
    insert into bamb.ta_test values(886,550,27,91,22);
    insert into bamb.ta_test values(886,550,27,179,17);
    FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
    886 550 6 256 13
    886 550 6 410 10
    886 550 12 190 10
    886 550 12 168 48
    886 550 27 91 22
    886 550 27 179 17
    6 rows selected.
    what I want to end up with is:
    FO_ID CC_ID SUPPLIER_ID CUM_ORDER_LEAD_TIME ORDER_LEAD_TIME
    886 550 6 410 10
    886 550 12 190 10
    886 550 27 179 17
    How would I do this? [email protected]

    Hi Timothy!
    Try a nested query as:
    Greetings Peter
    select ta_test.FO_ID, ta_test.CC_ID,
    ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
    ta_test.ORDER_LEAD_TIME
    from
    ( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as max_colt
    from ta_test
    group by SUPPLIER_ID
    ) subquery_01,
    ta_test
    where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
    and
    subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
    SQLWKS> select ta_test.FO_ID, ta_test.CC_ID,
    2> ta_test.SUPPLIER_ID, ta_test.CUM_ORDER_LEAD_TIME,
    3> ta_test.ORDER_LEAD_TIME
    4>
    5> from
    6> ( select SUPPLIER_ID, max(CUM_ORDER_LEAD_TIME) as
    max_colt
    7> from ta_test
    8> group by SUPPLIER_ID
    9>
    10> ) subquery_01,
    11> ta_test
    12>
    13> where subquery_01.SUPPLIER_ID = ta_test.SUPPLIER_ID
    14> and
    15> subquery_01.max_colt = ta_test.CUM_ORDER_LEAD_TIME
    16>
    FO_ID CC_ID SUPPLIER_I CUM_ORDER_ ORDER_LEAD
    886 550 6 410 10
    886 550 12 190 10
    886 550 27 179 17

Maybe you are looking for