在列名中仅保留日期

时间:2018-04-11 15:37:48

标签: sql sql-server vb.net crystal-reports crystal-reports-2010

我已通过在线帮助创建了一个存储过程,以生成每月出勤报告

    USE [Attendace]
GO
/****** Object:  StoredProcedure [dbo].[PerDayAttendance]    Script Date: 04/11/2018 20:16:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PerDayAttendance] 
@STARTDATE DATE,  
@ENDDATE DATE  
AS BEGIN
WITH DATERANGE AS
(  
   SELECT DT =DATEADD(DD,0, @STARTDATE)  
   WHERE DATEADD(DD, 1, @STARTDATE) <= @ENDDATE  
   UNION ALL  
   SELECT DATEADD(DD, 1, DT)  
   FROM DATERANGE  
   WHERE DATEADD(DD, 1, DT) <= @ENDDATE  
)  SELECT * INTO cte_DATES  
FROM DATERANGE   
DECLARE @COLUMN varchar(max)
SELECT @COLUMN=ISNULL(@COLUMN+',','')+ '['+ CAST(CONVERT(DATE , T.DT) AS varchar) + ']'  FROM cte_DATES T
DECLARE @Columns2 varchar(max)  
SET @Columns2 = SUBSTRING((SELECT DISTINCT ',ISNULL(['+ CAST(CONVERT(DATE , DT) 
as varchar )+'],'''') AS ['+ CAST(CONVERT(DATE , DT) as varchar )+']' 
FROM cte_DATES GROUP BY dt FOR XML PATH('')),2,8000)  
DECLARE @QUERY varchar(MAX)  
SET @QUERY = 'SELECT P.EID, ENAME, ' + @Columns2 +', Wdays, Holidays, K.Present, (Wdays-(Holidays + K.Present))as Absent, (cast(((s.Salary/Wdays)*(k.present+Holidays)) as numeric(36,0))) as Salary FROM   
(  
SELECT  A.EID, A.ENAME ,  B.DT AS DATE, (Case when cast(A.WorkTime as time) > 
''00:00:00'' then ''P'' else ''Abs'' end) as worktime FROM TblAttendnce  A 
RIGHT OUTER JOIN cte_DATES B 
ON A.EDATE=B.DT
) X  
PIVOT   
(  
MIN([Worktime])  
FOR [DATE] IN (' + @COLUMN + ')  
) P 
Cross apply (select Wdays, Holidays from dbo.fn_Fn1(''' + CAST(@STARTDATE AS VARCHAR(50)) + ''','''+ CAST(@ENDDATE as Varchar(50))+'''))H 
Right Outer Join (select eid, COUNT(present) as present from Attendace.dbo.vwPayroll 
where Edate between ''' + CAST(@STARTDATE AS VARCHAR(50)) + '''and'''+ CAST(@ENDDATE as Varchar(50))+'''
group by eid) as K on K.eid=p.EID
Right Outer Join ((select eid, salary from dbo.employeeMast))as s on S.eid=p.eid
WHERE ISNULL(ENAME,'''')<>''''  
'
Exec (@QUERY)  
DROP TABLE cte_DATES   
END

现在的结果是这样的

Please Check the Image

我需要从此过程准备一个水晶报告,但由于动态列标题名称,我无法执行此操作。

我的查询是如何将列名设为01,02,03而不是2018-04-01,2018-04-02,2018-04-03

我的意思是我想将列名重命名为dd / mm / yyyy的DD

通过这种方式,我可以在水晶报告中反映出来。

1 个答案:

答案 0 :(得分:0)

当我开发Crystal Report或培训客户时;我教他们要么重命名为SQL或存储过程中的数据字段,例如3或插入重复详细信息行,并使用OLD行进行开发。使新线成为评论线。