如何划分SQL单列以供多次使用

时间:2016-01-04 11:42:33

标签: sql sql-server sql-server-2008-r2

我的数据库表值插入如下。

 Userid       AttedanceDate             SessionType 

 mazhar    2016-01-02 10:37:22.397         login

 mazhar    2016-01-03 10:38:24.970         logout

 mazhar    2016-01-02 11:39:22.397         login

 mazhar     2016-01-02 11:40:24.970        logout

 mazhar    2016-01-03 10:37:22.397         login

 mazhar     2016-01-03 10:38:24.970        logout

我需要如下结果。

Userid       AttedanceDatelogin         AttedanceDatelogout        Total Hours   

 mazhar        2016-01-02 10:37:22.397    2016-01-02 11:40:24.970      01:02
 mazhar        2016-01-03 10:37:22.397    2016-01-03 10:38:24.970      00:01

我的选择查询。

select  t1.AttendanceDate,t1.Userid,t1.SessionType from Table_Branches_AttendanceInfo as t1 inner join Table_Authorize_Users as t2 on t1.Userid=t2.Userid where (CONVERT(varchar(50), AttendanceDate, 101) BETWEEN @Fromdate AND @Todate)

3 个答案:

答案 0 :(得分:1)

SQL Server 中,您可以通过以下方式实现它:

<强> QUERY

 select userid
        , max(case when SessionType = 'login' then AttedanceDate end) as AttedanceDatelogin          
        , max(case when SessionType = 'logout' then AttedanceDate end) as AttedanceDatelogout  
        , datediff(hh, max(case when SessionType = 'login' then AttedanceDate end), max(case when SessionType = 'logout' then AttedanceDate end)) Hours  
 from #t
 group by UserId

示例数据

create table #t
(
   Userid nvarchar(60),       
   AttedanceDate datetime,
   SessionType  nvarchar(60)
)
insert into #t values 
   ('mazhar','2016-01-02 10:37:22.397','login'),
   ('mazhar','2016-01-03 10:38:24.970','logout'),
   ('mazhar','2016-01-02 11:39:22.397','login'),
   ('mazhar','2016-01-02 11:40:24.970','logout'),
   ('mazhar','2016-01-03 10:37:22.397','login'),
   ('mazhar','2016-01-03 10:38:24.970','logout')

<强>输出

userid  AttedanceDatelogin      AttedanceDatelogout         Hours
mazhar  2016-01-02 10:37:22.397 2016-01-02 11:37:24.970     1

编辑#1

根据您的评论,您可以遵循:

 select userid
        , min(case when SessionType = 'login' then AttedanceDate end) as AttedanceDatelogin          
        , max(case when SessionType = 'logout' then AttedanceDate end) as AttedanceDatelogout  
        , convert(char(5),dateadd(ss,datediff(ss, min(case when SessionType = 'login' then AttedanceDate end), max(case when SessionType = 'logout' then AttedanceDate end)),'19000101'),8) Hours  
 from #t
 group by UserId, convert(date, AttedanceDate)

编辑后输出

userid  AttedanceDatelogin          AttedanceDatelogout         Hours
mazhar  2016-01-02 10:37:22.397     2016-01-02 11:40:24.970     01:03
mazhar  2016-01-03 10:37:22.397     2016-01-03 10:38:24.970     00:01

答案 1 :(得分:0)

您可以使用条件聚合

SELECT UserId, 
       MAX(CASE WHEN SessionType = 'login' THEN AttedanceDate END) AS AttedanceDatelogin,
       MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END) AS AttedanceDatelogout,
       DATEDIFF(hour, 
                MAX(CASE WHEN SessionType = 'login' THEN AttedanceDate END),
                MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END)) AS TotalHours
FROM Table_Branches_AttendanceInfo as t
GROUP BY Userid

修改

对于每天多次登录/注销,您可以使用以下查询,该查询仅考虑每天的第一次登录和上次注销:

SELECT UserId, 
       MIN(CASE WHEN SessionType = 'login' THEN AttedanceDate END) AS AttedanceDatelogin,
       MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END) AS AttedanceDatelogout,
       DATEDIFF(hour, 
                MIN(CASE WHEN SessionType = 'login' THEN AttedanceDate END),
                MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END)) AS TotalHours
FROM Table_Branches_AttendanceInfo as t
GROUP BY Userid, CONVERT(DATE, AttedanceDate)

<强> EDIT2:

要获得小时/分钟,您可以使用:

SELECT UserId, 
       MIN(CASE WHEN SessionType = 'login' THEN AttedanceDate END) AS AttedanceDatelogin,
       MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END) AS AttedanceDatelogout,
       DATEDIFF(hour, 
                MIN(CASE WHEN SessionType = 'login' THEN AttedanceDate END),
                MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END)) AS Hours,
       DATEDIFF(minute, 
                MIN(CASE WHEN SessionType = 'login' THEN AttedanceDate END),
                MAX(CASE WHEN SessionType = 'logout' THEN AttedanceDate END)) % 60 AS Minutes
FROM mytable as t
GROUP BY Userid, CONVERT(DATE, AttedanceDate)

Demo here

答案 2 :(得分:0)

试试这个

select userid
        , min(case when SessionType = 'login' then AttedanceDate end) as AttedanceDatelogin          
        , max(case when SessionType = 'logout' then AttedanceDate end) as AttedanceDatelogout  
        ,CONVERT(varchar(5), DATEADD(minute, DATEDIFF(minute, min(case when SessionType = 'login' then AttedanceDate end), max(case when SessionType = 'logout' then AttedanceDate end)), 0), 114) as TotalHours
 from TableDemo
 group by UserId,CONVERT(date,AttedanceDate)