根据您最后的评论,您要查找的查询非常简单。使用 DatePart 函数并找出该对象的订单日期的 WEEK。检查以下查询。除非您的数据库已默认配置,否则 Sunday(int 7) 是 SQL 服务器中设置的第一天。因此,出于此查询目的,您需要将 Monday(int 1) 设置为 Week 的第一天。您可以通过执行检查当前设置 SELECT @@DATEFIRST
如果同一对象在下面的多个星期中有 3 次或更多次位置,则查询将针对该周的每一周返回该对象。如果您只需要对象,那么您可以从结果集中跳过 weekNumber 并执行 Distinct ObjectID。
不要忘记在 END 处将 DATEFIRST 设置重置为其原始值。
DECLARE @Object TABLE
(
objectID INT
)
DECLARE @Order TABLE
(
orderID INT
,objectID INT
,DateSubmitted DATE
)
INSERT INTO @Object( objectID )
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
INSERT INTO @Order ( orderID, objectID, DateSubmitted )
SELECT 1,1,'10/2/2013'
UNION ALL SELECT 2,1,'10/3/2013'
UNION ALL SELECT 3,1,'10/5/2013'
UNION ALL SELECT 4,1,'10/09/2013'
UNION ALL SELECT 5,1,'10/10/2013'
UNION ALL SELECT 6,1,'10/13/2013'
UNION ALL SELECT 4,2,'10/15/2013'
UNION ALL SELECT 5,2,'10/16/2013'
UNION ALL SELECT 6,2,'10/21/2013'
UNION ALL SELECT 7,3,'09/02/2013'
UNION ALL SELECT 8,3,'09/03/2013'
UNION ALL SELECT 9,3,'09/04/2013'
DECLARE @CurrentDateFirst INT=@@DATEFIRST
SET DATEFIRST 1;
SELECT i.objectID,DATEPART(week,DateSubmitted) AS weekNumber
FROM @Object i
JOIN @Order j
ON i.ObjectID = j.ObjectID
WHERE DateSubmitted >= '9/1/2013'
GROUP BY i.objectID,DATEPART(week,DateSubmitted)
HAVING(COUNT(DISTINCT orderID) >= 3)
ORDER BY i.objectID
SET DATEFIRST @CurrentDateFirst