基于行的时间差基于Analytics的条件

时间:2017-01-11 10:31:10

标签: sql-server tsql powerbi powerbi-embedded

我在名为TABLE的表格中有以下数据:

编辑:添加了另外几行Characterid: 26052013030101,错过了。

    /------------------------------------------------------------------------\
    |      CharacterID     |     EVENTTYPE     |        TRIGGERTIME          |
    |----------------------+-------------------+-----------------------------|
    |    11052016190101    |    START          |   2017-06-01 13:35:38.000   |
    |    11052016190101    |    END            |   2017-01-06 08:05:18.620   |
    |    01012016170101    |    START          |   2017-06-01 13:33:18.000   |
    |    01012016170101    |    Player Left    |   2017-06-01 13:35:21.000   |
    |    01012016170101    |    END            |   2017-06-01 13:38:22.000   |
    |    26052013030101    |    START          |   2017-06-01 13:35:39.000   |
    |    26052013030101    |    RESET          |   2017-06-01 13:35:50.000   |
    \------------------------------------------------------------------------/

我已编写此查询以根据START的{​​{1}}和END值获取时差:

EVENTTYPE

但是,我们想要的是获得上述条件的时差 - 即SELECT cp_start.characterid, MAX(cp_start.triggertime) AS start_time, cp_end.triggertime AS end_time, datediff(second, MAX(cp_start.triggertime), cp_end.triggertime) FROM TABLE AS cp_start INNER JOIN TABLE AS cp_end ON ( cp_start.CharacterID= cp_end.CharacterID AND cp_end.triggertime > cp_start.triggertime) WHERE cp_start.eventtype = 'START' AND cp_end.eventtype = 'END' GROUP BY cp_start.characterid, cp_Start.TriggerTime, cp_end.TriggerTime START - 以及END和{{1}之间是否还有其他事件然后我们需要跳过那个特定的START

在上面的示例中,请参阅ENDCharacterID行的CharacterID = 01012016170101EVENTTYPE='Player Left'值之间有一行START 需要被跳过或不被视为

编辑:在上面,characterid = 26052013030101,只有START但没有END。它有RESET,这意味着我们在显示结果时不应该考虑这个值。 结束编辑

我们如何实现这一目标?

其次,有没有简单的方法在POWERBI中实现这一点并显示计数和时间差?

2 个答案:

答案 0 :(得分:1)

基于一些假设:

  • 对于两个相关的CharacterId值(“START”和“END”),每个EVENTTYPE只有一条记录
  • EVENTTYPE值为“END”的任何记录在TRIGGERTIME中的日期时间值总是晚于CharacterId的{​​{1}}的记录。 START”。

您可以使用以下内容:

EVENTTYPE

如果你想以不同于秒数的方式呈现时差,可以单独处理,而且还有很多其他问题可以解决这个问题。

您可以将SELECT DISTINCT c.CharacterId, start.TRIGGERTIME AS StartTime, [end].TRIGGERTIME AS EndTime, DATEDIFF(s, start.TRIGGERTIME, [end].TRIGGERTIME) AS [TimeDiff(seconds)] FROM [TABLE] c OUTER APPLY ( SELECT TRIGGERTIME FROM [TABLE] s WHERE s.CharacterId = c.CharacterId AND s.EVENTTYPE = 'START' ) start OUTER APPLY ( SELECT TRIGGERTIME FROM [TABLE] e WHERE e.CharacterId = c.CharacterId AND e.EVENTTYPE = 'END' ) [end] 同样移动到OUTER APPLY子句中的子查询中,但这样可以使逻辑更容易跟随恕我直言。

答案 1 :(得分:1)

无论CharacterID启动会话的次数如何,都会找到每条START条记录,然后找到以下END条记录:

declare @t table(CharacterID bigint,EVENTTYPE nvarchar(100),TRIGGERTIME datetime);
insert into @t values
 (11052016190101,'START','2017-01-01 13:35:38.000')
,(11052016190101,'END','2017-01-06 08:05:18.620')
,(01012013010101,'START','2017-06-01 13:33:18.000')
,(01012013010101,'Player Left','2017-06-01 13:35:21.000')
,(01012013010101,'END','2017-06-01 13:38:22.000')
,(01012013010101,'START','2017-07-01 13:33:18.000')
,(01012013010101,'Player Left','2017-07-01 13:35:21.000')
,(01012013010101,'END','2017-07-01 13:38:22.000');

with Starts as
(
    select CharacterID
            ,EVENTTYPE
            ,TRIGGERTIME
    from @t
    where EVENTTYPE = 'START'
)
select s.CharacterID
        ,s.TRIGGERTIME as StartTime
        ,e.TRIGGERTIME as EndTime
from Starts s
    outer apply (select top 1 TRIGGERTIME
                    from @t
                    where CharacterID = s.CharacterID
                        and TRIGGERTIME > s.TRIGGERTIME
                        and EVENTTYPE = 'END'
                    order by TRIGGERTIME
                ) e
order by CharacterID
        ,StartTime;

我稍微更改了您的测试数据,以便它实际上有意义,但使用上面脚本中的数据,输出如下:

CharacterID    | StartTime               | EndTime
---------------+-------------------------+------------------------
1012013010101  | 2017-06-01 13:33:18.000 | 2017-06-01 13:38:22.000
1012013010101  | 2017-07-01 13:33:18.000 | 2017-07-01 13:38:22.000
11052016190101 | 2017-01-01 13:35:38.000 | 2017-01-06 08:05:18.620