SQL 使用 dateadd() 的服务器 PIVOT

SQL Server PIVOT with dateadd()

我正在尝试从名为 BioStar 的数据库生成报告,该数据库来自 Suprema 指纹机。这是我拥有的数据示例:

nDateTime           sUserName           department Event EventName nUserID
---------           ---------            --------- -----  --------  ------
2015-08-15 07:52:37.000 Hamdy Mokhtar Moelem  Nursing   0   In      75
2015-08-15 16:26:14.000 Hamdy Mokhtar Moelem  Nursing   1   Out     75
2015-08-16 07:59:18.000 Hamdy Mokhtar Moelem  Nursing   0   In      75
2015-08-16 07:59:25.000 Hamdy Mokhtar Moelem  Nursing   0   In      75
2015-08-16 16:06:24.000 Hamdy Mokhtar Moelem  Nursing   1   Out     75
2015-08-16 16:06:30.000 Hamdy Mokhtar Moelem  Nursing   1   Out     75

这是我正在使用的存储过程:

    ALTER proc [dbo].[viewLogInfo]

@UserIdn int,
@startDate smalldatetime,
@endDate smalldatetime

as
--exec viewLogInfo '75','2015-08-15','2015-08-25'

select  DATEADD(s,el.nDateTime,'1970-01-01') nDateTime, sUserName,ud.sName,el.nTNAEvent,dek.sEventName,nUserID from tb_Event_Log el
join TB_USER u on el.nUserID=u.nUserIdn
join TB_EVENT_DATA ed on el.nEventIdn=ed.nEventIdn
join TB_TA_DEVICE_EVENT_KEY dek on el.nTNAEvent=dek.nKeyIdn
join TB_USER_DEPT ud on u.nDepartmentIdn=ud.nDepartmentIdn
where el.nUserID=@UserIdn and (DATEADD(s,el.nDateTime,'1970-01-01')  between @startDate and @endDate)

order by el.nEventLogIdn 

我希望报告看起来像这样:

nDateTime           sUserName               IN            OUT
---------           ---------               --            ---
2015-08-15          Hamdy Mokhtar Moelem    07:52:37      16:26:14

这是我目前尝试过的方法:

USE [BioStar]
GO
/****** Object:  StoredProcedure [dbo].[viewLogInfo12]    Script Date: 09/11/2015 20:42:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[viewLogInfo12]

--@UserIdn int,
@startDate smalldatetime,
@endDate smalldatetime


as
-- exec viewLogInfo12 '2015-08-01','2015-08-31'
select sUserName,departmentName,event01,nUserID,nDateTime2,ev1,
        [in] as inTime, [out] as outTime
        e

from

(
select  sUserName,ud.sName departmentName,el.nTNAEvent event01,dek.sEventName,dek.sEventName ev1,nUserID,DATEADD(s,el.nDateTime,'1970-01-01') nDateTime1,DATEADD(s,el.nDateTime,'1970-01-01') nDateTime2 from tb_Event_Log el
join TB_USER u on el.nUserID=u.nUserIdn
join TB_EVENT_DATA ed on el.nEventIdn=ed.nEventIdn
join TB_TA_DEVICE_EVENT_KEY dek on el.nTNAEvent=dek.nKeyIdn
join TB_USER_DEPT ud on u.nDepartmentIdn=ud.nDepartmentIdn
where  (DATEADD(s,el.nDateTime,'1970-01-01')  between @startDate and @endDate)

 ) m

pivot 

( max(sEventName) 
for sEventName in ([in],[out]) ) pvt

inTime和outTime均为空。 请帮忙。

cte 中的查询是您的过程中的查询。您还可以将数据插入临时 table 而不是使用 CTE。

WITH CTE
AS
(
    --This is your query
    SELECT Dateadd(s, el.ndatetime, '1970-01-01') nDateTime, 
           susername, 
           ud.sname, 
           el.ntnaevent, 
           dek.seventname, 
           nuserid  
    FROM   tb_event_log el 
           JOIN tb_user u 
             ON el.nuserid = u.nuseridn 
           JOIN tb_event_data ed 
             ON el.neventidn = ed.neventidn 
           JOIN tb_ta_device_event_key dek 
             ON el.ntnaevent = dek.nkeyidn 
           JOIN tb_user_dept ud 
             ON u.ndepartmentidn = ud.ndepartmentidn 
    WHERE  el.nuserid = @UserIdn 
           AND ( Dateadd(s, el.ndatetime, '1970-01-01') BETWEEN 
                 @startDate AND @endDate ) 
)    
   --***This is where the magic happens***
    SELECT dateprt, 
           susername, 
           Min([in])  AS [In], 
           Max([out]) AS [out] 
    FROM   (SELECT Cast(ndatetime AS DATE) AS dateprt, 
                   Cast(ndatetime AS TIME) AS timeprt, 
                   * 
            FROM   CTE) pv 
           PIVOT (Max(timeprt) 
                 FOR eventname IN ([In], 
                                   [Out])) AS pvt 
    GROUP  BY dateprt, 
              susername 

Fiddle 这里.. http://sqlfiddle.com/#!3/e3218/1