Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 获取SQL中的“事件链”_Sql Server 2008 - Fatal编程技术网

Sql server 2008 获取SQL中的“事件链”

Sql server 2008 获取SQL中的“事件链”,sql-server-2008,Sql Server 2008,我有一张这样的桌子: CustomerID | ContactTime | AttemptResult -----------+-----------------+----------------- 1 | 1/1/2016 5:00 | Record Started 1 | 1/1/2016 6:00 | Appointment 2 | 1/2/2016 5:00 | Record Started 1

我有一张这样的桌子:

CustomerID | ContactTime     | AttemptResult 
-----------+-----------------+-----------------
1          | 1/1/2016 5:00   | Record Started
1          | 1/1/2016 6:00   | Appointment 
2          | 1/2/2016 5:00   | Record Started
1          | 1/3/2016 6:00   | Sold
2          | 1/2/2016 5:00   | Sold
3          | 1/4/2016 5:00   | Record Started
3          | 1/4/2016 6:00   | Sold

我怎样才能以一种方式来查询它,从而按照尝试结果的顺序获取所有组合?比如:

CustID | Sequence
-------+--------------------------------------
1      | Record Started -> Appointment -> Sold 
2      | Record Started ->  Sold 
3      | Record Started ->  Sold 

我甚至不知道从哪里开始

如果这是您的完整数据集,我可以帮助您。否则我需要看更多。使用称为窗口函数的东西。下面是对每个CustomerID有多少个条目进行索引或跟踪

    Select *, row_number() over (partition CustomerID group by ContactTime) as Combo
    into #temp
    from table
然后数一数有多少2人的组合发生记录开始->售出,3人的组合发生记录开始->约会->售出

   Select CustomerID, max(Combo) as MaxCombo
   into #temp1
   from #temp
   group by CustomerId

   Select MaxCombo, count(*)
   from #temp1
   group by MaxCombo
您也可以使用公共表表达式代替这些临时表,但我不想添加太多混淆

这给了我“CustomerID”附近不正确的语法。我将其更改为按CustomerID顺序按ContactTime分区,并修复了错误,但结果不是我想要的。
   Select CustomerID, max(Combo) as MaxCombo
   into #temp1
   from #temp
   group by CustomerId

   Select MaxCombo, count(*)
   from #temp1
   group by MaxCombo