开发者

Merge two or more rows in SQL Server 2000

开发者 https://www.devze.com 2023-02-27 06:56 出处:网络
Suppose my table contains the following information: row | ID|date1|date2| place| value --------------------------------------------------------------------------

Suppose my table contains the following information:

row | ID  |       date1          |        date2            | place   | value
--------------------------------------------------------------------------
1   |  1  |  2011-01-23 05:33:21 |    2011-01-23 06:43:10  |    A    |     5
2   |  1  |  2011-01-23 06:44:21 |    2011-01-23 07:13:28  |    A    |     4
3   |  1  |  2011-01-23 07:14:21 |    2011-01-23 07:27:28  |    A    |     4
4   |  1  |  2011-01-23 08:30:11 |    2011-01-23 09:56:45  |    A    |     6
5   |  1  |  2011-01-23 09:58:21 |    2011-01-23 10:43:26  |    B    |     8
6   |  2  |  2011-01-23 05:33:21 |    2011-01-23 06:43:21  |    A    |     3

I want to merge just like row 1 and row 2 way that as below.(there are so many rows in the table like above) The condition is DATEDIFF(minute, row1.date2, row2.date1) < 10 and row1.place = row2.place

row | ID  |       date1          |        date2          |  place   | value
==================================================开发者_开发知识库=====================
1   |  1  |  2011-01-23 05:33:21 |   2011-01-23 07:27:28 |     A    |    13
2   |  1  |  2011-01-23 08:30:11 |   2011-01-23 09:56:45 |     A    |    6
3   |  1  |  2011-01-23 09:58:21 |   2011-01-23 10:43:26 |     B    |    8
4   |  2  |  2011-01-23 05:33:21 |   2011-01-23 06:43:21 |     A    |    3

please help me to select the above result from my table.

Thanks in advance.


insert into table
   select 
      row, id, date1, date2, place, sum(value)
   from table
   where (your date diff condition)
   group by row, id, date1, date2, place

I think this should work.. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消