Sql 选择&;用户ID上的组,其中事务类型适合特定订单,并且在30分钟内

Sql 选择&;用户ID上的组,其中事务类型适合特定订单,并且在30分钟内,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,在SQLServer2008R2数据库中有一个表,我们在其中存储TransactionID、时间戳、TransactionType、UserID和其他信息 创建表[dbo]。[TransactionRecords]( [TransactionID][float]空, [时间戳][日期时间]空, [TransactionType][nvarchar](255)空, [UserID][nvarchar](255)空 ) 我想做的是查询,这样我就可以返回用户的事务ID,这些用户作为OnlineTria

在SQLServer2008R2数据库中有一个表,我们在其中存储TransactionID、时间戳、TransactionType、UserID和其他信息

创建表[dbo]。[TransactionRecords](
[TransactionID][float]空,
[时间戳][日期时间]空,
[TransactionType][nvarchar](255)空,
[UserID][nvarchar](255)空
)

我想做的是查询,这样我就可以返回用户的事务ID,这些用户作为OnlineTrial登录,然后在时间戳的30分钟窗口内返回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
选择不同的[TransactionID]
,a.[时间戳]
,a.[TransactionType]
,a.[UserID]
来自[Adhoc].[dbo].[TransactionRecords]a
左外部联接[Adhoc].[dbo].[TransactionRecords]b
在a.UserID=b.UserID上

其中DATEDIFF(MI,a.timestamp,b.timestamp)您可以使用
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
    )

应该是基于用户ID和显式事务类型引用的简单自身连接。外面只有在线试用版。。。JOIN显式查找同一用户,但在线订单。。通过计算时差,你就应该得到所需的时间

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'
选择
tr.transactionid,
tr.timestamp,
tr.transactionType,
tr.UserID
从…起
交易记录
加入transactionRecords命令
在tr.UserID=trOrder.UserID上
TroOrder='OnlineOrder'

和DATEDIFF(MI,tr.timestamp,trOrder.timestamp)(1)SQL Server的哪个版本?(2) 您应该包括示例数据、所需结果和示例查询。这绝对是一个图片不值1000字的论坛。@jpw更好的是,一个
create table
insert
script@GordonLinoff刚刚更新,谢谢。@Laurence刚刚包括脚本,谢谢。我实际上在查找两个订单行,但顺序过程是OnlineTrial,然后在30分钟内由同一用户ID进行OnlineOrder。加入只会让我得到订单,也不一定是这样。@TroySteen,我从在线试用开始,然后才寻找订单。。。基于这些试验。如果DATEDIFF函数与预期相反(30分钟),则可能通过DATEDIFF(MI,trOrder.timestamp,tr.timestamp)进行更改
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'