完成詐欺分析
到目前為止,我們已把日間水療的資料拆成「進場」與「離場」的事件串,並依時間先後排序。這個事件串包含兩個關鍵欄位:StartOrdinal 與 StartOrEndOrdinal。StartOrdinal 代表所有進場事件的時間順序;StartOrEndOrdinal 則是所有進場與離場事件的合併順序。掌握這兩個資訊後,我們可以找出同時在場人數的最大值。
前一個練習的結果現在已存放在名為 #StartStopOrder 的暫存資料表中。
本練習屬於課程
SQL Server 的時間序列分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
s.CustomerID,
MAX(2 * s.StartOrdinal - s.StartOrEndOrdinal) AS MaxConcurrentCustomerVisits
FROM #StartStopOrder s
WHERE s.EntryCount = 1
GROUP BY s.CustomerID
-- The difference between 2 * start ordinal and the start/end
-- ordinal represents the number of concurrent visits
HAVING MAX(2 * s.___ - s.___) > 2
-- Sort by the largest number of max concurrent customer visits
ORDER BY ___ ___;