Query  to find 10 latest records

hi all,
i have 100 records with following attributes.
username
mobile no
date
i want to select 10 latest rocords for a perticular mobile no(based on date attribute).
regards,
uttam

Hi Mohan,
I would use the same analytic function to get the records. However, I will have to modify the expression to get the records starting from the latest date as posted by Uttam. I just added the option DESC to start from the latest record and run down to the earliest.
select * from(
select username, mobile_no, date,
rank() over(partition by mobile_no order by date DESC) rn
from tabl1)
WHERE rn<=10;

Similar Messages

  • Query to find the latest record with respect to the current status

    Dear gurus
    I have the following data in a table
    Customernum
    bkcode
    reqtdate
    Prevstat
    currstat
    The data will be like this
    CustomerNum bkcode reqdate prevstat currstat
    5900 1 03-Aug-12 0 1
    5900 1 06-Aug-12 1 0
    5900 5 22-Jun-12 0 1
    If a customer has an issue to solved, a record is added with bkcode , register date and currstat will be 1
    If the issue is resolved for the bookingcode,a new record is added, the currentstatus will become 0. and prev stat will show 1. Row no 1 and 2 reflects this case
    If this table is queried for finding the unresolved issues. the output should be only the Last row of the above example. since issue with bookingcode 1 has been resolved
    I have trying hard to get this thing confused what to use Lead or Max
    Kindly guide me

    Hi,
    one way here:
    WITH mytable(CustomerNum, bkcode, reqdate, prevstat, currstat)
    AS
       SELECT 5900, 1, TO_DATE('03-Aug-12', 'DD-Mon-YY'), 0, 1 FROM DUAL UNION ALL
       SELECT 5900, 1, TO_DATE('06-Aug-12', 'DD-Mon-YY'), 1, 0 FROM DUAL UNION ALL
       SELECT 5900, 5, TO_DATE('22-Jun-12', 'DD-Mon-YY'), 0, 1 FROM DUAL
    SELECT CustomerNum, bkcode, reqdate, prevstat, currstat
      FROM (SELECT a.*
                 , ROW_NUMBER() OVER (PARTITION BY CustomerNum, bkcode
                                           ORDER BY reqdate DESC) AS rn
              FROM mytable a
    WHERE rn=1
       AND currstat=1;
    CUSTOMERNUM     BKCODE REQDATE     PREVSTAT   CURRSTAT
           5900          5 22-JUN-12          0          1Regards.
    Al
    Edited by: Alberto Faenza on Dec 18, 2012 5:23 PM
    Changed again!! Previous logic was wrong

  • SQL Query issue find difference in records

    Hi All,
    I am using oracle 10g. I need urgent help in finding difference in records based on date:
    I have table sales as below:
    salesman SALES_COUNT DATE
    JOHN 20 04/01/2012
    DENNY 15 04/01/2012
    JOHN 30 04/02/2012
    DENNY 30 04/02/2012
    JOHN 45 04/03/2012
    DENNY 50 04/03/2012
    SALES_COUNT is increasing for sales man with date. Its like cumulative count. John has total sales from 04/01/2012 to 04/03/2012 is 50 and same case with Denny. This SALES_COUNT will keep on increasing with dates as sales keep adding in the table for each salesman.
    But i want to have seprate counts for each salesman.
    for e.g: SALES_COUNT JOHN on 04/02/2012 is 30-20 =10
    SALES_COUNT JOHN on 04/03/2012 is 45-30 =15
    SALES_COUNT DENNY on 04/02/2012 is 30-15 =15
    SALES_COUNT JOHN on 04/03/2012 is 50-30 =20
    Please help me with this scenario and let me know if need more information. I will greatly appreciate your help.
    Thanks.

    Does this give you what you want?
    with t as (
         select 'JOHN' salesman, 20 sales_count, to_date('04/01/2012', 'mm/dd/yyyy') sale_date from dual
         union all
         select 'DENNY' salesman, 15 sales_count, to_date('04/01/2012', 'mm/dd/yyyy') sale_date from dual
         union all
         select 'JOHN' salesman, 30 sales_count, to_date('04/02/2012', 'mm/dd/yyyy') sale_date from dual
         union all
         select 'DENNY' salesman, 30 sales_count, to_date('04/02/2012', 'mm/dd/yyyy') sale_date from dual
         union all
         select 'JOHN' salesman, 45 sales_count, to_date('04/03/2012', 'mm/dd/yyyy') sale_date from dual
         union all
         select 'DENNY' salesman, 50 sales_count, to_date('04/03/2012', 'mm/dd/yyyy') sale_date from dual
    select salesman,
           sales_count sales_todate,
           sale_date,
           sales_count - lag(sales_count, 1, 0) over (partition by salesman order by sale_date) daily_sales
    from t
    SALESMAN,SALES_TODATE,SALE_DATE,DAILY_SALES
    DENNY,15,4/1/2012,15
    DENNY,30,4/2/2012,15
    DENNY,50,4/3/2012,20
    JOHN,20,4/1/2012,20
    JOHN,30,4/2/2012,10
    JOHN,45,4/3/2012,15
         

  • Query to find out empty records

    Hi experts,
    I want to find out those tables in my databases which does not have any records or data. I have many tables in my databases which are totally empty. Please help me to build a query . Which database view i should access to find out the tables with empty records? Please help.

    Hello,
    Just pass the user list you want to count and not admin users or other default users.
    SELECT owner, table_name, num_rows
          FROM dba_tables
          WHERE owner IN ('MYUSER','OTHERUSER');or this
    DECLARE
       CURSOR mycur
       IS
          SELECT owner, table_name, num_rows
          FROM dba_tables
          WHERE owner NOT IN ('SYS', 'SYSTEM', 'SYSMAN', 'OUTLN')
                AND iot_name IS NULL;
       v_sql     VARCHAR2 (100);
       v_count   NUMBER := 0;
    BEGIN
       FOR cur IN mycur
       LOOP
          v_sql     := NULL;
          v_count   := 0;
          v_sql     :=
             'select count (*) from ' || cur.owner || '.' || cur.table_name;
          EXECUTE IMMEDIATE v_sql INTO v_count;
          IF (v_count = 0)
          THEN
             DBMS_OUTPUT.put_line(   'Table name = '
                                  || cur.table_name
                                  || ' rows '
                                  || v_count);
          END IF;
       END LOOP;
    END;Regards

  • Query to find the latest two transactions for each Group

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

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

  • Query to find today's records?

    Hello,
    I am working on a little CF project and I need to find all
    records that have today's date (time would also be included on the
    field, in case that is relevant) What query would you use to
    retrieve all records entered "today"? Thank you!

    No dateValue () has been part of Microsoft Access for a while
    now. What version of Access are you using?
    Are you using Access as a front end to MS SQL?
    Anyway, there is something else going on.
    Turn on all debugging options and post the EXACT, FULL, error
    message here.

  • How to find out the latest Record in per_all_people_f and per_all_assignme

    Hi ,
    How to find out the latest Record in per_all_people_f and per_all_assignments_f
    Requirement : Need to find out the latest record in per_all_people_f and per_all_assignments_f to update the attribute column with pre defined value . Its not possible to track only with person_id / assignment_id and effective end date
    SELECT pp_id
    FROM (SELECT app.person_id pp_id,
    asf.*
    FROM apps.per_all_people_f app,
    apps.per_all_assignments_f asf
    WHERE --app.person_id=123568 and
    asf.person_id = app.person_id AND
    app.effective_end_date = to_date('31-dec-4712') AND
    asf.effective_end_date = to_date('31-dec-4712')
    GROUP BY app.person_id)
    HAVING COUNT(pp_id) > 1
    GROUP BY pp_id
    This query also returns more than 1 value for person_id .
    It would be great if you put comment on this .. Thanks in advance ,
    Arya

    I am getting more records with asf.primary_flag='Y' . If you give ur mail id , i will send the sample data
    ASSIGNMENT_ID     EFFECTIVE_START_DATE     EFFECTIVE_END_DATE     BUSINESS_GROUP_ID     RECRUITER_ID     GRADE_ID     POSITION_ID     JOB_ID     ASSIGNMENT_STATUS_TYPE_ID     PAYROLL_ID     LOCATION_ID     PERSON_REFERRED_BY_ID     SUPERVISOR_ID     SPECIAL_CEILING_STEP_ID     PERSON_ID     RECRUITMENT_ACTIVITY_ID     SOURCE_ORGANIZATION_ID     ORGANIZATION_ID     PEOPLE_GROUP_ID     SOFT_CODING_KEYFLEX_ID     VACANCY_ID     PAY_BASIS_ID     ASSIGNMENT_SEQUENCE     ASSIGNMENT_TYPE     PRIMARY_FLAG     APPLICATION_ID     ASSIGNMENT_NUMBER     CHANGE_REASON     COMMENT_ID     DATE_PROBATION_END     DEFAULT_CODE_COMB_ID     EMPLOYMENT_CATEGORY     FREQUENCY     INTERNAL_ADDRESS_LINE     MANAGER_FLAG     NORMAL_HOURS     PERF_REVIEW_PERIOD     PERF_REVIEW_PERIOD_FREQUENCY     PERIOD_OF_SERVICE_ID     PROBATION_PERIOD     PROBATION_UNIT     SAL_REVIEW_PERIOD     SAL_REVIEW_PERIOD_FREQUENCY     SET_OF_BOOKS_ID     SOURCE_TYPE     TIME_NORMAL_FINISH     TIME_NORMAL_START     BARGAINING_UNIT_CODE     LABOUR_UNION_MEMBER_FLAG     HOURLY_SALARIED_CODE     REQUEST_ID     PROGRAM_APPLICATION_ID     PROGRAM_ID     PROGRAM_UPDATE_DATE     ASS_ATTRIBUTE_CATEGORY     ASS_ATTRIBUTE1     ASS_ATTRIBUTE2     ASS_ATTRIBUTE3     ASS_ATTRIBUTE4     ASS_ATTRIBUTE5     ASS_ATTRIBUTE6     ASS_ATTRIBUTE7     ASS_ATTRIBUTE8     ASS_ATTRIBUTE9     ASS_ATTRIBUTE10     ASS_ATTRIBUTE11     ASS_ATTRIBUTE12     ASS_ATTRIBUTE13     ASS_ATTRIBUTE14     ASS_ATTRIBUTE15     ASS_ATTRIBUTE16     ASS_ATTRIBUTE17     ASS_ATTRIBUTE18     ASS_ATTRIBUTE19     ASS_ATTRIBUTE20     ASS_ATTRIBUTE21     ASS_ATTRIBUTE22     ASS_ATTRIBUTE23     ASS_ATTRIBUTE24     ASS_ATTRIBUTE25     ASS_ATTRIBUTE26     ASS_ATTRIBUTE27     ASS_ATTRIBUTE28     ASS_ATTRIBUTE29     ASS_ATTRIBUTE30     LAST_UPDATE_DATE     LAST_UPDATED_BY     LAST_UPDATE_LOGIN     CREATED_BY     CREATION_DATE     TITLE     OBJECT_VERSION_NUMBER
    931510     7-Nov-08     31-Dec-12     122     (null)     (null)     (null)     3978     1     (null)     14402     (null)     220150     (null)     734956     (null)     (null)     476     (null)     (null)     (null)     (null)     2     E     Y     (null)     100035417-2     (null)     (null)     (null)     45948739     (null)     (null)     (null)     (null)     (null)     (null)     (null)     868007     (null)     (null)     (null)     (null)     449     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)                                        
    797386     26-Aug-08     26-Aug-08     122     (null)     (null)     (null)     3980     3     (null)     14402     (null)     218925     (null)     734956     (null)     (null)     476     (null)     (null)     (null)     (null)     1     E     Y     (null)     100035417     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     740071     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)                                        
    916076     26-Aug-08     31-Dec-12     122     (null)     (null)     (null)     3980     1     4     14402     (null)     218925     (null)     734956     (null)     (null)     476     (null)     (null)     (null)     (null)     1     B     Y     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)                                        
    797386     25-Feb-08     25-Aug-08     122     (null)     (null)     (null)     3980     1     (null)     14402     (null)     218925     (null)     734956     (null)     (null)     476     (null)     (null)     (null)     (null)     1     E     Y     (null)     100035417     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     740071     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)     (null)

  • Find the latest updated or the latest inserted record  in a table

    Hi All,
    Thanks in advance
    Just a simple question
    How do we find the latest updated or the latest inserted record in a table ?
    Provide some queries in SQL?

    You can order by rowid desc to get lately inserted records, but I'm not sure about updated records.That is incorrect, Oracle might use old rowid's even in inserts and you cannot assure that the max(rowid) refers to the latest record.
    If the table is created with rowdependencies one can use ORA_ROWSCN pseudo column to check on date/time when the last dml has been performed over that table. But, that has some limitations too, Old snapshots will be erased hence one can check the last dml with a time frame of few days.
    Regards,
    Prazy

  • Query to find records with more than 2 decimal places

    I have written the below query to find records with more than 2 decimal places, but it is returning records with decimal places 1 & 2.
    The datatype of the AMT column is NUMBER (without any precision).
    SELECT amt  FROM amount_table
    WHERE substr(amt, instr(amt, '.')) LIKE '.%'
           AND length(substr(amt, instr(amt, '.') + 1)) > 2Output:-
    AMT
    *41591.1*
    *275684.82*
    *64491.59*
    *3320.01*
    *6273.68*
    *27814.18*
    *30326.79*
    131.8413635
    162.5352898
    208.5203816
    8863.314632
    22551.27856
    74.716992
    890.0158441
    2622.299682
    831.6683841
    *1743.14*
    2328.195877
    3132.453438
    5159.827334
    3.236234727
    37.784
    Thanks

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    create table amount_table
      LINE_NUMBER        NUMBER not null,
      FEE_AMT            NUMBER not null
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (60208, 41591.1);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (60213, 275684.82);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (60238, 64491.59);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (63026, 3320.01);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (59906, 6273.68);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (83111, 27814.18);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (83114, 30326.79);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112395, 131.8413634682);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112399, 162.5352898104);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112402, 208.5203815738);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112403, 8863.3146321954);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112406, 22551.2785551322);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112407, 74.716992);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112410, 890.015844079);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112411, 2622.2996817048);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112414, 831.6683840698);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112415, 1743.14);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112418, 2328.1958771886);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112419, 3132.4534379886);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112422, 5159.8273341686);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112423, 3.2362347266);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112426, 37.784);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112427, 198.7423503696);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112430, 0.7220848332);
    insert into amount_table (LINE_NUMBER, FEE_AMT)
    values (2112433, 12.4149375254);

  • Query to find out 2 columns duplicate records

    hi
    how can i find out duplicate records from a table with 2columns duplicated
    eg: emp_id, book_id duplicated together
    emp_id book_id
    001 A
    001 A
    001 B
    in this case query should return ( emp_id: 001 , book_id: A ) because these are duplicated together

    SQL> with t as
      2  (
      3  select 1 a,'A' b from dual
      4  union all
      5  select 1,'A' from dual
      6  union all
      7  select 1,'B' from dual
      8  )
      9  select distinct a,b
    10    from (
    11  select t.*,count(1) over (partition by a,b order by a) cnt
    12    from t)
    13   where cnt > 1
    14  /
             A B
             1 A
    SQL> with t as
      2  (
      3  select 1 a,'A' b from dual
      4  union all
      5  select 1,'A' from dual
      6  union all
      7  select 1,'B' from dual
      8  )
      9   select a,b
    10     from t
    11    group by a,b
    12   having count(*) > 1
    13  /
             A B
             1 A

  • Query to find all tasks on latest published version of a project

    Hello
    Can anybody help me on the query to find all tasks under latest published version of a project?
    Thanks!!

    Do you mean  "All tasks under latest published workplan version"??

  • Returning latest records based on specific conditions

    Hi Everyone, Thanks for everyone response for my queries. All responses are highly appreciated. I need one help in developing Sql query. I am
    using Oracle 11.2.
    I have table Test as below having columns as, ID;Name;Position;Level;Date_Added;Flag_Name;
    Suppose I have below data in table:
    ID       Name      Position      Level              Date_Added                Flag_Name
    1       Jack       Manager        10         10/29/2013 11:00:00 PM          Match
    2       Tom        Supervisor     20            10/31/2013 9:00:00 AM       NoMatch
    1       Jack       Manager        15          11/1/2013  10:00:00 AM          Error
    1       Jack       Manager        20         10/1/2013  9:00:00 AM           NoMatch
    3       John       Salesman      5           10/4/2013  2:00:00 PM           Error
    1       Jack       Manager        17         9/28/2013  2:00:00 AM           NoMatch
    2       Tom        Supervisor     12         11/2/2013  10:00:00 AM          NoMatch
    4       Remy       Accountant     12        11/4/2013  1:00:00 PM           Error
    4       Remy       Accountant     19         11/3/2013  2:00:00 AM           Error
    2       Tom        Supervisor     10            10/29/2013 7:00:00 PM           Error
    I need to return latest data in query based on combination of (ID;Name;Position) and date range with below conditions:
    1) Need to return latest records for Flag_Name other than Match.
    2) If latest record for combination of (ID;Name;Position) will come with Flag_Name as Error in that specified date range, then we need to look for this combination has Flag_Name as NoMatch anywhere before in table.
    3) If Flag_Name as NoMatch is found for this, then we need to return it. In this case also, if we found multiple records with Flag_Name as NoMatch for combination of (ID;Name;Position), then we need to return latest one.
    4) Else we find no record for combination of (ID;Name;Position) with Flag_Name as NoMatch in table, then we can return the latest record with Flag_Name as Error.
    While select ID;Name;Position should be unique combination, as we always return latest data for it.
    As per above table, if I need to return data for date range between 29-Oct-2013 and 5-Nov-2013, expected result will be like below:
    ID       Name      Position      Level         Date_Added                      Flag_Name    
    1       Jack       Manager        20              10/1/2013  9:00:00 AM           NoMatch
    2       Tom        Supervisor     12             11/2/2013  10:00:00 AM          NoMatch
    3       John       Salesman        5              10/4/2013  2:00:00 PM           Error
    4       Remy       Accountant     12           11/4/2013  1:00:00 PM           Error
    Please let me know if anyone has any doubt about the logic. Is it possible if we can do it in select statement rather than pl/sql code?
    All comments/suggestions will be highly appreciated.
    Thanks in advance.
    Regards
    Dev    

    First i didn't read topic exactly )))
    here is second way. I get with stmt from Karthick (thanks)
    with t
        as
        select 1 id, 'Jack' name, 'Manager'    position, 10 lvl, to_date('10/29/2013 11:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Match'   flag_name
          from dual union all
        select 2 id, 'Tom'  name, 'Supervisor' position, 20 lvl, to_date('10/31/2013 09:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
          from dual union all
        select 1 id, 'Jack' name, 'Manager'    position, 15 lvl, to_date('11/1/2013  10:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
          from dual union all
      select 1 id, 'Jack' name, 'Manager'    position, 20 lvl, to_date('10/1/2013  09:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
        from dual union all
      select 3 id, 'John' name, 'Salesman'   position,  5 lvl, to_date('10/4/2013  02:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
        from dual union all
      select 1 id, 'Jack' name, 'Manager'    position, 17 lvl, to_date('9/28/2013  02:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
        from dual union all
      select 2 id, 'Tom'  name, 'Supervisor' position, 12 lvl, to_date('11/2/2013  10:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
        from dual union all
      select 4 id, 'Remy' name, 'Accountant' position, 12 lvl, to_date('11/4/2013  01:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
        from dual union all
      select 4 id, 'Remy' name, 'Accountant' position, 19 lvl, to_date('11/3/2013  02:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
        from dual union all
      select 2 id, 'Tom'  name, 'Supervisor' position, 10 lvl, to_date('10/29/2013 07:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
        from dual
    select ID,
           Name,
           Position,
           max(lvl) keep (dense_rank first order by с desc, Date_Added desc) lvl,
           max(Date_Added) keep (dense_rank first order by с desc, Date_Added desc) Date_Added,
           min(Flag_Name) keep (dense_rank first order by с desc, Date_Added desc) Flag_Name
    from(
    select  ID,
                Name,
                Position,
                lvl,
                Date_Added,
                Flag_Name,           
                decode(flag_name, 'Error', -1, 1) с
    from T
    where flag_name != 'Match'
    group by ID,Name,Position
    ID
    NAME
    POSITION
    LVL
    DATE_ADDED
    FLAG_NAME
    1
    Jack
    Manager
    20
    01.10.2013 09:00:00
    NoMatch
    2
    Tom
    Supervisor
    12
    02.11.2013 10:00:00
    NoMatch
    3
    John
    Salesman
    5
    04.10.2013 14:00:00
    Error
    4
    Remy
    Accountant
    12
    04.11.2013 13:00:00
    Error
    Ramin Hashimzade
    Message was edited by: RaminHashimzadeh

  • How to find the last records of the table in adventure work db

    hi all,
    i want to find out " how to receive a last records of the table (eg:  person.emailaddress ) in sql 2008 and 2005"
    Thanks

    If you are looking for the latest record( the one which got inserted/modified last) then use this query
    select top 1 * from person.emailaddress
    Order by ModifiedDate desc
    Satheesh
    My Blog |
    How to ask questions in technical forum

  • Query to find out the time used by an user for an application

    Hello All,
    I want to know the query to find out the whole time used by the user for an application. Please view the below data
    Employee:
    SNO EMP_ID EMP_NAME EMP_DATE LOGIN_TIME LOGOUT_TIME
    1 10 Visu 21-Nov-2010 06:30:00 07:30:00
    2 10 Visu 21-Nov-2010 06:40:00 07:20:00
    3 10 Visu 21-Nov-2010 06:50:00 07:50:00
    4 10 Visu 21-Nov-2010 07:30:00 08:30:00
    5 10 Visu 21-Nov-2010 09:30:00 10:30:00
    By checking the above data we can say that the total time Visu used the application is
    8.30 - 6.30 (From 1,2,3,4 records) = 2hrs
    10.30 - 9.30 (Based on 5th rec) = 1hr
    So the total time Visu used the application would be 3 hrs = 180 mins.
    Could you please help me in getting the result from that data using a query?

    odie_63 wrote:
    I think it may be solved with analytics too.
    with t1 as (
                select 1 sno,10 emp_id,'Visu' emp_name,'21-Nov-2010' emp_date,'06:30:00' login_time,'07:30:00' logout_time from dual union all
                select 2,10,'Visu','21-Nov-2010','06:40:00','07:20:00' from dual union all
                select 3,10,'Visu','21-Nov-2010','06:50:00','07:50:00' from dual union all
                select 4,10,'Visu','21-Nov-2010','07:30:00','08:30:00' from dual union all
                select 5,10,'Visu','21-Nov-2010','09:30:00','10:30:00' from dual
         t2 as (
                select  emp_id,
                        emp_name,
                        emp_date,
                        to_date(emp_date || login_time,'DD-MON-YYYYHH24:MI:SS') login_time,
                        to_date(emp_date || logout_time,'DD-MON-YYYYHH24:MI:SS') logout_time
                  from  t1
         t3 as (
                select  t2.*,
                        case
                          when login_time < max(logout_time) over(
                                                                  partition by emp_id,emp_date
                                                                  order by login_time
                                                                  rows between unbounded preceding
                                                                           and 1 preceding
                            then 0
                          else 1
                        end start_of_group
                  from  t2
         t4 as (
                select  t3.*,
                        sum(start_of_group) over(partition by emp_id,emp_date order by login_time) grp
                  from  t3
         t5 as (
                select  emp_id,
                        emp_date,
                        min(login_time) login_time,
                        max(logout_time) logout_time
                  from  t4
                  group by emp_id,
                           emp_date,
                           grp
    select  emp_id,
            numtodsinterval(sum(logout_time - login_time),'day') time_spent
      from  t5
      group by emp_id
      order by emp_id
        EMP_ID TIME_SPENT
            10 +000000000 03:00:00.000000000
    SQL> SY.

  • How to find the longest record in a table?

    Hello,
    Is there a function to find the longest record in a table? Or is there a data dictionary that would tell you which record contains the longest data?
    I have a table with five columns and one million records. I want to find the record (5 columns combined) with the longest data. Thank you.

    Dear watson2000!
    The function "VSIZE" tells you the number of bytes in the internal representation of a column which means the size of a value within a column. An example of vsize can be found here:
    [http://www.adp-gmbh.ch/ora/sql/vsize.html]
    So I think you should try it with this query to get the size of the longest record:
    SELECT MAX(VSIZE(column1)) +
           MAX(VSIZE(column2))  +
           MAX(VSIZE(column3))  +
           MAX(VSIZE(column4))  +
           MAX(VSIZE(column5)) AS "Maximum Row"
    FROM your_table;To identify the longest record try like this:
    SELECT rowid
    FROM   your_table
    GROUP BY rowid
    HAVING  (MAX(VSIZE(column1)) +
             MAX(VSIZE(column2)) +
             MAX(VSIZE(column3)) +
             MAX(VSIZE(column4)) +
             MAX(VSIZE(column5))) = (SELECT MAX(VSIZE(column1)) +
                                          MAX(VSIZE(column2))  +
                                          MAX(VSIZE(column3))  +
                                          MAX(VSIZE(column4))  +
                                          MAX(VSIZE(column5))
                                   FROM your_table;)I hope that these two queries could be of help to you.
    yours sincerely
    Florian W.
    Edited by: Florian W. on 23.04.2009 20:53

Maybe you are looking for

  • How do I fix my processor from stuttering?

    I have a polycarbonate Macbook[Late 2009] with 2 GB RAM, 2.26 GHz Intel Core 2 Duo. Whenever I run any game, the game starts stuttering, and lagging in a strange manner. It slows down real bad for about a second or so, then back to normal for the nex

  • Unable to create the partition

    Tthe admin console show "ERR missing parameter" after I try to add the new partition. This error messsage show me after I submit to save. Please you suggest me how do i add the new partition and setting some user to used secondary partition. nickname

  • Rfc--- jdbc, how to define data types for the rfc's

    i downloaded the rfc.xsd in to local hard drive. while i am defining the data type i am getting an error. do i have to change any thing the rfc.sxd file please help me thanks

  • Codecs for Adobe Media Player

    I have Adobe Media Player installed, and it works fine for Adobe Flash. I got rid of Apple Quicktime some years back - it was a PITA. I now use the VLC player for MOV files. but I'd also like to use AMP for MOV files. I've seen a few sites that offer

  • Problem after upgrading to new jpdk

    Hi, I upgraded my Oracle Portal version from 3.0.6 to 3.0.7 .I downloaded latest jpdk and upgraded it,made changes to provider.xml with new tags. But if I now try to test the provider on browser with http://myserver.mydomain.com:port/servlet/xxx It g