简体   繁体   English

SQL查询以月份为列返回月份,以小时为总小时返回

[英]SQL Query to return Month days as Columns and Total Hours as Row

i am working on an Attendance report were I need to create a SQL Query from one table to return the attendance of employees net hours worked over the month. 我正在处理出勤报告,是否需要从一个表创建一个SQL查询以返回员工当月的净工作时间。 Day of the month should be as a column and in the rows should be the total Hours of employee. 月份中的一天应该作为一列,而行中应该是员工的总小时数。

The Table is having 6 Columns ( Employee Name, Dept , Position, Time In , Time Out and Total Hours 该表有6列(员工姓名,部门,职位,进场时间,超时时间和总工时

Picture for Selecting * From the Attendance Table 用于选择的图片*从出勤表中

i want to return the values as the following: 我想返回以下值:

EmployeeName | 员工姓名 | 1st | 1st | 2nd | 2号 | 3rd | 第三名 4th | 4号 | ...... | ...... | 30 June 6月30日


Emp 1 | Emp 1 | 10:30 | 10:30 | | | 10:40 | 10:40 | | | 10:10 | 10:10 | | | 10:21 | 10:21 |

The Days column should be returned in a parameter so i can add it to crystal report. 应该在参数中返回“天数”列,以便我可以将其添加到Crystal报表中。

Table Structure if you can advise please. 表结构,如果您可以建议的话。

Thanks in advance 提前致谢

You can use CASE statment like this: 您可以这样使用CASE语句:

        SELECT EmployeeName,
               (CASE WHEN EXTRACT(YEAR FROM DATE) = 2017 AND EXTRACT(MONTH FROM DATE) = 6 AND EXTRACT(DAY FROM DATE) = 1 then totalHours ELSE NULL END) AS "01/06",
               (CASE WHEN EXTRACT(YEAR FROM DATE) = 2017 AND EXTRACT(MONTH FROM DATE) = 6 AND EXTRACT(DAY FROM DATE) = 2 then totalHours ELSE NULL END) AS "02/06",
               .
               .
               .
               (CASE WHEN EXTRACT(YEAR FROM DATE) = 2017 AND  EXTRACT(MONTH FROM DATE) = 6 AND EXTRACT(DAY FROM DATE) = 30 then totalHours ELSE NULL END) AS "30/06"

FROM Attendance

So, for each day a new column will be created. 因此,每天都会创建一个新列。

I used something like this 我用这样的东西

        CREATE TABLE `AxsLog` (
`id`    integer NOT NULL UNIQUE,
`Logon` text NOT NULL DEFAULT current_timestamp,
`Logoff`    text NOT NULL DEFAULT current_timestamp,
`Duration`  text NOT NULL DEFAULT 0,
`SysDat`    text NOT NULL DEFAULT current_timestamp,
PRIMARY KEY(`id`) );

You can easily add an FK column for each row in your user table. 您可以轻松地为用户表中的每一行添加FK列。

Keep the logon id for each entry, then update that line on logoff 保留每个条目的登录ID,然后在注销时更新该行

UPDATE  AxsLog
Set Duration= (SELECT sum( strftime('%s', logoff) - strftime('%s', logon) ) 
/60 FROM AxsLog WHERE id= 1 )
WHERE id= 1 ; 

To build a report, use something like this. 要生成报告,请使用类似以下的内容。 This query only gives a total per month. 该查询仅给出每月总计。

select total(Duration)  
FROM AxsLog where substr(sysdat,6,2) = 'month' 

您的要求可以通过使用交叉表报表来满足,或者如果您想在sql中实现,则可以使用数据透视表

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

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