无法生成我的报告

时间:2010-09-07 06:39:56

标签: sql sql-server reporting

我必须以这种格式生成报告:

  

staffcode名称dateintime dateoutime duration

例如:

  

员工代码名称01-08-2010intime 01-08-20100uttime 01-08-2010duration

     

1001 Amit 09:00 18:30 09:30

在sql server管理工作室工作了5天我写了这样的查询

Declare @intime table
(
Staffcode varchar(7),
Name varchar(100),
[01-08-2010 InTime] datetime,
[02-08-2010 InTime] datetime,
[03-08-2010 InTime] datetime,
[04-08-2010 InTime] datetime,
[05-08-2010 InTime] datetime
)

Declare @outtime table
(
Staffcode varchar(7),
Name varchar(100),
[01-08-2010 outTime] datetime,
[02-08-2010 outTime] datetime,
[03-08-2010 outTime] datetime,
[04-08-2010 outTime] datetime,
[05-08-2010 outTime] datetime
)

Declare @Duration table
(
Staffcode varchar(7),
Name varchar(100),
[01-08-2010 Duration] datetime,
[02-08-2010 Duration] datetime,
[03-08-2010 Duration] datetime,
[04-08-2010 Duration] datetime,
[05-08-2010 Duration] datetime
)

Insert into @Intime
SELECT
StaffCode,
Name,
[01-08-2010] as [01-08-2010],
[02-08-2010] as [02-08-2010],
[03-08-2010] as [03-08-2010],
[04-08-2010] as [04-08-2010],
[05-08-2010] as [05-08-2010]
FROM
(
SELECT
StaffCode,
Name,
CONVERT(VARCHAR(10),AttendanceDate,105) AttendanceDate,
CONVERT(VARCHAR(5),MIN(FirstPunch),108) InTime
FROM AttendanceLog
JOIN Staff on Staff.Id = AttendanceLog.StaffId
WHERE AttendanceDate BETWEEN '2010-08-01' And '2010-08-05'
AND Staffcode BETWEEN '10001' AND '10999'
AND name <>'' and workstatus = 'Is Working'
GROUP BY
name,StaffCode,AttendanceDate
)p

PIVOT
(
  MIN(InTime)
  FOR AttendanceDate
  IN
(
[01-08-2010],
[02-08-2010],
[03-08-2010],
[04-08-2010],
[05-08-2010]
)
)AS pvt


Insert into @Outtime

SELECT
StaffCode,
Name,
[01-08-2010] as [01-08-2010],
[02-08-2010] as [02-08-2010],
[03-08-2010] as [03-08-2010],
[04-08-2010] as [04-08-2010],
[05-08-2010] as [05-08-2010]
FROM
(
SELECT
StaffCode,
Name,
CONVERT(VARCHAR(10),AttendanceDate,105) AttendanceDate,
CONVERT(VARCHAR(5),MAX(LastPunch),108) OutTime
FROM AttendanceLog
JOIN Staff on Staff.Id = AttendanceLog.StaffId
WHERE AttendanceDate BETWEEN '2010-08-01' And '2010-08-05'
AND Staffcode BETWEEN '10001' AND '10999'
AND name <>'' and workstatus = 'Is Working'
GROUP BY
name,StaffCode,AttendanceDate
)p

PIVOT
(
  MAX(OutTime)
  FOR AttendanceDate
  IN
(
[01-08-2010],
[02-08-2010],
[03-08-2010],
[04-08-2010],
[05-08-2010]
)
)AS pvt

Insert into @Duration

SELECT
StaffCode,
Name,
[01-08-2010] as [01-08-2010],
[02-08-2010] as [02-08-2010],
[03-08-2010] as [03-08-2010],
[04-08-2010] as [04-08-2010],
[05-08-2010] as [05-08-2010]
FROM
(
SELECT
StaffCode,
Name,
CONVERT(VARCHAR(10),AttendanceDate,105) AttendanceDate,
CONVERT(VARCHAR(5),(MAX(LastPunch) - MIN(FirstPunch)),114) Duration
FROM AttendanceLog
JOIN Staff on Staff.Id = AttendanceLog.StaffId
WHERE AttendanceDate BETWEEN '2010-08-01' And '2010-08-05'
AND Staffcode BETWEEN '1001' AND '1999'
AND name <>'' and workstatus = 'Is Working'
GROUP BY
name,StaffCode,AttendanceDate
)p

PIVOT
(
  MAX(Duration)
  FOR AttendanceDate
  IN
(
[01-08-2010],
[02-08-2010],
[03-08-2010],
[04-08-2010],
[05-08-2010]
)
)AS pvt

Select

I.StaffCode,
I.Name,
CONVERT(VarCHAR(5),[01-08-2010 InTime],114)  [01-08-2010 InTime],
CONVERT(VarCHAR(5),[01-08-2010 OutTime],114) [01-08-2010 OutTime],
CONVERT(VarCHAR(5),[01-08-2010 Duration],114)[01-08-2010 Duration],
CONVERT(VarCHAR(5),[02-08-2010 InTime],114)     [02-08-2010 InTime],
CONVERT(VarCHAR(5),[02-08-2010 OutTime],114) [02-08-2010 OutTime],
CONVERT(VarCHAR(5),[02-08-2010 Duration],114)[02-08-2010 Duration],
CONVERT(VarCHAR(5),[03-08-2010 InTime],114)     [03-08-2010 InTime],
CONVERT(VarCHAR(5),[03-08-2010 OutTime],114) [03-08-2010 OutTime],
CONVERT(VarCHAR(5),[03-08-2010 Duration],114)[03-08-2010 Duration],
CONVERT(VarCHAR(5),[04-08-2010 InTime],114)     [04-08-2010 InTime],
CONVERT(VarCHAR(5),[04-08-2010 OutTime],114) [04-08-2010 OutTime],
CONVERT(VarCHAR(5),[04-08-2010 Duration],114)[04-08-2010 Duration],
CONVERT(VarCHAR(5),[05-08-2010 InTime],114)     [05-08-2010 InTime],
CONVERT(VarCHAR(5),[05-08-2010 OutTime],114) [05-08-2010 OutTime],
CONVERT(VarCHAR(5),[05-08-2010 Duration],114)[05-08-2010 Duration]
From @Intime I
JOIN @Outtime O on I.StaffCode=O.StaffCode
JOIN @Duration D on I.StaffCode=D.Staffcode
order by Staffcode

从这个查询中我得到了在我公司工作的所有员工的实时,停工和持续时间..

我已经为这5天生成了报告..

但现在我想生成n天的报告......

为此我需要做的事。

但我只希望列中的所有日期..

请有人告诉我该怎么做......

请告诉我在查询中需要更改的内容???

我正在使用sql server 2005 .. 还有一件事我想我们需要在循环中使用... 但我很困惑如何使用和在哪里使用.. 因为我是这个领域的新人..

谢谢&amp;此致

1 个答案:

答案 0 :(得分:1)

首先,您不需要三个表来表示“in-”,“out-”时间和持续时间。您只需要将这些数据合并到一个CTE中。

然后,您希望以给定的时间跨度生成varchar格式的天数。

declare @from datetime = '20100901', @to datetime = '20100910'
;with Calendar as
(
  SELECT CAST(@from as datetime) AS [date]
  UNION ALL
  SELECT DATEADD(dd, 1, [date])
  FROM Calendar
  WHERE DATEADD(dd, 1, [date]) <= @to
)
SELECT substring(convert(varchar, [date], 121), 1, 10) Day
FROM Calendar c
OPTION (MAXRECURSION 0);

然后,您将这些日子组装在一个字符串中,并将其插入到您在sql代码中使用这些日期的位置。

然后你将使用exec命令运行这个查询怪物。

旁注。使用sql执行此任务真是太酷了,但我个人会安装Report Builder并根据您拥有的平面数据构建此报告,并且有各种各样的在那里可用的聚合函数,你不能轻易地模仿只使用sql而没有任何辅助函数。

<强>更新 Here is an actual implementation of a dynamic pivot