我有一个包含团队名称的表格,团队创建的日期以及团队解散的日期:
TeamName TeamStartDate TeamEndDate
Business Analysis 2012-12-31 00:00:00.000 2013-06-30 00:00:00.000
Business Systems 2012-06-18 00:00:00.000 2015-01-31 00:00:00.000
Business Systems and Portfolio 2012-12-31 00:00:00.000 2099-12-31 00:00:00.000
Data Administration/eCommerce/Testing 2012-10-29 00:00:00.000 2013-10-10 00:00:00.000
Data Solutions 2013-10-11 00:00:00.000 2099-12-31 00:00:00.000
Data Solutions-Reporting 2012-12-31 00:00:00.000 2013-10-10 00:00:00.000
某些团队被重命名(如业务系统和投资组合的情况),我希望能够为特定日期选择正确的团队。例如,如果我的报告要在2015年8月1日运行,我想看“业务系统和产品组合”,但如果报告要在2014年12月12日运行,我想看“业务系统”。我一直试图弄清楚如何做到这一点,但不能完全达到目的。有人可以帮忙吗?
答案 0 :(得分:0)
SELECT TeamName, TeamStartDate, TeamEndDate
FROM [TableName]
WHERE '2015-08-01 00:00:00' BETWEEN TeamStartDate AND TeamEndDate
01/08/2015是您要搜索的日期
答案 1 :(得分:0)
您的问题的答案是:无法确定;信息不足。
您可以将“业务分析”重命名为“业务系统”...或者将其重命名为“Pie”,我们所知道的。您需要一个标识符,告诉两个名称实际上属于同一个团队。
如果数据包含团队的唯一标识符,则以下示例将提供给定团队的最早名称:
DECLARE @teams TABLE(TeamID INT, TeamName varchar(100), TeamStartDate date, TeamEndDate date)
INSERT INTO @teams VALUES(1, 'Business Analysis ','2012-12-31 00:00:00.000','2013-06-30 00:00:00.000')
INSERT INTO @teams VALUES(2, 'Business Systems ','2012-06-18 00:00:00.000','2015-01-31 00:00:00.000')
INSERT INTO @teams VALUES(2, 'Business Systems and Portfolio ','2012-12-31 00:00:00.000','2099-12-31 00:00:00.000')
INSERT INTO @teams VALUES(3,'Data Administration/eCommerce/Testing','2012-10-29 00:00:00.000','2013-10-10 00:00:00.000')
INSERT INTO @teams VALUES(4,'Data Solutions ','2013-10-11 00:00:00.000','2099-12-31 00:00:00.000')
INSERT INTO @teams VALUES(5, 'Data Solutions-Reporting ','2012-12-31 00:00:00.000','2013-10-10 00:00:00.000')
SELECT *
FROM ( SELECT TeamName, TeamStartDate, TeamEndDate, ROW_NUMBER() OVER (PARTITION BY TeamID ORDER by TeamID) num
FROM @teams
WHERE '2014-12-12 00:00:00' BETWEEN TeamStartDate AND TeamEndDate) a
WHERE a.num = 1
结果:
TeamName TeamStartDate TeamEndDate num
------------------- ------------- ----------- ---
Business Systems 2012-06-18 2015-01-31 1
Data Solutions 2013-10-11 2099-12-31 1