2011-04-21 63 views
1

假設我的表包含以下信息:合併兩個或兩個以上的行SQL Server 2000中

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 

我要合併,就像第1行和第2行方式如下(有這麼多行像以上)條件的表是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 

請幫我選擇我的桌子上面的結果。

在此先感謝。

+0

請插入管道 「|」在你的數據中,使它更容易閱讀和放置每行數據在自己的線上。 – 2011-04-21 08:12:25

+0

是[此](http://stackoverflow.com/questions/5018403/need-to-merge-blocks-of-records-in-query-into-one-depending-on-other-query-col)你的問題? – 2011-04-21 13:40:25

回答

0
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 

我認爲這應該工作.. :)

+0

感謝您的答覆,但沒有行列,我想從我的表中選擇這些值來查看。我想合併兩行根據我的條件和日期2應該改變,因爲我上面提到以及價值。 – 2011-04-21 08:35:05

+0

如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼樣本」按鈕(「{}」),以精確地設置格式並對其進行語法突出顯示! – 2011-04-21 09:17:11