Help: SQL to select the closest points within groups of documents grouped by hour

Hello
I need help to find a SQL for an effective solution to the following problem:
-J' have 20 buses circling on a bus line and their locations are reported every 10 seconds and recorded in a table space 'bus_location' with 'bus_loc' as the SDO_GEOMETRY type.
-Location of bus each record there is a column msg_datetime with the time when the position has been saved.
-Bus circle the the bus route 8 to 12 times, from A to B, then b back to A, then new loop from A to B and so on.
-J' have 3 points of synchronization in the spatial table "timing_point" with "tp_loc" as the SDO_GEOMETRY type.
-A bus will pass each timing point 8 - 12 times in one direction and 8 - 12 times the other way, while making the loops on the road to the bus.

My task is: for each timing point, every bus, (A - B or B - bis), find nearest bus for each direction of travel place to this timing point.

Example:
Bus... * TimingPoint *... * time *... * Distance *.
...........................................*between*
...........................................*bus and TP*
12......A................ 14:01, 250 m
12......A................ 14:01:10... 150 m
12......A................ 14:01:20... 12 m < == it is closest to the TP for this pass
12......A................ 14:01:30, 48 m
12......A................ 14:01:40... 100 m
12......A................ 14:01:50... 248 m
12......A................ 14:29:40, 122 m
12......A................ 14:29:50, 72 m
12......A................ 14:30... 9 m < == it is closest to the TP for this pass
12......A................ 14:30:10... 10 m
12......A................ 14:30:20... 12 m

I tried to use SDO_NN, and I just can't close bus locations, but how do I find the location of bus closest to each passage? Or how to identify a location of bus groups who are together in 2-3 minutes and find the record closest to each of these groups of locations?
I thank very you much for any help.

Milan

Published by: mburazor on February 9, 2012 14:41

Milan,

Yes, the problem is one of the non-space analytical. Here's another GB, which should be independent of the year/month/date/hour/minute
as it does all its math based on an absolute number of minutes since 1970. I turn to 5 minutes but you could round/trunc at all
interval (5 minutes, 10 minutes, 15 minutes, etc.)

Note that I have created additional folders in the same table I built. (Tip: good idea to provide an example of dataset on the forum when)
ask tough questions of SQL like this).

drop table buslocns;

create table buslocns(
busno number,
timingpt varchar2(1),
pttime   timestamp,
distance number)
;
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:00','YYYY/MM/DD HH24:MI:SS'),250);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:10','YYYY/MM/DD HH24:MI:SS'),150);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:20','YYYY/MM/DD HH24:MI:SS'),12);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:30','YYYY/MM/DD HH24:MI:SS'),48);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:40','YYYY/MM/DD HH24:MI:SS'),100);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:01:50','YYYY/MM/DD HH24:MI:SS'),248);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:29:40','YYYY/MM/DD HH24:MI:SS'),122);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:29:50','YYYY/MM/DD HH24:MI:SS'),72);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:00','YYYY/MM/DD HH24:MI:SS'),9);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:10','YYYY/MM/DD HH24:MI:SS'),10);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:30:20','YYYY/MM/DD HH24:MI:SS'),12);

insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:59:40','YYYY/MM/DD HH24:MI:SS'),53);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:59:50','YYYY/MM/DD HH24:MI:SS'),28);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:00:00','YYYY/MM/DD HH24:MI:SS'),12);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:00:10','YYYY/MM/DD HH24:MI:SS'),73);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:44:40','YYYY/MM/DD HH24:MI:SS'),53);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 14:44:50','YYYY/MM/DD HH24:MI:SS'),28);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:45:00','YYYY/MM/DD HH24:MI:SS'),12);
insert into buslocns values (12,'A',to_timestamp('2012/02/10 15:45:10','YYYY/MM/DD HH24:MI:SS'),73);

commit;
with tensOfMinutes as (
select row_number() over (order by busno,timingpt, pttime) as rid,
       busno,
       timingpt,
       pttime,
       round(((cast(pttime as date) - cast('01-JAN-1970' as date)) * 1440) / 5) * 5 as mins,
       distance
  from BUSLOCNS
)
select busno,timingpt,to_char(to_date('01-JAN-1970','DD-MON-YYYY') + ( mins / 1440 ),'DD-MON-YYYY HH24:MI:SS') as pttime,minDist
  from (select busno,timingpt,mins,min(distance) as minDist
          from tensOfMinutes a
         group by a.busno, a.timingpt, a.mins
         order by 1, 2, 3 ) ;
-- Result
--
BUSNO                  TIMINGPT PTTIME               MINDIST
---------------------- -------- -------------------- ----------------------
12                     A        10-FEB-2012 14:00:00 12
12                     A        10-FEB-2012 14:30:00 9
12                     A        10-FEB-2012 14:45:00 28
12                     A        10-FEB-2012 15:00:00 12
12                     A        10-FEB-2012 15:45:00 12

concerning
Simon

Tags: Database

Similar Questions

  • Select the anchor point of the Group and anchor it to the PointInsertion

    Hi all

    How to select the anchor point of the Group and dock it at the point of insertion of the document in indesign cs5.5 using javascript or vb.net

    You need get a reference to your group - call the myGroup - and the other to the PointInsertion where you want it anchored - call - the myIP. Given those, you can achieve your goal (in CS5 or later) by:

    myGroup.anchoredObjectSettings.insertAnchoredObject (myIP);

    You can specify a second parameter, if you want that specifies the position of anchor:

    myGroup.anchoredObjectSettings.insertAnchoredObject (myIP, AnchorPosition.anchored);

    That should help you get started.

    Dave

  • The closest point of all in a table

    Hello

    I have two table 2D elements. Array1 and array2.

    I take a value from the array Array2 and calculate its distance from all elements of array Array1. I need to get the item that is the short of it. How is this posible?

    I did a VI. After that calculation of the distance how is it possible to check the short and how to keep that close to the value in a table?

    Please take a look

    Thank you

    Although I don't understand quite what kind of output you want, here's a possible solution that can probably point you in the right direction. Good luck!

    I assume that you want the value of the item to the nearest table and I guess by the distance you are talking about the 2D distance between two points in the complex plane. Other interpretations would be to get the closest value array indices, for example.

  • How to return a query all the elements for an LOV if I select the "white point" (in this LOV)

    Hello. In my application, I have a classic report where, in a region, I built a toolbar with three Popup LOVs which interact with the report. Each LOV displays different items and returns the value according to the selected item.

    For example:

    Popup LOV 1 named P33_DEPARTMENTS has the following components: production, development, projecting, ecc.

    Popup LOV 2 named P33_LOCATIONS was the following: Europe, America, Asia, Africa, ecc.


    Popup LOV 3 named P33_DEVICES has the following: printer, keyboard, mouse, monitor, ecc.


    If I query the table to return the lines according to the selected in these LOVs I have something like this:


    SELECT * FROM < table > WHERE the dep =: P33_DEPARTMENTS AND loc =: P33_LOCATIONS AND dev =: P33_DEVICES


    Now I want that if the user selects all values (LOVs one white point) (or all), then the query must return all values for that LOV.

    For example, if for the named LOV P33_DEPARTMENTS 'white' value is selected, then the query must return to the table lines this cointain values "production ' OR 'developing' OR 'project', ecc for the columns named"dep".

    This means that must be the operator AND between LOVs but the operator or inside the LOV even, if the item is selected.


    How is it possible? Thank you

    SELECT *
    FROM T
    WHERE dep = nvl( :P33_DEPARTMENTS, dep)
      and loc = nvl( :P33_LOCATIONS, loc)
      and dev = nvl( :P33_DEVICES, loc)
    

    You just need to ensure that, when no value is selected, the LOV will return a NULL value.

    MK

  • Error message 'Please select the access point or wireless router to set up' when you set up a network

    Original title: set up a network.

    I want to create a wireless network for the games, and when I click on the network. A screen comes written on this "Choose the access point or wireless router to set up" and that "it might take 90 s for devices reconfigured to appear on your computer.

    But no device never appears below it, even after 90 minutes.
    Please help me with this.
    It was so easy in windows 7.

    Hi Manas,

    What is the brand and model of the Wireless Router/Access Point?

    Given that the issue has not persisted in Windows 7, there is a chance that the wireless router/Access Point is not compatible with Windows 8.

    Please check the links below on the implementation of a network.

    http://Windows.Microsoft.com/en-in/Windows-8/Setup-home-network

    http://Windows.Microsoft.com/en-in/Windows-8/what-you-need-Setup-home-network

    http://Windows.Microsoft.com/en-in/Windows/set-up-wireless-router#set-up-wireless-router=Windows-8

    http://Windows.Microsoft.com/en-in/Windows-8/Add-device-or-PC-to-network

    Answer please if you have any questions.

  • Select the encoding then make sure readable document

    I used to be able to put all the names of my words and then get back them and put them in my contacts list, but now I have a problem them opening he said

    Select encoding, and then make sure the readable document unable to understand this

    Hi Vagn Frederiksen,

    1. are you facing problems with Office Word?

    If so, you can read the following article and see if it helps:

    Troubleshoot encoded text files

    Hope this information is useful.

  • SQL query - select a row of each group to date

    Hello

    I have data like this.

    Visit_Date Visit_type Consultant

    2009-05-09 G name1
    2009-05-09 G name2
    2009-05-09 G Name3

    06/09/2009 I have name4

    09/07/2009 G name5
    09/07/2009 G name6

    How to select data as follows

    2009-05-09 G name1
    09/06/2009 G name4
    09/07/2009 G name5

    That is to say a line of each visit_date

    Thank you
    Nathan MK

    Published by: k_murali on October 7, 2009 22:44

    Hello

    k_murali wrote:
    I modified your request to meet my requirement of 100%. But it is not work.

    Select trunc (visit_dt) visit_date, min (visit_type) visit_type, min (consultant)
    SD
    where visit_type = 'G' and client_id = 21503
    Trunc Group (visit_dt)
    order of trunc (visit_dt)

    Union of all the

    Select min (visit_dt) visit_date, min (visit_type) visit_type, min (consultant)
    SD
    where visit_type = 'I '.
    Visit_Dt group
    order of visit_Dt

    But if I run separately (b4 & after all), his works very well. But I need the result.

    What do you mean by "doesn't quite work" and "working well"? The UNION is too slow?
    If so, combine them in a single query, without UNION.
    It seems that the main difference between the two stems of the UNION, is that the upper pin (for visit_type = 'G') are grouped by days (TRUNC (visit_date)), but the pane at the bottom (for visit_type = 'I') are grouped by seconds (visit_date, without TRUNC). You can GROUP BY an expression BOX which maps all points in any calendar the same day point only when visit_type = 'G', like this:

    SELECT        CASE
              WHEN  visit_type = 'G'     THEN  TRUNC (visit_date)
              WHEN  visit_type = 'I'     THEN         visit_date
            END                       AS visit_date
    ,        visit_type
    ,        MIN (consultant)      AS consultant
    FROM        dd
    WHERE        (     visit_type     = 'G'
            AND     client_id     = 21503
            )
    OR        visit_type     = 'I'
    GROUP BY   CASE
              WHEN  visit_type = 'G'     THEN  TRUNC (visit_date)
              WHEN  visit_type = 'I'     THEN         visit_date
            END
    ,             visit_type
    ORDER BY   visit_date
    ,             visit_type
    ;
    

    This should be faster, because it makes only one pass through the table dd.

    If you do not want to repeat the CASE expression (in the SELECT clause and the GROUP BY clause), then you can calculate it in a subquery.
    There is no need to repeat the CASE expression in the ORDER BY clause; We can use the alias column (defined in the SELECT clause) in the ORDER BY clause.

  • How to find the closest higher value?

    Hi all

    I want to select the closest higher value in my data (test data are below):
    with test_data as
    ( select 0.21 as val1, 0.19 as val2 from dual
      union all
      select 0.21 as val1, 0.29 as val2 from dual
      union all
      select 0.21 as val1, 0.49 as val2 from dual
      union all
      select 0.67 as val1, 0.49 as val2 from dual
      union all
      select 0.67 as val1, 0.69 as val2 from dual
    )
    select * from 
    (
    select 
    val1,
    val2,
    rank() over (order by abs(val2 - val1)) nearest
    from test_data
    )
    where nearest = 1;
    output:
    VAL1    VAL2    NEAREST
    0.21     0.19     1
    0.67     0.69     1
    Output of this query is the NEAREST value to the value in the column of VAL1. But I would get the SUPERIOR value CLOSEST. As below the value at the higher value 0.21 closest isn't 0.29 0.19.

    desired output:
    VAL1    VAL2
    0.21     0.29
    0.67     0.69
    Is it possible to get the features you want using analytical functions?

    Thx for the help!

    Kind regards

    Jiri

    Published by: Jiri n. 10 October 2012 01:14
    with test_data as
    ( select 0.21 as val1, 0.19 as val2 from dual
      union all
      select 0.21 as val1, 0.29 as val2 from dual
      union all
      select 0.21 as val1, 0.49 as val2 from dual
      union all
      select 0.67 as val1, 0.49 as val2 from dual
      union all
      select 0.67 as val1, 0.69 as val2 from dual
    )
    
    select
     val1
    ,val2
    ,first_value(val2) over (partition by val1 order by
       case when val2 >= val1 then 1 else 2 end
       , val2
     ) nearest
    from test_data
    
    VAL1 VAL2 NEAREST
    .21 .29 .29
    .21 .49 .29
    .21 .19 .29
    .67 .69 .69
    .67 .49 .69 
    
  • Select the last 50 closed records

    The research of a statement select simple sql which selects the latest 50 records closed in the database.

    I found some help out there who have an example like:

    SELECT RecordID, closed_date
    (SELECT RecordID, closed_date, RANK() over (ORDER BY closed_Date DESC) date_rank
    FROM table1)
    WHERE the date_rank < = 50;

    Only problem is, this is to select something like 4000 recordings, and many of them are not yet closed.

    I think that you need to retrieve the records that are only closed in the first place.

    Try this,

    SELECT RecordID, closed_date
      FROM (SELECT RecordID,
                   closed_date,
                   ROW_NUMBER () OVER (ORDER BY closed_Date DESC) rn
              FROM table1
             WHERE closed_date < SYSDATE)  --Notice this.
     WHERE rn <= 50;
    

    G.

  • Wifi utility to search and select a connection point

    The connection software Toshiba configfree love lock onto a signal in a box, although there may be a lot and does not allow me to select the connection point that I want to use. Why?

    Hello

    As much I know ConfigFree tool can be used for monitoring of LAN or WLAN connections and not for the WLAN connection setting. I put t know what cell phone you have and what ConfigFree version you have, but you can click with the left button of the mouse on the ConfigFree icon and choose Search for wireless devices option. On this path, you will see all available wireless networks. If you want to connect to one of them, you must configure settings of Windows or client if you a.

    Good bye

  • Align with the graphic point nearest

    I have a chart xy with several points. The graph is controlled by a mouse press event, when the user clicks on the chart, he draws a red circle at the point of click. My question is how I would get this to align with the closest point?


  • Accidentally, I purchase an upgrade when I was trying to buy Acrobat Pro. I need help in Exchange for the upgrade to what I need.

    I can't find a certain number of support the customer to solve this problem.

    You can contact the Support from Adobe using link below:

    Contact the customer service

    [Click on always need help button and select the Chat option]

  • Please, help me to create the foreign key.

    Oracle 10g using sqldbx

    I tried everything I could but all in vain.

    (Folder) CREATE TABLE
    FolderId INT NOT NULL PRIMARY KEY,
    FolderName varchar (255) NOT NULL,
    CreatedBy varchar (255) NOT NULL,
    CreateDate TIMESTAMP NOT NULL
    )


    (Document) CREATE TABLE
    DocumentID INT NOT NULL PRIMARY KEY,
    FolderId INT NOT NULL,
    DocumentTitle varchar (255) NOT NULL,
    DocumentAuthor varchar (255) NOT NULL,
    CreateDate TIMESTAMP NOT NULL
    )



    Document of ALTER TABLE ADD CONSTRAINT fkdocument FOREIGN KEY (document.folderid) REFERENCES (the folder. FOLDERID)

    the last statement says invalid folder name. Help, please

    Hello

    Welcome to the forum!

    See the Manual of the SQL language for the correct syntax:

    ALTER TABLE     document
    ADD CONSTRAINT      fkdocument
    FOREIGN KEY      (folderid)
    REFERENCES      folder (FOLDERID)
    ;
    

    Thanks for posting your code! It is very useful.
    Have you seen the FAQ forum yet? {message: id = 9360002}

  • Select the query within the PL/SQL block.

    Hello Experts,

    I'm just a beginner with PL/SQL.
    If I write a select query of client like fire against a database and SQL dev, it gives me result.
    For example: select * from employee;

    Now suppose that when I use the same query within a PL/SQL block:
    Declare
    Start
    Select * from employee;
    end;
    /
    It gives error during execution, by mentioning that an INTO should etc...
    I have my doubts here:
    1. is it impossible to use a simple select statement within a PL/SQL block (If yes why?)

    I know it's a very basic question, I tried to search it on the forum but could not find the thread, please redirect me to the link if it is already the answer.

    Please read this first. may ask why?

    http://docs.Oracle.com/CD/B28359_01/AppDev.111/b28370/TOC.htm

  • Is it possible to delete a plot with the help of the mouse select the chart control?

    For example, when I use the plot to draw thousands of lines on the chart control, I want to delete some of them, but there are thousands of generated plotHandle.

    How can I get the plotHandle of the specific line on the chart control with the help of the mouse to select the option?

    If I save the information from the lines, such as the plotHandle and the coordinates on a table or list, etc, I compare information lines for the plotHandle of the specific line.
    Is it all a user-friendly way to remove a track selection on the chart control?

    I can't give you a reasonable way to solve this problem, since as you have already noted plot do not snappable plots.

    A possible warkaround would be to replace the plot of individual instructions with a PlotXY matching with 2 bays 2 points each, which correspond to the x 1, x 2, y1, y2 parameters to plot. PlotXY plots are snappable, so you can enable this procedure select the plot and remove it.

Maybe you are looking for