选择&在UserID上进行分组,其中交易类型适合特定订单且在30分钟内

时间:2014-09-02 22:00:14

标签: sql sql-server sql-server-2008-r2

在我们的SQL Server 2008 R2数据库中有一个表,我们存储TransactionID,TimeStamp,TransactionType,UserID和其他信息。

CREATE TABLE [dbo].[TransactionRecords]( [TransactionID] [float] NULL, [TimeStamp] [datetime] NULL, [TransactionType] [nvarchar](255) NULL, [UserID] [nvarchar](255) NULL )

INSERT INTO TransactionRecords ( TransactionID, TimeStamp, TransactionType, UserID) VALUES
(3211, '2014-09-01 14:08:54.000', 'OnlineTrial', '00E235420CE9FD33'), 
(3212, '2014-09-01 14:28:19.000', 'OnlineOrder', '00E235420CE9FD33'), 
(3213, '2014-09-01 14:29:00.000', 'NewsletterSubscribe', '31E4E29230BFA615'), 
(3214, '2014-09-01 14:34:50.000', 'OnlineTrial', '2D37174DD60F450B'), 
(3215, '2014-09-01 14:44:44.000', 'OnlineOrder', '2D580670019B61F6'), 
(3216, '2014-09-01 14:54:22.000', 'NewsletterSubscribe', '376C5DF69356616E'), 
(3217, '2014-09-01 15:03:06.000', 'OnlineTrial', '31E4E29230BFA615'), 
(3218, '2014-09-01 15:27:01.000', 'OnlineOrder', '31E4E29230BFA615'), 
(3219, '2014-09-01 15:33:00.000', 'NewsletterSubscribe', '130F764A98409EF4'), 
(3220, '2014-09-01 15:42:00.000', 'OnlineTrial', '0417546E0D518432'), 
(3221, '2014-09-01 16:18:50.000', 'OnlineOrder', '05E4EB4E47C756B6'), 
(3222, '2014-09-01 16:55:40.000', 'OnlineOrder', '1B9FEBB7FD029625'), 
(3223, '2014-09-01 17:32:30.000', 'OnlineOrder', '061310EE4F20E3EA'), 
(3224, '2014-09-01 18:01:20.000', 'OnlineTrial', '061310EE4F20E3EA'), 
(3225, '2014-09-01 18:46:10.000', 'NewsletterSubscribe', '144937298097C9D4')`

我想做的是查询,以便在TimeStamp的30分钟窗口内返回记录为OnlineTrial的用户的事务ID,然后返回OnlineOrder。

现在我的查询是:

SELECT DISTINCT a.[TransactionID]
  ,a.[TimeStamp]
  ,a.[TransactionType]
  ,a.[UserID]
FROM [Adhoc].[dbo].[TransactionRecords] a
LEFT OUTER JOIN [Adhoc].[dbo].[TransactionRecords] b
ON a.UserID = b.UserID
WHERE DATEDIFF(MI, a.timestamp, b.timestamp) <= 30
AND a.TransactionID <> b.TransactionID
AND (a.TransactionType = 'OnlineTrial'
OR a.TransactionType = 'OnlineOrder')
ORDER BY a.UserID, a.TimeStamp

理想的查询只返回事务3211,3212,3217和&amp; 3218 但我的查询仍返回用户使用OnlineOrder然后使用OnlineTrial(ID 3223&amp; 3224)

的结果

2 个答案:

答案 0 :(得分:0)

您可以使用exists来匹配必要的行:

select
    *
from
    dbo.TransactionRecords t1
where
    t1.transactiontype = 'OnlineTrial' and
    exists (
        select
            'x'
        from
            dbo.TransactionRecords t2
        where
            t1.UserID = t2.UserID and
            t2.transactiontype = 'OnlineOrder' and
            datediff(mi, t1.Timestamp, t2.Timestamp) between 0 and 30
    )
union all
select
    *
from
    dbo.TransactionRecords t1
where
    t1.transactiontype = 'OnlineOrder' and
    exists (
        select
            'x'
        from
            dbo.TransactionRecords t2
        where
            t1.UserID = t2.UserID and
            t2.transactiontype = 'OnlineTrial' and
            datediff(mi, t2.Timestamp, t1.Timestamp) between 0 and 30
    )

Example SQLFiddle

答案 1 :(得分:0)

应该是基于用户ID和显式事务类型引用的简单JOIN。外部WHERE只获得在线试用... JOIN明确地寻找同一个用户,但在线订购...通过ANDing时差应该是你需要的。

select
      tr.transactionid,
      tr.timestamp,
      tr.transactionType,
      tr.UserID
   from
      transactionRecords tr
         JOIN transactionRecords trOrder
            on tr.UserID = trOrder.UserID
           AND trOrder = 'OnlineOrder'
           AND DATEDIFF(MI, tr.timestamp, trOrder.timestamp) <= 30
   where
      tr.transactionType = 'OnlineTrial'