尝试获取其他人优先的事件的时间

时间:2019-03-01 17:13:29

标签: sql sql-server sql-server-2012

我正在尝试找出如何获得某人从事各种任务的时间。有3种不同的级别可以掩盖发生在较低级别的事件。级别2隐藏0和1,级别1隐藏0,但被2隐藏,级别0仅在没有其他可用条件时出现

虽然我可以使一个事件的停止在下一个级别结束,但是我不知道如何在上一个事件结束后使它继续。如果我给每个人的时间是一个列表,甚至是每分钟的平均数,那对我想做的事情也同样适用。

当前代码和示例表:

1) To make the color column look clean and neat as the image above because now I tried to adjust its height slight smaller than the card height which is 35
2) The text to be centered
3) The gesture to detect the whole of the text column
4) I have other design where it might have 3 or more column and how to build a separator
5) I have set the card width: 30.0, but it always goes right till the end it never stays at 30.


new Container(
                        height:190,

                        child:
                        new ListView.builder(
                          shrinkWrap: true,
                          itemCount: vdata.length,

                          itemBuilder: (BuildContext ctxt, int index) {

                            return Container(
                              margin: new EdgeInsets.fromLTRB(10, 0, 10, 0),
                              width: 30.0,
                              height: 35.0,

                                  child: Card(
                                    color: Colors.white,
                                    child: Row (
                                      //mainAxisSize: MainAxisSize.max,
                                     // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                              new Column(
                                                 mainAxisAlignment: MainAxisAlignment.center,
 crossAxisAlignment: CrossAxisAlignment.center,

                                                children: <Widget>[

                                                Container(

                                                      decoration: new BoxDecoration(
                                                      color: Colors.amber,
                                                      borderRadius: new BorderRadius.only(
                                                        topLeft: const Radius.circular(5),
                                                        bottomLeft: const Radius.circular(5)
                                                      ),

                                                    ),
                                                  height: 27,
                                                  width: 10,

                                                  ),
                                              ],
                                              ),
                                              new Column(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                mainAxisSize: MainAxisSize.max,
                                                  //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                                children: [
                                                      //new Row( 
                                                          // mainAxisSize: MainAxisSize.max,


                                                            //children: <Widget>[
                                                        new GestureDetector(
                                                          onTap: () {
                                                            setState(() {
                                                              _localVehicleSelected=vdata[index]["pr"].toString();
                                                            });


                                                          doSomething(vdata[index]["pr"].toString());

                                                        }, 
                                                        child:new Text('Test Plate',

                                                                  ),
                                                       ),


                                                    //style: Theme.of(context).textTheme.body2
                                                  //],
                                                 //),



                                            ],
                                          ), 
                                      ],
                                    ),




                                  )
                            );

所需结果:

declare @test table ([Type] varchar(10), [Date] date, [start] time, [stop] time, [level] int)

insert into @test
values ('Shift',getdate(),'08:00','12:00',0),
       ('WorkEvent',getdate(),'08:10','11:00',1),
       ('Break',getdate(),'10:00','10:10',2)

select *,
case 
      when lead([start]) over( partition by [Date] order by start,[stop]) is null then [stop] -- no more records, don't alter stop time
      when lead([start]) over( partition by [Date] order by start,[stop]) > [stop] then [stop] -- gap in records (for break or other logout), don't alter stop time
      else lead([start]) over( partition by [Date] order by start,[stop]) -- use next records start time as stop time to ensure continuity of work period
      end as [NewStop]
from @test

我可以使用的其他结果:

Type        Date        start               stop                level
Shift       2019-03-01  08:00:00.0000000    08:10:00.0000000    0
WorkEvent   2019-03-01  08:10:00.0000000    10:00:00.0000000    1
Break       2019-03-01  10:00:00.0000000    10:10:00.0000000    2
WorkEvent   2019-03-01  10:10:00.0000000    11:00:00.0000000    1
Shift       2019-03-01  11:00:00.0000000    12:00:00.0000000    0

1 个答案:

答案 0 :(得分:1)

我确定这不是最佳速度,而且我还没有用不同的日期进行测试。但是无论如何,这里的查询似乎有效

   with valuable_events(dt, timeSt, [level],tp, knd, min_level, seq) as (
        select all_events.*, top_level_x.min_level, ROW_NUMBER() over (order by all_events.dt, all_events.timeSt)
        from
        ( select [Date] as dt, [start] as timeSt, [level], type, 'start' as knd
          from @test T1
            union 
          select [Date] as dt, [stop] as timeSt, [level], type, 'stop' as knd
           from @test T2 
        )all_events
        outer apply (select max([level]) max_level , min([level]) min_level
                     from @test top_prio
                     where all_events.dt = top_prio.Date
                        and all_events.timeSt between top_prio.start and top_prio.stop
                     ) top_level_x
        where all_events.level = top_level_x.max_level
    )
    select iif(evnt.knd='start', evnt.tp, next_evnt.tp) as [Type], 
            evnt.dt as [Date], 
            evnt.timeSt as [start], 
            next_evnt.timeSt as [stop], 
            iif(evnt.knd='start', evnt.level, next_evnt.level) as [Level]
            --, *
    from valuable_events evnt
            join valuable_events next_evnt on evnt.seq = next_evnt.seq-1
    where not (evnt.level = evnt.min_level 
                and evnt.knd = 'stop'
                and next_evnt.knd = 'start')