简体   繁体   English

sql server数据透视表查询

[英]sql server pivot table query

we are trying to convert an attendance log data into a pivot table . 我们正在尝试将出勤记录数据转换为数据透视表。 for the sake of simplicity, the actual data is in the form : 为了简单起见,实际数据采用以下形式:

EmployeeId, InOrOut,  DateTime
1            0        2019-01-01 08:00:00
1            1        2019-01-01 17:00:00
1            0        2019-01-02 08:00:00
1            1        2019-01-02 17:00:00
2            0        2019-01-01 08:00:00
2            1        2019-01-01 17:00:00

and we need to make it like so: 我们需要这样:

EmployeeId, Date,        InTime , OutTime
1           2019-01-01   08:00    17:00
1           2019-01-02   08:00    17:00
2           2019-01-01   08:00    17:00

however the query we have made doesnt seem to work like this. 但是,我们进行的查询似乎无法像这样工作。 the query is as under: 查询如下:

SELECT * FROM
(
SELECT aml.EnrollNumber, aml.A_Date, aml.InOutMode, aml.A_Time
 FROM dbo.Attendence_Machines_LOG aml) AS AttendanceTable
PIVOT (
     max(A_Date)
    FOR InOutMode in ([1],[0])
) as PivotTable

There are many confusions regarding pivot and personally cannot find much tutorials. 关于数据透视有很多困惑,个人找不到太多的教程。

how do we tell the query that on which basis is the data put in a new row (for example in this case, how will we tell the query to seperate the records according to date and employeeid) 我们如何告诉查询将数据放在新行的基础上(例如,在这种情况下,我们如何告诉查询根据日期和员工编号分隔记录)

any help appreciated 任何帮助表示赞赏

You can do aggregation : 您可以进行聚合:

SELECT aml.EnrollNumber, aml.A_Date,
       MAX(CASE WHEN aml.InOutMode = 1 THEN aml.A_Time END),
       MAX(CASE WHEN aml.InOutMode = 0 THEN aml.A_Time END)
FROM dbo.Attendence_Machines_LOG AS aml
GROUP BY aml.EnrollNumber, aml.A_Date;

EDIT : After question edit made : 编辑:问题编辑后:

SELECT aml.EnrollNumber, CAST(aml.A_Date AS DATE),
       MAX(CASE WHEN aml.InOutMode = 1 THEN aml.A_Time END),
       MAX(CASE WHEN aml.InOutMode = 0 THEN aml.A_Time END)
FROM dbo.Attendence_Machines_LOG AS aml
GROUP BY aml.EnrollNumber, CAST(aml.A_Date AS DATE);

If you want this is pivot then try this: 如果您希望这是关键,请尝试以下操作:

    GO
    create table #temptable ( empid int, inorout int, attdate datetime )
    Go

    insert into #temptable ( empid, inorout, attdate )
    values ( 1 ,           0        ,'2019-01-01 08:00:00'),
    (1            ,1        ,'2019-01-01 17:00:00')
    ,(1            ,0        ,'2019-01-02 08:00:00')
    ,(1            ,1        ,'2019-01-02 17:00:00')
    ,(2            ,0        ,'2019-01-01 08:00:00')
    ,(2            ,1        ,'2019-01-01 17:00:00')


    select * from #temptable

    select empid, atdate, [0], [1]  from (
    select empid,inorout,  CAST(attdate as DATE) as atdate, attdate from #temptable ) as d
    pivot 
    ( max(attdate) for inorout in ( [0], [1] )
    ) as pv

    go

    drop table #temptable

If any confusion feel free to ask. 如有任何疑问,请随时提出。

Note: But this will work only if you have only 1 In and 1 Out per day as your given raw data. 注意:但这仅在每天只有1进1出作为给定原始数据的情况下才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM