NEAT SQL - time scale between two records to find...

Hello everyone, first of all please let me apologize because it's a fundamental issue, however my sql is nowhere near as good as my design skills and I'm having a headache with something...

In simple terms, I have a table which we will call "TBL_MOVEMENTS".

In this table, we record the time several doors open and close. So we have the passes "DT" (Datetime in timestamp), 'DOORID' and 'STATE '.

For the sake of examples, we will say that we want to select the hours each door was open for. (actually every door will open and close hundreds of times)

The data in the table would present a little as follows...

DT DOORID STATE
----------------------------------------------------------------------------------------
2010-01-15 09:00:01.223 OPEN A3
2010-01-15 09:02:04.789 OPEN A1
2010-01-15 09:07:55.123 NARROW A3
2010-01-15 09:08:12.654 NARROW A1


In English the logic I would attempt in the direction of

Select the doorid and the timedifference between each open event and the very next event close to the same door.

If anyone can help I would be very grateful!

Hi and welcome to the forum!

You are looking for something like this?

SQL> WITH door_events AS
  2  (
  3          SELECT TO_TIMESTAMP('2010-01-15 09:00:01.223','YYYY-MM-DD HH24:MI:SS.FF3') AS DT, 'A3' AS DOORID, 'OPEN' AS STATE FROM DUAL UNION ALL
  4          SELECT TO_TIMESTAMP('2010-01-15 09:02:04.789','YYYY-MM-DD HH24:MI:SS.FF3') AS DT, 'A1' AS DOORID, 'OPEN' AS STATE FROM DUAL UNION ALL
  5          SELECT TO_TIMESTAMP('2010-01-15 09:07:55.123','YYYY-MM-DD HH24:MI:SS.FF3') AS DT, 'A3' AS DOORID, 'CLOSE' AS STATE FROM DUAL UNION ALL
  6          SELECT TO_TIMESTAMP('2010-01-15 09:08:12.654','YYYY-MM-DD HH24:MI:SS.FF3') AS DT, 'A1' AS DOORID, 'CLOSE' AS STATE FROM DUAL
  7  )
  8  SELECT  DOORID
  9  ,       DT              AS DT_OPENED
 10  ,       DT_CLOSED
 11  ,       DT_CLOSED - DT  AS DURATION
 12  FROM
 13  (
 14          SELECT  DOORID
 15          ,       DT
 16          ,       LEAD(DT) OVER (PARTITION BY DOORID ORDER BY DT) AS DT_CLOSED
 17          FROM    DOOR_EVENTS
 18  )
 19  WHERE   DT_CLOSED IS NOT NULL
 20  ORDER BY 1
 21
SQL> /

DO DT_OPENED                      DT_CLOSED                      DURATION
-- ------------------------------ ------------------------------ ------------------------------
A1 2010-01-15 09:02:04.789        2010-01-15 09:08:12.654        +000000000 00:06:07.865000000
A3 2010-01-15 09:00:01.223        2010-01-15 09:07:55.123        +000000000 00:07:53.900000000

Published by: Centinul on January 15, 2010 14:46

The minor error.

Tags: Database

Similar Questions

  • How do the time difference between two dates?

    Hi all

    I use this query to get the time difference between two dates.

    Select to_timestamp ('2012-10-03 12:00 ',' YYYY-MM-DD hh)-to_timestamp ('2012-10-03 11:00 ',' YYYY-MM-DD hh) as double diff;

    but do not get the correct result.

    Thank you

    Left KEY... Left Padding of tanks.

  • ___How calculate the scale between two objects (without ArtDirector Toolkit)?

    Normally, I'd use ArtDirector Toolkit, but I can not install it or software at the present time (too long to explain why) remove.

    In any case, I have several items on the scale, and I'll try to find a formula/calculation to determine the scale.

    Example - 1 item is 3,7486 "and I want that it 5,3231" - how to calculate the percentage of the scale to expand it?

    I know you smart popular forum can help!

    Thank you!

    142.00234754308275%

    Hugh Betcha wrote: normally I'd use ArtDirector Toolkit, but I can not install it or software at the present time (too long to explain why) remove.

    Alternatively, you can Google "scale percentage between two numbers" - gives a lot of info free/calculators

  • time measurement between two finite pulse

    Hello Gerd

    Hello to you!

    I need support in the time between two pulses finished measure.

    Please find instant for the wave form, I want to measure the time between yellow and Red","yellow and blue ".

    Can you give me some indication to implement this thing?

    Concerning

    Nitin

    Oops Basics deliver,

    I'll implement the same and let you know if I find any difficulty.

    Thank you man.

  • What is the time interval between two Oracle certification exams?

    Hello world

    I'm looking forward to associate level certification in SQL and PL/SQL.

    According to a link from the official website of Oracle, I need to spend the next two STAGES to win the Oracle PL/SQL Developer Certified Associate Certification. The two steps are:

    URL of the location where the screenshots below (measures) is as follows:

    https://education.Oracle.com/pls/web_prod-PLQ-dad/db_pages.GetPage?page_id=653 & get_params = P_ID:162 #tabs - 1

    Capture1.JPG

    Capture2.JPG

    There are to options available in two stages , so I chose to give the two following tests:

    1 (from step 1) EXAM: 1Z0-061 (database Oracle 12 c: basic SQL)

    REVIEW 2 (step 2): 1Z0-144 (Oracle Database 11 G: program PL/SQL)

    Now my questions are these:

    Question 1: I intend to take the exam 1Z0-061 first (lets say in June), may therefore take the second exam (1Z0-144) at any time after appearing for the first review or is there no specific time frame in which I'm suppose to take the second review?


    Question 2: will what type of certification I get if I take only one exam (1Z0-061 or 1Z0-144)? I know Oracle's official website said that in order to get "Oracle PL/SQL Developer Certified Associate Certification", I need to pass the two exams; but what happens if I chose to give only a single review at the present time? In the case giving a single review, what type of document of certification will I right to (wave speculation: right to half certification Oracle PL/SQL Developer )?

    Thanking you.


    Best regards

    Ankit

    Answer 1: He is there no time limit/limit. Finally, I guess Oracle takes his retirement 1Z0-144, but which does not occur for many years.  They have not even removed from the earlier incarnation (1Z0-147).

    Answer 2: Passing a review in itself does not have a certification or credential.  The only "book", you will have is the sheet of test results.

  • Find the time delay between two signals

    In this code, I sent you a pulse in the acquisition of data using labview. The pulse triggers a thermistor to another analog input input data.

    I want to find the delay between the time of the positive edge of the switch, and the exact time of the DAQ begins to take in the signal of the temperature probe.

    I enclose the code below.

    Thank you.

    Hey,.

    Here is a link to some information about the sources of clock on the FPGA.

    http://digital.NI.com/public.nsf/allkb/94CE338CC80ACF008625791400516BAA?OpenDocument

    Hope this helps

  • How to measure the time elapsed between two steps?

    Hello

    In my script, I ask the subsequences. How can I measure the time it takes each subsequence?

    Something like:

    Statement: StationGlobals.TimeElapsed = 0

    -> SOMETHING HERE TO START A COUNTER<>

    call sous-suite

    Popup: Str (TimeElapsed)

    Thanks for help

    StationGlobals.Time = Seconds()

    ... / / stuff in time

    StationGlobals.Time = Seconds() - StationGlobals.Time

    You can also view the sample report of basic step in 2012 TestStand time (you can now download an eval).

  • calculation of time between two time as timestamp

    How can calculate two hour, START as the timestamp TIME in the data type, END TIME as of time also.

    select END_TIME-START_TIME from PROG_DLY_WORK
    

    Result

    + 01:50:00.000000 000000000

    How can get exactly the time spent as 01:50 ? and these calculated time must sum (elapsed_time) in another column.

    oracle as PaaS cloud environment

    Product VERSION STATUS
    NLSRTL11.2.0.3.0Production
    Oracle Database 11g Enterprise Edition11.2.0.3.064 bit Production
    PL/SQL11.2.0.3.0Production
    AMT for Linux:11.2.0.3.0Production

    Time difference between two columns in hours

    Start

    FOR REC IN (SELECT START_TIME, END_TIME, CALC_TIME CAL_ID)

    LOOP

    UPDATE CALc_TIME

    SET ELAPSED_TIME = round (())

    TO_DATE (to_char (REC. End_time, 'ddmmyyyyhh24miss'),

    "ddmmyyyyhh24miss")-

    TO_DATE (to_char (REC. Start_time, 'ddmmyyyyhh24miss'),

    "ddmmyyyyhh24miss")

    () * 24).

    WHERE CAL_ID = REC. CAL_ID;

    END LOOP;

    end;

    Lag in minutes

    Start

    FOR REC IN (SELECT START_TIME, END_TIME, CALC_TIME CAL_ID)

    LOOP

    UPDATE CALc_TIME

    SET ELAPSED_TIME = round (())

    TO_DATE (to_char (REC. End_time, 'ddmmyyyyhh24miss'),

    "ddmmyyyyhh24miss")-

    TO_DATE (to_char (REC. Start_time, 'ddmmyyyyhh24miss'),

    "ddmmyyyyhh24miss")

    () * 24 * 60)

    WHERE CAL_ID = REC. CAL_ID;

    END LOOP;

    end;

  • How to insert the file between the records in the database file

    Hi, I'm working on the project in which I need reading / writing of .mdb file. I am able to write data to the file, but I am not able to write data between two records. It's that I have to insert a few lines of data between the records in the file.

    Can someone tell me how to do? I really stuck with this problem.

    vaibhav

    Thank you
    In fact it was the problem with version 8.5.
    When I installed 8.5.1 it solved.

  • Count the number of records between two values of keys (BTREE)

    How can I count the number of keys between two values?

    I use python driver and BTREE access method.

    = >

    Ideally, what I want is a set of whole time series data (intervals may change) to a given number of points on average. The keys are timestamps and the values are the data that it takes on average. I need to count the number of records between two timestamps so that I can divide this figure by the number of points I need and data on average. What is the best way to do it?  Or should I keep the timestamp constant intervals and use the RECNO access method?

    Thank you
    (first post btw... and why is there not a lot of people at stackoverflow that answering the questions of Berkeley DB?)

    BDB is an integrated db and there no internal counters or statistics you might grap to use for this.    You will have to do it manually.

    You can create a cursor, grap the records you want, whenever you get the next card that you bump a counter.

    If you are using RECNO, you can use a slider to obtain the number of registration (DB_GET_RECNO), and if all that you data is in

    sequentail records with no missing documents, you can find the total number of take the last rec #-original rec # + 1 to get a count.

    If you pass the SQL API, you can issue a SQL query to give you a count.  Select count (*) where...

    As you enter the data anyway, so better perhaps to count records as you go along.

    Thank you

    Mike

  • Pulling two records at a time

    I have a TABLE with many records in it, I want to go out two records at once (as if they were a record), of documents that meet the criteria WHERE. Order is not important. Basically, I have a TABLE of things wilt need treatment or who have already been treated. I treat both at once. (In a related research, I'm not up yet, I can do 20 at a time.) How could I get this?

    My first thought was a CTE with ROW_NUMBER() RN, using CONNECTION BY RN = RN + 2 PREREQUISITE and have a subquery (or TABLE attached) out the second disc:

    WITH Moo (SELECT... ROW_NUMBER RN FROM Table WHERE...)
    SELECT..., (SELECT... STARTING FROM Moo Moo2 WHERE Moo2.Id = Moo.Id + 1)
    Moo log IN Id = Id + 2 PREREQUISITE;

    Or a join. Is there a better way to approach it?

    >
    Is there a better way to approach it?
    >
    It depends on how you plan to process records.
    >
    I can treat both at once
    >
    What does that mean? How to treat? A procedure using stored? Code using Java?

    A procedure or Java code could just retrieve the records he wants to treat either one or twenty. Why do you need to choose only two and then two others etc.?

    If you use the code then the simple thing is to define a cursor and picks up the records that you want to process.

    In a stored procedure, you can make a COLLECTION in BULK with a LIMIT of two if you want to just 2 records. You can use a P_LIMIT parameter that controls which is the limit.

    But the least effective way is to use a simple query with a clause limiting the line you are proposing because Oracle does not keep the slider open between executions. Using a cursor in a procedure, the cursor remains open. Even if you use Java code to retrieve a result set and then go get her.

  • Dynamic action - Get the difference between two dates + times

    I have problems a little dynamic to work action. I'm trying to get the time between two dates with the time difference.

    Here is what I got (this is apex 4.0):

    Two date pickers + two numbers fields (date/start/end times)

    I created a dynamic action on the page who fires on the point lose focus (above points).

    The real action for the DA is the body of the PL/SQL function:
    declare
      end_date DATE;
      start_date DATE;
    Begin
      start_date := to_char(:P1_START_DATE || ' ' || :P1_START_TIME, 'DD-MON-YYYY HH:MIAM');
      end_date := to_char(:P1_END_DATE || ' ' || :P1_END_TIME, 'DD-MON-YYYY HH:MIAM');
      :P1_HOURS := end_date-start_date;
    End;
    When I change the values on the page, I get the following error:

    AJAX call back Server error ORA-06502: PL/SQL: digital or value error: character number conversion error to set the value.

    I'm guessing that there is a problem with the date formatting, but I can't make it work. Thanks in advance!

    Hi djston,

    because you chose the dynamic action of 'Set value' with the "Body of the PL/SQL function" type you need to return the value. Try the following code

    declare
      end_date DATE;
      start_date DATE;
    Begin
      start_date := to_date(:P1_START_DATE || ' ' || :P1_START_TIME, 'DD-MM-YYYY HH:MIAM');
      end_date := to_date(:P1_END_DATE || ' ' || :P1_END_TIME, 'DD-MM-YYYY HH:MIAM');
      RETURN (end_date-start_date)*24;
    End;
    

    and P1_REQUESTED_HOURS like 'item affected. "

    Concerning
    Patrick
    -----------
    My Blog: http://www.inside-oracle-apex.com
    APEX 4.0 Plug-Ins: http://apex.oracle.com/plugins
    Twitter: http://www.twitter.com/patrickwolf

    Published by: Patrick Wolf on January 17, 2011 10:54

  • How to find the time between two channels of entry in the data acquisition card or pci 6036

    Hello

    I read a lot-related posts on the simultaneous measurement of two input voltage of similar channels in map data acquisition. I know that the best material is "simultaneous measurments of the Series DAQ cards" but I only pci data acquisition card 6036 and I try to understand what is the time between the reading of the two channels . This period is always constant? (must it rely on a voltage (amplitude, frequency, waveform..). I send the sine wave (s) to the two channels and read the values of V, if they read the same value, the difference should always be zero but I get-0,002 to 0.002 Volt difference (I must find a way to convert it in time). A screenshot of my VI is attached. I wonder how I can accurately measure the time delay between the channel.

    I am open to any suggestion, my final goal to read exactly two channels at the same time ((ou connaître le délai exact donc je peux correspondre les données correspondantes étant donné le temps de retard))

    Hi spinup,

    better you should post your question in the forum of LabVIEW, LabWindows/CVI is used

    Good luck.

  • Take the time between two values

    Hi people,

    I have a problem and I know idea how to solve... I need help.

    The problem is I want to take the time between two values max as you can see in the chart.

    For example, in the image that I have add

    4.5 - 1 840 = 2.66

    And enter this value in the 'time between mostra '.

    It's that I want...

    But what I think is very complicated, because I don't know how to take the time correctly and does remove...

    Thank you very much

    Any solution?

    Hi jocuma,

    I tried something and hope that helps u.

    Just create two arrays of temperature and voltage. First of all, I'll get the value of the voltage when it is more of a certain value and that same index to get the value of time and store in the shift register.

    When I get the second higher than the limit value, I'll get time and subtract the previous value.

  • Measure the time between two digital pulse

    Hello

    For a non-critical calendar application, I need to measure the time interval between consecutive TTL pulses, ranging from the order of 0.5 s for a few seconds, with a low accuracy of +/-10-50ms. The interval being measured varies between the rising edge of the first pulse and the front of the next and so on.

    I have several input lines I need to deal with. Because it's a critical machination low cost, I don't want to use digital counters for each line, so I work with an acquisition of data USB6008 and have connected the input rows TTL on the digital inputs of the device. Avoiding will be sufficient.

    I found a good example of VI on discussion forums that does almost the same thing, only it uses instead of the DAQ Assistant user input. The VI works including the time the program going on in a while loop. I replaced with the DAQ Assistant output (a channel) user input in the hope that it is still work.

    When I run the program in "run once" mode, it seems to work perfectly. However, in "continuous run" it measures only a very small interval, probably just the time between two samples.  I think it has something to do with the help of a while loop in combination with the DAQ Assistant. Anyone who has any suggestions how to solve this problem?

    Thank you!

    OK... first of all, you should never use the button "run continuously.  I wish that NEITHER would be to eliminate it, but told me that it is sometimes useful for debugging.  If you want your program to run over and over again, use a while loop with a stop"" button.

    If I'm reading your code correctly, you make your initial moment, and then collect data from data acquisition.  When one of the channels is "T", you stop your loop and the end time of capture.  (By the way, why you convert your table to a cluster?  Why not just index the appropriate channel in the table directly?)

    Since you want to capture the time between two consecutive pulses, you need to know when a transition has occurred... i. e when your digital line went from F (no pulse) to T (pulse start).  This will give you your forehead.  Right now, all you're doing is looking for a value T - so you have no way of knowing if you are looking for to the previous impulse again, or a new impetus.  You also burn 100% of your processor with the way you have your programme in place.

    You need a small loop delay so that your VI is not 100% of your hogs CPU time.  Given that you can live with an accuracy of 50msec, what I suggest that you use.

    See attached picture for you give an idea of how to implement.  He will probably need some refining operations, but it should point you in the right direction.

    I hope this helps.

Maybe you are looking for

  • Satellite A300-1BZ - new battery does not charge

    Hello I bought a new battery (PA3534U-1BRS) because my old battery (3 years) hold office as well as it should!The new battery will not charge, the Red battery light suggesting everything of fine, but then begins to Flash after 20 minutes.When I turn

  • Driver for Windows 7 64 bit for the Geforce 210 M graphics card

    G ' Day, I'm looking for a driver who doesn't seem to be available on the Tosbiba site. The driver needed is for the graphics card of Geforce 210 m inside the laptop, and I'm looking for the * 7-64 BIT Windows * version of the drivers. Download the d

  • Dash Internet Radio Streamer

    I noticed some problems with the new application streaming Internet Radio. 1. the volume control does not work once the stream is started2. the flow cannot be stopped without unplugging the unit from the wall. The stop button is disabled.3. the alarm

  • Problem downloading HP Support Assistant

    I have a HP Pavilion 500-164 with 8.1 Windows, AVG 2014 and my HP Support Assistant show this message: Error: HPSF.exe has stopped working

  • Superdisk 120 MB for Windows XP driver

    Recently I had to reformat my hard drive.  I use Windows XP and the pilot lost my Imation Superdisk 120 MB drive.  Any suggestions how to get it? Thank you Paul