3

あなたが私の問題で私を助けることができればクールだろう。ゲームのイベントを見ることができるテーブルがあります。Valueすべてのプレイヤーは、後の列に表示されるある程度のゴールドを持っていますLog in。私は、金の人々がその後最初のゲームをどれだけプレイし始めるかを知る必要がありますLog in。プレイヤーがゲームを開始する前に、金の量を増やすことができBuy goldます。Get bonusValue

**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     

私が探している結果はそのようなものです。これは基本的に、プレーヤーStart gameと最新Log inのプレーヤーの間の値の合計です。

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

ありがとうございました!

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

1つのアプローチは、各行を検索することLog inです。次に、またはのいずれかである次の行を検索しLog inますStart game。その行がの場合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フィドルでの例。

于 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 に答える