SQL Query to retrieve records in a table with the same records or only one record in a table

Hello

Got a curious requirment to extract the Non-distincts records in a table

Example of

Account number Type of account
12345SB
12345

SB

12346CR
12346SB
12347SB
12348CR
12349SB

Requrirement is to retrieve the records as follows

Result must be

Account number Type of account
12345SB
12345

SB

12347SB
12348CR
12349SB

You will notice that 12346 which has two separate lines have been eliminated in the result. I tried several qeries to achieve this result, one or the other had a few mismatch. Can someone give me a request for it.

Thanks in advance.

SELECT ACCOUNT_NUMBER,

ACCOUNT_TYPE

DE)

SELECT ACCOUNT_NUMBER,

ACCOUNT_TYPE,

COUNT (*) ON CNT_ACC (ACCOUNT_NUMBER PARTITION).

COUNT (*) ON CNT_NUM_TYP (PARTITION ACCOUNT_NUMBER, ACCOUNT_TYPE)

FROM T1)

WHERE CNT_ACC = CNT_NUM_TYP;

exit;

12345 SB
12345 SB
12347 SB
12348 CR
12349 SB

Tags: Database

Similar Questions

  • SQL Query to retrieve records for all after the Max Dates.

    Hello everyone,

    I am trying to retrieve the record more recent, based on the date field ( nextDate ).

    Currently, there are only 4 records in the MC_Maintenance table and two in the Machine table.

    Machine table

    MC_id             EquipID          

    1                      0227

    MC_id EquipID

    2                     0228

    ---------------------------------

    MC_Maintenance table

    Maint_id MC_id Next_maint

    1                      2                      08/25/2010     

    2                      2                      07/01/2010

    3                      1                      2010-11-06

    4                      1                      07/11/2010

    I have am trying to accomplish is,.

    the list of the two machines of the table of the Machine with the MAX (Next_maint) control the list of MC_Maintenance results

    These are the records I want to display.

    Maint_id MC_id Next_maint

    1                      2                      08/25/2010

    4                      1                      07/11/2010                 

    Here is the SQL query

    SELECT

           MC. MC_ID as ID,

    MC.complete_Date completed.

    MC.next_maint as nextDate,.

    MC.maint_notes as noted,.

    MC.facility Facility.

    M.EquipId,.

    $m.name as the name.

    M.SerialNumber SN.

    M.dept as dept.

    M.Freq as freq

    Of MC_Maintenance MC, Machine M

            where  MC. MC_ID = M.MC_ID

    '           Using MAX (nextDate)

    Any ideas would be useful.

    TJ

    I thought it was a simple group of problem?

    SELECT M.EquipID, MC.MC_ID, Max(MC.next_maint)
    FROM MC_Maintenance MC INNER JOIN Machine M ON MC.MC_ID = M.MC_ID
    GROUP BY M.EquipID, MC.MC_ID
    
  • SQL Query to retrieve data in XML format with several levels

    Hello

    I'm looking to help generate XML from Oracle database. The request must contain many levels of sub...

    For example.
    Create table inventory
    (Group varchar2 (10))
    category varchar2 (10),
    ProductName varchar2 (10),
    Detail varchar2 (10));

    The output should be also present below. The data must be multiple values for each item.

    < inventory >
    < name of group 'auto' = >
    < category name 'cars' = >
    < name productname = 'Seat' >
    < details > red color < / details >
    < details > 5 seats < / details >
    < / productname >
    < / category >
    < / Group >
    < / inventory >

    Please share your ideas by retrieving data in the XML structure using SQL or PL/SQL for oracle 10 g.

    Thank you and best regards,
    Siva.

    This?

    SQL> create table t as
    with t as (
     select 'automobiles' "GROUP", 'cars' category, 'seat' productname, 'color red' details from dual union all
     select 'automobiles', 'cars', 'seat', '5 seats' from dual union all
     select 'automobiles', 'cars', 'make', 'ford' from dual union all
     select 'automobiles', 'cars', 'make', 'Toyota' from dual union all
     select 'automobiles', 'Bus', 'Model', 'volvo' from dual union all
     select 'automobiles', 'flight', 'Airbus', '400 passenger' from dual union all
     select 'automobiles', 'flight', 'Airbus', '500 mph' from dual union all
     select 'HealthCare', 'Nutrition', 'centrum', 'Organic' from dual union all
     select 'HealthCare', 'Nutrition', 'centrum', 'Chemical' from dual union all
     select 'Computers', 'Software', 'Database', 'Oracle' from dual union all
     select 'Computers', 'Software', 'Database', 'sqlserver' from dual
    )
    select * from t
    /
    Createtable successfully completed.
    
    SQL> select xmlserialize (content x.column_value indent) xml
      from xmltable('element Inventory
                     {for $g in distinct-values(ora:view("t")/ROW/GROUP)
                       return element group
                          {attribute name {$g},
                            for $c in distinct-values(ora:view("t")/ROW[GROUP=$g]/CATEGORY)
                              return element category {attribute name {$c},
                                for $p in distinct-values(ora:view("t")/ROW[GROUP=$g and CATEGORY=$c]/PRODUCTNAME)
                                  return element productname {attribute name {$p},
                                    for $d in distinct-values(ora:view("t")/ROW[GROUP=$g and CATEGORY=$c and PRODUCTNAME=$p]/DETAILS)
                                      return element details {$d}
                                                             }
                                                      }
                          }
                     }') x
    /
    XML
    --------------------------------------------------------------------------------
    
      
        
          
            
    Oracle
    sqlserver
    Chemical
    Organic
    volvo
    Toyota
    ford
    5 seats
    color red
    400 passenger
    500 mph
    1 row selected.
  • SQL query to retrieve a single record for each employee of the following table?

    Hi all

    Help me on the writing of SQL query to retrieve a single record for each employee of the following table? preferably a standard SQL.

    CREATE TABLE xxc_contact)

    empnum NUMBER,

    alternatecontact VARCHAR2 (100),

    relationship VARCHAR2 (10),

    phtype VARCHAR2 (10),

    Phone NUMBER

    );

    insert into xxc_contact values (123456, 'Rick Grimes', 'SP', 'Cell', 9999999999)

    insert into xxc_contact values (123456, 'Rick Grimes', 'SP', 'Work', 8888888888)

    insert into xxc_contact values (123457, 'Daryl Dixon', 'EN', 'Work', 7777777777)

    insert into xxc_contact values (123457, 'Daryl Dixon', 'EN', 'Home', 3333333333)

    insert into xxc_contact values (123456, 'Maggie Greene', 'CH', 'Cell', 9999999999)

    insert into xxc_contact values (123456, 'Maggie Greene', 'CH', 'Home', 9999999999)

    expected result:

    EmpNum AlternateContact Relationship PhType Phone       

    123456 rick Grimes SP cell 9999999999

    Daryl Dixon EN work 7777777777 123457

    Home 123458 Maggie Greene CH 6666666666

    Thanks in advance.

    994122 wrote:

    Thank you all, that I got a result

    http://www.orafaq.com/Forum/m/620305/#msg_620305

    By Lalit Kumar B...

    Specifically, the two simple solutions provided were:

    1 using the row_number, entitled Oracle ranking based on descending order of the inside telephone each empnum group. And finally selects the lines which has least rank (of least since that order is descending for phone).

    SQL > column alternatecontact format A20;

    SQL >

    SQL > SELECT empnum, alternatecontact, relationship, phtype, phone

    2 from (SELECT a.*, row_number() over r (PARTITION BY empnum ORDER BY phone / / DESC))

    3 FROM xxc_contact one)

    4. WHEN r = 1

    /

    EMPNUM ALTERNATECONTACT RELATIONSHIP PHTYPE PHONE

    ---------- -------------------- ---------- ---------- ----------

    123456 rick Grimes SP cell 9999999999

    Daryl Dixon EN work 7777777777 123457

    Home 123458 Maggie Greene CH 6666666666

    2. with the help of MAX, Oracle automatically assigns the maximum phone for all the rows in each group of empnum. And finally selects the rows with the maximum phone. Order by clause is omitted here intentionally. You can find out why.

    SQL > SELECT empnum, alternatecontact, relationship, phtype, phone

    2 (SELECT a.*, MAX (phone) over (PARTITION BY empnum) rn FROM xxc_contact one)

    3 WHERE phone = rn

    4.

    EMPNUM ALTERNATECONTACT RELATIONSHIP PHTYPE PHONE

    ---------- -------------------- ---------- ---------- ----------

    123456 rick Grimes SP cell 9999999999

    Daryl Dixon EN work 7777777777 123457

    Home 123458 Maggie Greene CH 6666666666

    Kind regards

    Lalit

  • How to export data to excel that has 2 tables with the same number of columns and the column names?

    Hi everyone, yet once landed upward with a problem.

    After trying many things to myself, finally decided to post here...

    I created a form in form builder 6i in which clicking on a button, the data gets exported to the excel sheet.

    It works very well with a single table. The problem now is that I cannot do the same with 2 tables.

    Because the tables have the same number of columns and the columns names.

    Here are the 2 tables with column names:

    Table-1 (MONTHLY_PART_1) Table-2 (MONTHLY_PART_2)
    SL_NOSL_NO
    MODELMODEL
    END_DATEEND_DATE
    U-1U-1
    U-2U-2
    U-4U-4
    ..................
    ..................
    U-20U-20
    U-25U-25

    Given that the tables have the same column names, I get the following error :

    402 error at line 103, column 4

    required aliases in the SELECT list of the slider to avoid duplicate column names.

    So how to export data to excel that has 2 tables with the same number of columns and the column names?

    Should I paste the code? Should I publish this query in 'SQL and PL/SQL ' Forum?

    Help me with this please.

    Thank you.

    Wait a second... is this a kind of House of partitioning? Shouldn't it is a union of two tables instead a join?

    see you soon

  • CF8 cfquery multiple table join with the same names of columns - default

    This seems to be a defect in the CF8 cfquery object. I'm at a loss as to a solution or a good work around. With regard to the substance, this query is generated dynamically on the fly based on what is happening in it a user. This isn't the most elegent SQL but work and return all columns. The app itself is a data viewer to look at the logs. Here is an example of a query being generated.

    Select *.

    of web_trans, web_info_trans, web_res_trans

    where (web_info_trans.trans_dte > = '' 2008-12-1)

    and web_info_trans.trans_dte < '' 2008-12-2)

    and (web_info_trans.trans_dte = web_trans.trans_dte)

    and (web_info_trans.trans_num = 5060345)

    and (web_info_trans.trans_num = web_trans.trans_num)

    and (web_res_trans.trans_num = 5060345)

    and (web_res_trans.trans_num = web_trans.trans_num)

    and (web_trans.web_trans_cde = "NTUI")

    and (web_trans.web_status_cde = 'P')

    and (web_trans. TRANS_NUM < 5060347)

    order of web_trans. TRANS_NUM / / desc

    These three tables contain a similar column called ZIP_CDE. They contain different values, and when this query is executed in Microsoft Query Analyzer results are displayed correctly. Run this same query SQL with CFQUERY and the value of one of the other tables (web_trans) will eventually replace the value for the other columns called ZIP_CDE. The exact amount columns are retruned only values some how get corrupted.

    This is a defect of cf8 is a book autour or an update that resolves this problem that I may have missed?

    Thank you for the ideas.

    Thus it seems still as a default since CF is essentially where, except for the return value play. Other query tools return results correctly so for me, it turns out be CF bug at a certain level.

    Actually I'm swinging to agree with you here, but not for the same reason.  CF actually * is * get all the columns back (as you say), it just doesn't expose a way to tell the difference between a column called 'x' and an another column called 'x', because the way to access the data in columns is simple queryname [columnName] [rowNumber] (or a Variant fo this, but all the amount of variations to that or abbrev. it).  This code shows how he has all four columns of the original two queries (being a stand-in for tables, in this case):


    Q1 = queryNew("");
    Q2 = queryNew("");
       
    queryAddColumn (q1, 'id', [1,2,3,4]);
    queryAddColumn (q1, "data", ["one", "two", "three", "four"]);
    queryAddColumn (q2, 'id', [1,2,3,4]);
    queryAddColumn (q2, "data", ["tahi", "rua", "toru", "wha"]); It's Maori, in case you are interested


    SELECT *.
    Q1, q2
    WHERE q1.id = q2.id

    So CF knows there are four columns (a call getMetadata (q3) get also demonstrates this), but it does not expose a way to approach the second (or even greater) column of the same name.  This is the bug/shortfall.

    However, relying on an underlying coldfusion.sql.QueryTable method, you can rename the columns, so:

    Then you're OK.

    I would be normally reluctant to recommend doing this, because these methods change from version to version of CF, but it's your call whether this approach will help you.

    To be honest, I'm with Owain who, even in SQL one generally doesn't work with columns with the same name, an alias them qualified with a table name/alias. As the table columns from of is not returned by SQL with the result set, it must be evidence against this by folding the columns in the first place.

    Also, I wonder at the bottom of the extraction of data, you don't really know the structure, that is to say, do SELECT *.  How can you know not even that is ours as the first column, second column, etc.?  I don't think that SQL applies in fact a contract that, t - it?  (I don't know).

    What are you actually do here?

    --

    Adam

  • It is possible to have two tables with the same name in Oracle!

    Oracle Version: 10 gr 2

    MS Access 2007, I had to use the 'Export' by which I copy a table (and its data) to an Oracle schema via an ODBC connection. Later, I realized that, during the copy of tables with a mix of lower and upper case names, the table does not copied (exported). But MS Access will give you the message that table obtained export successfully.

    MS-Access mess around Oracle data dictionary.

    When you issue
    SQL>select * from tab;
    
    TNAME                          TABTYPE  CLUSTERID
    ------------------------------ ------- ----------
    AMStates                       TABLE
    Version                        TABLE
    You will see the names of the tables. But when you try to DESCRIBE or SELECT this table, you will
    SQL>desc Version
    ERROR:
    ORA-04043: object Version does not exist
    You can even create another table with the same name in the schema
    SQL>create table VERSION (X NUMBER);
    
    Table created.
    Why this is happening and how can I bring these items 'non-existent '?

    Hello

    Use

    SQL > desc 'Version '.

    Or

    SQL > select * from 'Version '.

    Or

    SQL > drop table 'Version '.

    To overcome the problems of mixed-case.

  • Copy SQL statement to another database with the same SQL_ID

    Is there a reliable way to copy a sql statement in a database to another preserving the formatting so it ends with the same SQL_ID.

    I want to copy a statement of the shared pool to a test database to develop a plan to import sql database in production.

    In the past, I just did ' select sql_fulltext from v$ sql "in SQL Developer and pasted the result into a spreadsheet SQL Developer and run it.   The statement is now in the pool shared with the correct sql_id.

    This does not work for instruction, I am currently in train.  It ends with a different id of sql.

    I know it's probably a problem of formatting, but the statement is 1000 characters and a manual check will take forever.

    You could put in a SQL Tuning Set and transfer it.

  • Can the records with the same pictures are in the Organizer add? I have to start? I tried to organize photos in folders in a time better. I have to start all over again?

    I made new files so I could have a better time line, but the Organizer will not let me load them. The new files contain the same images, but they are better organized.  To add pictures in the folders I have to delete those in the organizer and start again?  I feel like I got this backward little and should have organized their first. Help.

    virginiae56248392 wrote:

    I made new files so I could have a better time line, but the Organizer will not let me load them. The new files contain the same images, but they are better organized.  To add pictures in the folders I have to delete those in the organizer and start again?  I feel like I got this backward little and should have organized their first. Help.

    The key to understanding the difference between the Organization of your files and folders directly in your disk folders tree and by organizing with a catalogue, is that the catalogue:

    -contains no image files, that links to the actual location on the disk

    -stores the links in a database with organizers strict rules, which is to avoid duplicates.

    One of the highlights of the databases such as those in the elements or Lightroom is that they store duplicate data. Which avoids not only of space useless, but also ensures that you're updating a file and forget its duplicates. As a result, the Organizer will prevent you from 'import', which is to 'index' your files several times. It recognizes the files into two criteria: the size of the file in kilobytes and the "date" of the shooting.

    The other strength of the catalogue, it's that you can select the sort order of your files displayed: by date, name of the file, import annealing in Bell or same "custom" order in albums. This means you don't ever need to have your physical files and folders sorted by dates on your drive. Try to get a similar organisation to the date on your car in your catalog can be an unnecessary pain. Good if you choose a folder structure to date in office at the time of importation; There must be little order at this time anyway. Once your photos are cataloged, the rule is that if you want to change the structure of your file, you must do it from the Organizer itself (left panel of record). This means that you are working with already imported files (you can not import duplicates). You can organize in this folder on the same panel as if you were in Solution Explorer / finder. You can either use the 'move' menu or drag and drop of folders with their subfolders, you can move the files of images to other subfolders. Beware, there are traps in doing this, for example by trying to move a subfolder in master already a folder with the same name of subfolder...

    So, ask yourself if all this work is really worth. And be patient and prudent to do this task from the file Organizer Panel.

  • Drop a large number of tables with the same prefix

    Hello

    can you help me to make a script to move all the tables with the same prefix = TEST_LINK_?

    Thank you very much.
    Crystal

    Hi crystal,

    Here's something to start. It does not all FK constraints on those tables and assumes that the executor is the owner.

    declare
      pstmt  constant varchar2(255) := 'drop table :t purge';
      vstmt varchar2(255);
    begin
    for rec in (select table_name
                  from user_tables
                 where table_name like 'TEST_LINK\_%' escape '\')
       loop
          vstmt := replace(pstmt, ':t', rec.table_name);
          dbms_output.put_line(vstmt || ';');
    --      execute immediate vstmt;
       end loop;
    end;
    /
    

    Concerning
    Peter

  • Need to create a new line in the table with the same data as the primary key, but new PK

    Hello gurus,

    I have a table with a column as a primary key, I need to create a new row in the table, but with the same data in one of the lines, but with different primary keys, focus a double row with key primary diferent...

    Any ideas of how it can be done without much complication?

    Thanks in advance for your answer.

    Agress,
    Karim idrissi

    user9970447 wrote:
    Hello gurus,

    I have a table with a column as a primary key, I need to create a new row in the table, but with the same data in one of the lines, but with different primary keys, focus a double row with key primary diferent...

    Any ideas of how it can be done without much complication?

    Thanks in advance for your answer.

    Agress,
    Karim idrissi

    something like

    insert into mytable values ('literal for new pk',
                                           select non-pk-1,
                                                    non-pk-2,
                                                    non-pk-n
                                           from mytable
                                           where pk-col = 'literal for existing pk')
    
  • How to create two employees with the same name as the vendor record?

    Hi all

    We must create a vendor for the employee record, so that we can issue pretty invoice for cash payment. If there are 2 employees with the same name, how can we create records of supplier for these 2 employees? The system does not have the name of the provider in duplicate.

    Please notify.

    Kind regards
    HY

    Hello

    In R12, it is possible to create 2 employee with the same name but with a different number.
    And it is also possible to create these 2 employees as suppliers in Payables.

    HTH,
    Vik

  • Create a schema of the user with the same name and tables within this scheme

    I am a newbie with Oracle.
    I installed my first Oracle 10 g database in my life.
    I need to create a user and a new schema with the same name.
    Subsequently, I need to create tables in this schema using the * isqlPlus.
    I got to create the user via the Oracle Enterprise Manager Console.
    I tried to create a schema through the same tool, but I have not found a possibility to do using GUI.
    Is it possible to do so through Oracle Enterprise Manager Console?
    What are the permissions the user must have access isqlPlus to create the tables in the schema?

    Thank you!!!

    Felipe

    A schema is just a collection of objects belonged to a particular user. If you do not need to create a separate schema: the schema is created automatically when you create the user.

    To connect to the database, a user must CREATE SESSION privilege. To create a table, the user has the CREATE TABLE privilege and should be given a quota on any tablespace will be created in the table. If you don't care management quota or limit the storage space that a user can create tables, you can grant the user the UNLIMITED TABLESPACE privilege. If you care the management of quota, you must run commands like

    ALTER USER <>
      QUOTA <>
      ON <>
    

    for each tablespace for the user to be able to use.

    Justin

  • SQL query to retrieve the same record because no. boxes

    Hi all

    I have a table of Bill XXINV.

    the columns are

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    1123                XXITEM2                     2                   6

    I can do a sql query so that I can retrieve the same records repeated as number INVH_BOXES.

    Select * from XXINV

    where invh_no = 1122

    output:

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    Desired output:

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    or

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122 XXITEM1 1 1 of 5

    1122 XXITEM1 1 2 of 5

    1122 1 3 5 XXITEM1

    1122 XXITEM1 1 4 of 5

    1122 XXITEM1 1 5 of 5

    Please suggest. Like I need to print each line in a single page to stick them on boxes so that packaging.

    Kind regards

    Afzal.

    Just add INVH_ITEM_CODE to the connection by the clause.

    Select invh_no

    invh_item_code

    invh_qty

    to_char (, 'fm99') | 'from ' | TO_CHAR (invh_boxes, 'fm99') invh_boxes

    of xxinv

    where invh_no = 1122

    connect

    by level<=>

    and prior invh_item_code = invh_item_code

    and prior sys_guid() is not null

  • SQL query for retrieving data based on Certain model

    Hi all

    I want to retrieve all the identifiers of all the people who are permanently seated during the last hour.

    Data are expressed as below:

    -Creation of the activity Table

    CREATE TABLE activity_log

    (

    Username, NUMBER of

    Activity VARCHAR2 (30),

    StartTime VARCHAR2 (6).

    EndTime VARCHAR2 (6)

    );

    -Filling with sample data

    INSERT INTO activity_log VALUES('39','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('39','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('39','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('39','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('39','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('39','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('39','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('39','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('39','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('39','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('39','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('39','Sitting','09:56','10:00');

    INSERT INTO activity_log VALUES('39','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('39','Sitting','10:06','10:10');

    INSERT INTO activity_log VALUES('39','Sitting','10:11','10:15');

    INSERT INTO activity_log VALUES('39','Sitting','10:16','10:20');

    INSERT INTO activity_log VALUES('39','Sitting','10:21','10:25');

    INSERT INTO activity_log VALUES('39','Sitting','10:26','10:30');

    INSERT INTO activity_log VALUES('39','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('39','Standing','10:36','10:40');

    INSERT INTO activity_log VALUES('39','Standing','10:41','10:45');

    INSERT INTO activity_log VALUES('39','Walking','10:46','10:50');

    INSERT INTO activity_log VALUES('39','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('39','Walking','10:56','11:00');

    INSERT INTO activity_log VALUES('40','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('40','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('40','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('40','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('40','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('40','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('40','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('40','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('40','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('40','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('40','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('40','Walking','09:56','10:00');

    INSERT INTO activity_log VALUES('40','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('40','Standing','10:06','10:10');

    INSERT INTO activity_log VALUES('40','Standing','10:11','10:15');

    INSERT INTO activity_log VALUES('40','Walking','10:16','10:20');

    INSERT INTO activity_log VALUES('40','Walking','10:21','10:25');

    INSERT INTO activity_log VALUES('40','Walking','10:26','10:30');

    INSERT INTO activity_log VALUES('40','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('40','Sitting','10:36','10:40');

    INSERT INTO activity_log VALUES('40','Sitting','10:41','10:45');

    INSERT INTO activity_log VALUES('40','Standing','10:46','10:50');

    INSERT INTO activity_log VALUES('40','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('40','Walking','10:56','11:00');

    Based on the data, the user ID 39 must be found, since it's been sitting since 09:36-10:35, which is continuous 1 hour.

    Any guidance how to do using SQL query will be of great help and appreciation.

    Thank you very much

    Kind regards

    Bilal

    So what exactly is wrong with the request that I already gave you?

    Just to remind one untested (because of lack of insert statements) rewrite according to your new data:

    with grp as)

    Select

    username

    UserRecognizedActivityID activity

    starttime

    starttime + endetime + 1

    row_number() over (partition by order of starttime userid)

    -ROW_NUMBER() over (partition of userid, UserRecognizedActivityID order of starttime)

    RN

    of activity_log

    )

    Select

    username

    min (starttime) starttime

    max (endtime) endtime

    max (activity) activity

    GRP

    Group userid, rn

    with round (max (endtime) - min (starttime) * 24 * 60) > = 59

Maybe you are looking for