如何在表上进行双重比较

时间:2012-09-18 20:41:39

标签: sql tsql

我需要将一列LeadDate(数据类型为datetime)与另一个表(名为EmployeeWorkTime)中保存的值进行比较。 LeadDate列的格式如下:“2006-08-08 11:50:51。000”。

employeeworktime表是这样的:

SyStaffId       Associate Name           Monday    Tuesday  Wednesday Thursday   Friday    Saturday        Sunday
34128           Natasha Dawson            0919      0918      0920      0918      0918      WKEND           WKEND    
34169           Elaine Bueno              0918      1019      0918      1019      0918      WKEND           WKEND    
34385           Aida Rodriguez            0918      1019      0918      1019      0918      WKEND           WKEND    
34419           Sy Lim                    1019      1019      1019      1019      0918      WKEND           WKEND     

我创建了一个视图,并包含了比较所需的所有字段。新视图与附件中一样。但是,当我想将提前期与时间列中的值进行比较时。我遇到了一些困难。由于提前日不同,它们位于DayofWeekName列中,我想将提前期与仅一个与提前日相同的列进行比较。我怎么能这样做?

例如,对于第一个引导,dayofweekname是Thursay,我想将引导时间(即leadhour列)的前导时间复制到星期四列,我怎样才能编写代码来实现它。

非常感谢。

用于创建新视图的命令:

Create View dbo.cstAdmissionDailyReportLeadCount_jy
AS
SELECT     dbo.syStudent.AmRepID, dbo.syStudent.LeadDate, DATEPART(dw, syStudent.LeadDate) AS DayofWeekNumber, DATEPART(hh, syStudent.LeadDate)
                      AS LeadHour, dbo.EmployeeWorkTime.[Associate Name], DATENAME(dw, syStudent.LeadDate) AS DayofWeekName, EmployeeWorkTime.Monday,
                      EmployeeWorkTime.Tuesday, EmployeeWorkTime.Wednesday, EmployeeWorkTime.Thursday, EmployeeWorkTime.Friday,
                      EmployeeWorkTime.Saturday, EmployeeWorkTime.Sunday, CASE WHEN DATEPART(hh, syStudent.LeadDate)
                      < 18 THEN 1 ELSE 0 END AS CountAsEffectiveLead
FROM         dbo.syStudent INNER JOIN
                      dbo.EmployeeWorkTime ON dbo.syStudent.AmRepID = dbo.EmployeeWorkTime.SyStaffId 

1 个答案:

答案 0 :(得分:0)

将这个添加到您的select子句有帮助吗?

case when DATENAME(dw, syStudent.LeadDate) = "Monday" then EmployeeWorkTime.Monday
when DATENAME(dw, syStudent.LeadDate) = "Tuesday" then EmployeeWorkTime.Tuesday
when DATENAME(dw, syStudent.LeadDate) = "Wednesday" then EmployeeWorkTime.Wednesday
when DATENAME(dw, syStudent.LeadDate) = "Thursday" then EmployeeWorkTime.Thursday
when DATENAME(dw, syStudent.LeadDate) = "Friday" then EmployeeWorkTime.Friday
when DATENAME(dw, syStudent.LeadDate) = "Saturday" then EmployeeWorkTime.Saturday
else EmployeeWorkTime.Sunday end as ewt_day