如何按日期分组并在rdlc报告中获取所有进出时间?

时间:2018-02-18 14:35:36

标签: sql-server rdlc

这是我的sql查询输出,如下所示:

DATEEVENT   emp_code    emp_name      entry           DT            time
2018-01-04     102       Ihsan Titi    IN   2018-01-04 15:57:04.000 15:57
2018-01-04     102       Ihsan Titi    IN   2018-01-04 15:57:05.000 15:57
2018-01-04     102       Ihsan Titi    OUT  2018-01-04 16:46:19.000 16:46
2018-01-04     102       Ihsan Titi    IN   2018-01-04 18:15:27.000 18:15
2018-01-04     102       Ihsan Titi    OUT  2018-01-04 18:20:47.000 18:20
2018-01-04     102       Ihsan Titi    IN   2018-01-04 20:02:05.000 20:02

但在我的报告中,我将行分为读者ID和date_event,并按列分组列。它没有得到所有的in_time和out_time。它只得到一个进出时间。我的报告输出如下图所示。< / p>

enter image description here

预期产出:

 DATEEVENT  emp_code          IN                       OUT
2018-01-04     102    2018-01-04 15:57:04.000           -
                      2018-01-04 15:57:04.000  2018-01-04 16:46:19.000 16:46
                      2018-01-04 18:15:27.000  2018-01-04 18:15:27.000 18:15
                      2018-01-04 20:02:05.000           -

任何人都可以提供帮助..提前致谢

1 个答案:

答案 0 :(得分:1)

检查此查询。如果您想为某些字段显示空白值,最好在报告中执行此操作

declare @t table (
    emp_code int
    , emp_name varchar(100)
    , entry varchar(5)
    , DT datetime
)
insert into @t
values 
    (102, 'Ihsan Titi', 'IN', '20180104 15:57:04.000')
    , (102, 'Ihsan Titi', 'IN', '20180104 15:57:05.000')
    , (102, 'Ihsan Titi', 'OUT', '20180104 16:46:19.000')
    , (102, 'Ihsan Titi', 'IN', '20180104 18:15:27.000')
    , (102, 'Ihsan Titi', 'OUT', '20180104 18:20:47.000')
    , (102, 'Ihsan Titi', 'IN', '20180104 20:02:05.000')

select
    DATEEVENT = min(cast(DT as date)), emp_code
    , [IN] = isnull(max(iif(rn = 1, convert(varchar(19), DT, 120), NULL)), '-')
    , [OUT] = isnull(max(iif(rn = 2, convert(varchar(19), DT, 120), NULL)), '-')
from (
    select
        *, rn = row_number() over (partition by emp_code, grp order by DT)
    from (
        select 
            *, grp = sum(iif(entry = 'IN', 1, 0)) over (partition by emp_code order by DT)
        from 
            @t
    ) t
) t
group by emp_code, grp

输出:

DATEEVENT   emp_code    IN                     OUT
------------------------------------------------------------------
2018-01-04  102         2018-01-04 15:57:04    -
2018-01-04  102         2018-01-04 15:57:05    2018-01-04 16:46:19
2018-01-04  102         2018-01-04 18:15:27    2018-01-04 18:20:47
2018-01-04  102         2018-01-04 20:02:05    -

实际上不需要row_number

select
    DATEEVENT = min(cast(DT as date)), emp_code
    , [IN] = min(convert(varchar(19), DT, 120))
    , [OUT] = iif(max(convert(varchar(19), DT, 120)) = min(convert(varchar(19), DT, 120)), '-', max(convert(varchar(19), DT, 120)))
from (
    select 
        *, grp = sum(iif(entry = 'IN', 1, 0)) over (partition by emp_code order by DT)
    from 
        @t
) t
group by emp_code, grp