English 中文(简体)
我怎么能够把重叠时间合并在一个窗口内?
原标题:How can I merge overlapping times within a window?

我有这样一个表格:

CREATE TABLE #TEMP (Name VARCHAR(255), START_TIME datetime, END_TIME datetime);

INSERT INTO #TEMP VALUES( John ,  2012-01-01 09:00:01 ,  2012-01-01 12:00:02 )
INSERT INTO #TEMP VALUES( John ,  2012-01-01 09:40:01 ,  2012-01-01 11:00:02 )
INSERT INTO #TEMP VALUES( John ,  2012-01-02 05:00:01 ,  2012-01-02 05:15:02 )
INSERT INTO #TEMP VALUES( David ,  2012-01-04 05:00:01 ,  2012-01-04 05:15:02 )
INSERT INTO #TEMP VALUES( David ,  2012-01-05 07:01:01 ,  2012-01-05 15:15:02 )

SELECT *
FROM #TEMP

DROP TABLE #TEMP

数据是:

     Name   START_TIME                 END_TIME
1    John   2012-01-01 09:00:01.000    2012-01-01 12:00:02.000
2    John   2012-01-01 09:40:01.000    2012-01-01 11:00:02.000
3    John   2012-01-02 05:00:01.000    2012-01-02 05:15:02.000
4    David  2012-01-04 05:00:01.000    2012-01-04 05:15:02.000
5    David  2012-01-05 07:01:01.000    2012-01-05 08:15:02.000

鉴于许多方面的话, 页: 1 在本表格中按<2> 由于时间范围重叠,将合并成单行:

John 2012-01-01 06:00:01.000 2012-01-01 18:00:02.000

<45>将合并,因为从07:01:01.000中减去6小时后,即落到“滚动编码”<4之窗。

在一个包含大约100万头的大桌上,这样做是否有好办法?

最佳回答

I think that the best way to do this is creating a windows table and join #temp table with this new window table:

(1) 第1步:编制窗口表,列出所有可能的窗口缺口(包含重叠窗口):

   SELECT 
      Name,
      dateadd(hour, -6, start_time) as start_w, 
      dateadd(hour, +6, start_time) as end_w
   into #possible_windows
   FROM #TEMP 

2) 建立改善业绩的进度表指数

   create index pw_idx on #possible_windows ( Name, start_w)

3) 在自行选择时消除重叠窗口。 这是编制指数的原因:

   select p2.* 
   into #myWindows
   from #possible_windows p1
   right outer join #possible_windows p2
     on p1.name = p2.name and 
        p2.start_w > p1.start_W and p2.start_w <= p1.end_w
   where p1.name is null

4) 在座标上 视窗或直接使用。

http://www.ohchr.org。

SELECT 
  Name,
  dateadd(hour, -6, start_time) as start_w, 
  dateadd(hour, +6, start_time) as end_w,
  ROW_NUMBER() over(partition by Name order by Name, 
                    dateadd(hour, -6, start_time) ) as rn
into #possible_windows
FROM #TEMP 

create index pw_idx on #possible_windows ( Name, start_w)

select p2.* 
from #possible_windows p1
right outer join #possible_windows p2
  on p1.name = p2.name and 
     p2.start_w > p1.start_W and p2.start_w <= p1.end_w
where p1.name is null

http://www.ohchr.org。

Name  start_w       end_w         rn 
----- ------------- ------------- -- 
David 2012-01-03 23:00:012012-01-04 11:00:011  
David 2012-01-05 01:01:012012-01-05 13:01:012  
John  2012-01-01 03:00:012012-01-01 15:00:011  
John  2012-01-01 23:00:012012-01-02 11:00:013 

页: 1

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签