需要SQL查询的帮助

时间:2011-07-21 11:22:49

标签: sql sql-server

我有以下数据:

TransactionType        Lane      TransactionDate
------------------------------------------------
           Sale          17  2011-07-21 06:15:34
         SignOn          18  2011-07-21 07:00:00
           Sale          18  2011-07-21 07:06:43
         SignOn          20  2011-07-21 08:01:12
           Sale          18  2011-07-21 07:30:00
           Sale          18  2011-07-21 08:30:00
           Sale          18  2011-07-21 09:30:50
           Sale          20  2011-07-21 10:15:34
        SignOff          18  2011-07-21 19:00:00
        SignOff          20  2011-07-21 19:10:52

我需要获取以下信息:

  1. SignOn和SignOff交易类型之间的lane计数
  2. 每个lane处于SignOn状态的总时间,例如{18}的lane行为12小时,lane 20的行的时间为11小时9分40秒

2 个答案:

答案 0 :(得分:1)

以下内容并未更正每个泳道的多次登录 - 签收事件,但至少是一个开始。 如果我有关于每天允许多少次登录签名事件和/或其他约束的信息,我会尝试调整它,以便它适用于一般情况。

  

1. SignOn和SignOff交易类型之间的车道数

SELECT      sales.lane, signon.transactiondate, count(*) as salecount
FROM        table1 sales 
INNER JOIN  table1 signon 
        ON   (signon.lane = sales.lane AND signon.transactiontype = 'SignOn')
INNER JOIN  table1 signoff  
        ON   (signoff.lane = sales.lane AND signoff.transactiontype = 'SignOff')
WHERE       sales.transactiontype = 'Sale' 
            AND sales.transactiondate 
              BETWEEN signon.transactiondate AND signoff.transactiondate
GROUP BY    sales.lane, signon.transactiondate
  

2.每个泳道处于SignOn状态的总时间,例如具有18号车道的行是12小时,具有车道20的行具有11小时,9分40秒的时间

SELECT      signon.lane,
            datediff(hour, signon.transactiondate, signoff.transactiondate) as hours,
            datediff(minute, signon.transactiondate, signoff.transactiondate) as minutes,
            datediff(second, signon.transactiondate, signoff.transactiondate) as seconds,

FROM        table1 signon
INNER JOIN  table1 signoff 
        ON    (signoff.lane = signon.lane 
              AND signoff.transactiondate > signon.transactiondate
              AND signoff.transactiontype = 'SignOff')
WHERE       signon.transactiontype = 'SignOn'                               
GROUP BY    signon.lane, signon.transactiondate

答案 1 :(得分:1)

这将获得大部分内容......包括每个通道多次登录/退出以及每个登录/退出时间段内的多少次交易。获得此结果后,您可以从此查询以获取活动车道/会话的总计数,以及任何累积值...例如,总共18次登录/退出总销售交易的总次数,或者只是总计的数量车道是开放的,并说明了这种销售活动。

由于group by是在lane,signon / off,我只是将MIN()应用于小时,分钟,秒,这对于每个登录/关闭会话始终是一个常量。

SELECT 
      PreQuery.Lane,
      PreQuery.SignedOn,
      PreQuery.SignedOff,
      MIN( datediff(hour, PreQuery.SignedOn, PreQuery.SignedOff )) as hours,
      MIN( datediff(minute, PreQuery.SignedOn, PreQuery.SignedOff )) as minutes,
      MIN( datediff(second, PreQuery.SignedOn, PreQuery.SignedOff )) as seconds,
      COUNT(*) as NumOfSales 
   from 
      ( SELECT 
              T1.Lane,
              T1.TransactionDate SignedOn,
              MIN( T2.TransactionDate ) as SignedOff
           from 
              Trans1 T1
                 LEFT JOIN Trans1 T2
                    ON T1.Lane = T2.Lane
                    AND T2.TransactionType = "SignOff"
                    AND T2.TransactionDate > T1.TransactionDate 
           where 
              T1.TransactionType = "SignOn" 
           group by 
              T1.Lane,
              T1.TransactionDate ) PreQuery 
      LEFT Join Trans1 T3 
         on PreQuery.Lane = T3.Lane 
        AND T3.TransactionType = "Sale" 
        AND T3.TransactionDate BETWEEN PreQuery.SignedOn AND PreQuery.SignedOff
   GROUP BY 
      PreQuery.Lane,
      PreQuery.SignedOn,
      PreQuery.SignedOff