SQL从多个日期时间寄存器获取最小小时

时间:2018-07-05 23:45:39

标签: sql-server tsql

我需要显示INFO列,在REGISTRATION列中显示每个日期的最少小时,在LOG中显示一个小时

Log CAT   INFO       REGISTRATION
10  1   551203  2018-06-04 08:47:54.000
10  1   551549  2018-06-05 08:59:02.000
579 1   551675  2018-06-05 10:13:36.000
579 1   553681  2018-06-05 11:31:44.000
579 1   551707  2018-06-05 12:57:33.000
579 1   551364  2018-06-04 10:16:04.000
579 1   551378  2018-06-04 10:39:01.000
579 1   551379  2018-06-04 10:40:22.000
579 1   551406  2018-06-04 15:47:52.000
580 1   550922  2018-06-04 11:21:01.000
580 1   551001  2018-06-04 12:43:22.000
580 1   553321  2018-06-04 15:37:52.000

正是这样,每个INFO是每个LOG的每个日期的最小小时数

INFO
551203       -->(2018-06-04 08:47:54.000)        
551675       -->(2018-06-05 10:13:36.000)   
551364       -->(2018-06-04 10:16:04.000)   
550922       -->(2018-06-04 11:21:01.000)   

谢谢!

1 个答案:

答案 0 :(得分:0)

假设信息值按升序显示,那么我相信这就是您要寻找的:

select min(info) as info, min(registration) as registration
from log
group by log, cast(registration as date);

或者仅使用row_number()避免做出该假设:

with data as (
    select *,
        row_number() over 
            (partition by log, cast(registration as date) order by registration) as rn
    from log
)
select * from data where rn = 1;