3

Would be cool if you could help me with my problem. I have a table where you can see events from a game. Every player has some amount of gold which you can see in a Value column after Log in. I need to find out with how much gold people start playing their FIRST game after Log in. Before players start the game they can Buy gold or Get bonus which will raise their amount of gold by Value

**Player ID      Timestamp      Action     Value**    
    1111           09:11        Log in      500
    1111           09:25        Buy gold    100
    1111           09:28        Get bonus    50
    1111           09:30        Start game      
    2222           11:14        Log in      800
    3333           12:01        Log in      700
    3333           12:04        Get bonus    50
    3333           12:08        Start game   
    3333           12:15        Buy gold    100
    3333           12:18        Start game
    1111           14:40        Log in      300
    1111           14:42        Buy gold    100
    1111           14:50        Start game
    2222           15:22        Log in      600
    2222           15:25        Buy gold    100
    2222           16:25        Log in      400
    2222           16:30        Get bonus    50
    2222           16:35        Start game     

Te result I am looking for is something like that. It is basically a SUM of Values between Start game and the most recent Log in of a player.

**Match number   Player ID     Value**
     1             1111         650
     2             3333         750
     3             1111         400
     4             2222         450

Thank you!

4

3 回答 3

1

您可以尝试一些相关的子查询:

SELECT PlayerID, SUM(value) FROM table a
WHERE
TimeStamp >= (SELECT MAX(TimeStamp) FROM table WHERE a.PlayerID = PlayerID AND Action = 'Log in' )
GROUP BY PlayerID
于 2013-03-24T13:13:34.277 回答
1

一种方法是搜索每一Log in行。然后查找下一行是Log inor Start game。如果该行是 a Start game,则您有一对标识登录后的第一个游戏。然后,您可以找到该对之间所有行的值的总和:

select  row_number() over (order by start.Timestamp) as MatchNumber
,       start.[Player ID]
,       vals.SumValue
from    YourTable login
cross apply
        (
        select  top 1 *
        from    YourTable start
        where   login.[Player ID] = start.[Player ID]
                and login.Timestamp < start.Timestamp 
                and start.Action in ('Start game', 'Log in')
        order by
                start.Timestamp
        ) start
cross apply
        (
        select  sum(cast(vals.Value as int)) as SumValue
        from    YourTable vals
        where   vals.[Player ID] = start.[Player ID]
                and vals.Action in ('Log in', 'Get bonus', 'Buy gold')
                and vals.Timestamp between login.Timestamp and start.Timestamp
        ) vals
where   login.Action = 'Log in'
        and start.Action = 'Start game'

SQL Fiddle 的示例。

于 2013-03-24T13:21:38.510 回答
0

这有点复杂。我的方法是为每次登录查找下一个时间戳,即“登录”或“开始游戏”,然后返回原始表以查找所有金牌条目。

select tli.playerId, tli.TimeStamp, sum(value)
from (select t.*,
             (select top 1 timestamp
              from t t2
              where t2.playerId = t.playerId and
                    t2.timestamp > t.timestamp and
                    t2.action in ('log in', 'start game')
              order by timestamp
             ) as nextTimestamp
      from t
      where t.action in ('log in')
     ) tli join
     t
     on t.playerId = tli.playerId and
        t.timestamp >= tli.timestamp and
        t.timestamp < tli.nextTimeStamp
group by tli.playerId, tli.timeStamp
于 2013-03-24T13:15:50.180 回答