我试图根据生效日期制定一个返回员工语言环境的查询。
declare @locale table
(
[locale_id] [int] IDENTITY(1,1),
[locale_name] [varchar](50)
)
declare @employees table
(
[employee_id] [int] IDENTITY(1,1),
[employee_number] [varchar](20),
[employee_name] [varchar](50),
[employee_dept] [varchar](50),
[report_to] [int]
)
declare @employee_locale table
(
[employee_locale_id] [int] IDENTITY(1,1),
[locale_id] [int],
[employee_id] [int],
[effective_date] [datetime]
)
insert into @locale values ('CST')
insert into @locale values ('PHT')
insert into @employees values ('812621','Davolio Nancy','UCC GTT','0')
insert into @employees values ('811363','Fuller Andrew','UCC GTT','1')
insert into @employees values ('811397','Leverling Janet','UCC GTT','1')
insert into @employees values ('814495','Peacock Margaret','UCC GTT','2')
insert into @employee_locale values ('1','1','May 14 2012 12:00AM')
insert into @employee_locale values ('1','2','Apr 12 2010 12:00AM')
insert into @employee_locale values ('1','3','Apr 26 2010 12:00AM')
insert into @employee_locale values ('1','4','Sep 9 2013 12:00AM')
insert into @employee_locale values ('2','1','June 14 2012 12:00AM')
insert into @employee_locale values ('1','1','July 14 2012 12:00AM')
insert into @employee_locale values ('2','1','August 14 2012 12:00AM')
insert into @employee_locale values ('1','1','September 14 2012 12:00AM')
--select * from @locale
--select * from @employees
--select * from @employee_locale
declare @current_datetime datetime
set @current_datetime = '07/01/2012'
SELECT employees.employee_id, employees.employee_number,
employees.employee_name, employees.employee_dept, employees.report_to,
employee_locale.effective_date, locale.locale_name
FROM @employees employees
INNER JOIN @employee_locale employee_locale ON employees.employee_id = employee_locale.employee_id
INNER JOIN @locale locale ON employee_locale.locale_id = locale.locale_id
WHERE employees.employee_number = '812621'
SELECT employees.employee_id, employees.employee_number,
employees.employee_name, employees.employee_dept, employees.report_to,
employee_locale.effective_date, locale.locale_name
FROM @employees employees
INNER JOIN @employee_locale employee_locale ON employees.employee_id = employee_locale.employee_id
INNER JOIN @locale locale ON employee_locale.locale_id = locale.locale_id
WHERE employee_locale.effective_date >= @current_datetime
and employee_locale.effective_date <= @current_datetime
and employees.employee_number = '812621'
例如。如果今天是&#39; 07/01/20&#39;,我想在特定时期获得Davolio Nancy的地点。在这种情况下,它将返回第二条记录。
请帮忙。感谢。
答案 0 :(得分:0)
CROSS APPLY
和ORDER BY [date] DESC
将找到提供@current_datetime
之前的最后日期:
SELECT
e.employee_id, e.employee_number,
e.employee_name, e.employee_dept, e.report_to,
el.effective_date, loc.locale_name
FROM @employees e
CROSS APPLY
(
SELECT TOP 1 el.effective_date, el.locale_id
FROM @employee_locale el
WHERE el.employee_id = e.employee_id
AND el.effective_date <= @date
ORDER BY el.effective_date desc
) el
INNER JOIN @locale loc ON loc.locale_id = el.locale_id
where e.employee_number = '812621'
如果OUTER APPLY
区域设置允许,请使用LEFT JOIN
和NULL