计算存在和不存在的天数

时间:2010-12-01 08:22:02

标签: visual-studio-2008

我已经存储了员工的出勤情况,如下所示......

Columns  :  EmployeeCode Day1   Day2   Day3 ................ Day31

Values     :  EC001      P       P      A                       A

为了我的目的,我需要得到现在的日数和&每位员工缺席日计数。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您好,只需复制并粘贴此代码即可正常使用。只需检查是否更改了表名,以确定数据库中的内容。如果这有帮助,请不要忘记投票。谢谢!

declare @tableName varchar(100)
declare @tableFieldId varchar(100)

set @tableName = 'tbl_Attendance'
set @tableFieldId = 'EmployeeCode'




------------- no need to edit from here -----------------------

declare @sql nvarchar(Max)
declare @Present nvarchar(Max)
declare @Absent nvarchar(Max)


Set @sql = 'Select ' + @tableFieldId + ''
set @Present = ''
set @Absent = ''
select 
    @Present = @Present + ' case when ' + syscolumns.[name] + ' = ''P'' then 1 else 0 end + ' + char(10) + char(13),
    @Absent = @Absent + ' case when ' + syscolumns.[name] + ' = ''A'' then 1 else 0 end + ' + char(10) + char(13)
from sysobjects inner join syscolumns on sysobjects.id = syscolumns.id
where sysobjects.[name] = @tableName 

-- remove last +
set @Present = lefT( @Present, len(@Present) - 4) + char(10) + char(13)
set @Absent= lefT( @Absent, len(@Absent) - 4) + char(10) + char(13)

set @sql = @sql + ', Present =' +  @Present + ', Absent =' +  @Absent + 
'from ' + @tableName +  char(10) + char(13) 


exec sp_executesql @sql