简体   繁体   English

避免在SQL Server过程中进行访存

[英]Avoiding fetch in SQL Server procedure

I have a table with areas that looks like this: 我有一张桌子,上面看起来像这样:

ID     NAME          Coord
-------------------------
1     area1           bla
1     area2           bla
1     area3           bla

and a table with the position of an object depending on time: 以及一个表,该表的对象取决于时间:

Time_Stamp                    Latitude    Longitude
---------------------------------------------------
2018-07-17 21:13:30.000       bla         bla
2018-07-17 21:13:45.000       bla         bla
2018-07-17 21:14:00.000       bla         bla
2018-07-17 21:14:15.000       bla         bla
2018-07-17 21:14:30.000       bla         bla
2018-07-17 21:14:45.000       bla         bla
2018-07-17 21:15:00.000       bla         bla
2018-07-17 21:15:15.000       bla         bla
2018-07-17 21:15:30.000       bla         bla
2018-07-17 21:15:45.000       bla         bla
2018-07-17 21:16:00.000       bla         bla

From this I'm selecting a result set with the position of the object foreach area (type=> 0 outside area, 1 inside area) 从这里,我选择一个对象的位置为每个区域(类型=> 0个外部区域,1个内部区域)的结果集

TS                            area         type
-----------------------------------------------
2018-07-17 21:13:30.000       area1         1
2018-07-17 21:13:30.000       area2         0
2018-07-17 21:13:30.000       area3         0
2018-07-17 21:13:45.000       area1         0
2018-07-17 21:13:45.000       area2         0
2018-07-17 21:13:45.000       area3         0
2018-07-17 21:14:00.000       area1         0
2018-07-17 21:14:00.000       area2         1
2018-07-17 21:14:00.000       area3         0

From this dataset, I need to get the time foreach area when the object enters and gets out an area.something like: 从这个数据集中,我需要获取对象进入和离开某个区域时每个区域的时间。

TS                            area         type
------------------------------------------
2018-07-17 21:13:30.000       area1         1
2018-07-17 21:13:45.000       area1         0
2018-07-17 21:14:00.000       area2         1

and so long... I have a select in a fetch (foreach area). 这么长...我在fetch(foreach区域)中有一个选择。

select * 
from
    (select 
         min(ts) TS,
         area,
         type 
     from
         (select 
              *,
              grp = ROW_NUMBER() OVER(ORDER BY ts) -
                      ROW_NUMBER() OVER(PARTITION BY type ORDER BY ts)
          from 
              @t 
          where 
              area = @area    -- this is area from fetch and 
                              -- @t is the table I've built first
         ) a 
     group by 
         grp, type, area) b 
order by 
    endtime

I need a solution to avoid the fetch if this is posible. 我需要一个解决方案来避免这种情况(如果可能)。

I think you just need partition by : 我认为您只需要按以下内容进行partition by

      select min(ts) as TS, area, type 
      from (select t.*,
                   (row_number() over (partition by area order by  ts) -
                    row_number() over (partition by area, type order by ts)
                   ) as grp
            from @t t
           ) t 
      group by grp, type, area
      order by min(ts) desc;

The outer query is not necessary -- and endtime is not defined in your subquery anyway. 外部查询不是必需的-而且endtime也未在子查询中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM