我需要找到所有CID(合同)符合以下条件
我能想到的唯一解决方案是unpivot
,因此日期范围内的每一天都有1,而该范围之外的天数为0。然后我们可以总结1,如果它大于2011年前六个月的天数,则包括该合同。
有更好的方法吗?
以下是如何生成数据
Select CID = 5439, ContractStartTime = '2008-06-26 00:00:00.000', ContractStopTime = '2012-01-29 00:00:00.000'
UNION Select CID = 6216, ContractStartTime = '2008-07-02 00:00:00.000', ContractStopTime = '2010-12-02 00:00:00.000'
UNION Select CID = 7770, ContractStartTime = '2008-12-03 00:00:00.000', ContractStopTime = '2010-12-01 00:00:00.000'
UNION Select CID = 12432, ContractStartTime = '2009-12-17 00:00:00.000', ContractStopTime = '2010-12-15 00:00:00.000'
UNION Select CID = 13986, ContractStartTime = '2009-05-11 00:00:00.000', ContractStopTime = '2011-05-09 00:00:00.000'
UNION Select CID = 14763, ContractStartTime = '2009-12-28 09:00:00.000', ContractStopTime = '2012-03-25 00:00:00.000'
UNION Select CID = 16317, ContractStartTime = '2010-12-08 00:00:00.000', ContractStopTime = '2011-12-06 00:00:00.000'
UNION Select CID = 20979, ContractStartTime = '2010-10-14 00:00:00.000', ContractStopTime = '2012-12-12 00:00:00.000'
UNION Select CID = 17871, ContractStartTime = '2010-01-20 00:00:00.000', ContractStopTime = '2012-01-19 00:00:00.000'
UNION Select CID = 19425, ContractStartTime = '2010-02-23 00:00:00.000', ContractStopTime = '2012-02-21 00:00:00.000'
UNION Select CID = 21756, ContractStartTime = '2010-12-28 00:00:00.000', ContractStopTime = '2012-12-25 00:00:00.000'
UNION Select CID = 20202, ContractStartTime = '2011-02-24 00:00:00.000', ContractStopTime = '2012-05-02 00:00:00.000'
UNION Select CID = 22533, ContractStartTime = '2010-12-27 00:00:00.000', ContractStopTime = '2011-01-26 00:00:00.000'
UNION Select CID = 23310, ContractStartTime = '2010-06-21 00:00:00.000', ContractStopTime = '2011-06-21 00:00:00.000'
UNION Select CID = 24864, ContractStartTime = '2010-07-25 00:00:00.000', ContractStopTime = '2012-12-26 00:00:00.000'
UNION Select CID = 27195, ContractStartTime = '2011-01-11 00:00:00.000', ContractStopTime = '2013-01-09 00:00:00.000'
UNION Select CID = 27972, ContractStartTime = '2011-01-04 00:00:00.000', ContractStopTime = '2013-01-02 00:00:00.000'
UNION Select CID = 29526, ContractStartTime = '2011-01-28 00:00:00.000', ContractStopTime = '2012-01-29 00:00:00.000'
UNION Select CID = 33411, ContractStartTime = '2011-04-28 00:00:00.000', ContractStopTime = '2011-04-27 00:00:00.000'
UNION Select CID = 34965, ContractStartTime = '2011-06-14 00:00:00.000', ContractStopTime = '2012-06-11 00:00:00.000'
UNION Select CID = 35742, ContractStartTime = '2011-06-07 00:00:00.000', ContractStopTime = '2011-07-06 00:00:00.000'
UNION Select CID = 39627, ContractStartTime = '2012-01-12 00:00:00.000', ContractStopTime = '2013-01-10 00:00:00.000'
UNION Select CID = 40404, ContractStartTime = '2011-11-30 00:00:00.000', ContractStopTime = '2012-07-29 00:00:00.000'
UNION Select CID = 48951, ContractStartTime = '2012-06-21 00:00:00.000', ContractStopTime = '2012-07-22 00:00:00.000'
Order by 3
答案 0 :(得分:3)
CID在1月到6月的窗口中有90个活动日......
DECLARE
@windowStart DATETIME,
@windowEnd DATETIME
SELECT
@windowStart = '20110101',
@windowEnd = '20110701'
SELECT
*
FROM
yourTable
WHERE
contractStopTime >= @windowStart
AND contractStartTime < @windowEnd
AND DATEDIFF(
DAY,
CASE WHEN ContractStartTime >= @windowStart THEN contractStartTime ELSE @windowStart END,
CASE WHEN ContractStopTime < @windowEnd THEN contractStopTime ELSE @windowEnd END
) >= 90