2017-02-22 45 views
2

我需要在一個表(Azure的SQL)運行對每個行集一些查詢:申請查詢表的每一個部分單獨

ID CustomerID  MsgTimestamp   Msg 
------------------------------------------------- 
1  123   2017-01-01 10:00:00  Hello 
2  123   2017-01-01 10:01:00  Hello again 
3  123   2017-01-01 10:02:00  Can you help me with my order 
4  123   2017-01-01 11:00:00  Are you still there 
5  456   2017-01-01 10:07:00  Hey I'm a new customer 

我想要做的是提取「聊天會話」爲每一位客戶從消息記錄中,也就是說,如果某人的兩個連續消息之間的差距少於30分鐘,則它們屬於同一個會話。我需要將每個會話的開始和結束時間記錄在一個新表格中。在上面的例子中,第一個客戶123的會話的開始和結束時間是10:00和10:02。

我知道我總是可以使用光標和臨時表來實現這個目標,但我正在考慮使用任何預建機制來達到更好的性能。請給我一些意見。

回答

1

您可以使用窗口函數而不是光標。像這樣的東西應該工作:

declare @t table (ID int, CustomerID int, MsgTimestamp datetime2(0), Msg nvarchar(100)) 
insert @t values 
(1,  123,   '2017-01-01 10:00:00',  'Hello'), 
(2,  123,   '2017-01-01 10:01:00',  'Hello again'), 
(3,  123,   '2017-01-01 10:02:00',  'Can you help me with my order'), 
(4,  123,   '2017-01-01 11:00:00',  'Are you still there'), 
(5,  456,   '2017-01-01 10:07:00',  'Hey I''m a new customer') 

;with x as (
    select *, case when datediff(minute, lag(msgtimestamp, 1, '19000101') over(partition by customerid order by msgtimestamp), msgtimestamp) > 30 then 1 else 0 end as g 
    from @t 
), 
y as (
    select *, sum(g) over(order by msgtimestamp) as gg 
    from x 
) 
select customerid, min(msgtimestamp), max(msgtimestamp) 
from y 
group by customerid, gg 
+0

太酷了!我曾經關於使用窗口函數,但不知道如何實現。和你使用「會話標記」(列g和gg)真棒! – Ryan