1

我正在尝试创建一个查询,该查询仅返回包含在一周的时间范围内具有三个或更多订单的对象的行,并且仅在 2013 年 9 月 1 日之后提交的订单。

SELECT OrderID, DateSubmitted, ObjectID = i.ObjectID
FROM dbo.Object i
JOIN dbo.Order j
ON i.ObjectID = j.ObjectID
WHERE DateSubmitted >= '9/1/2013'

我只是不知道如何将结果缩小到一周内有三个或更多订单的对象。我已经尝试了很多GROUP BYHAVING没有运气的子句。任何帮助将不胜感激。

4

4 回答 4

1

尝试:

SELECT ObjectID 
FROM dbo.Object i
JOIN dbo.Order j ON  J.ObjectID = i.ObjectID
WHERE DateSubmitted >= '9/1/2013'
GROUP BY ObjectID 
HAVING COUNT(1) >=3
于 2013-10-22T20:47:33.487 回答
1

不确定,但我需要更多关于表格的信息 最好的猜测是在 sql 中

SELECT count(OrderID), i.ObjectID
FROM dbo.Object i
JOIN dbo.Order j
ON i.ObjectID = j.ObjectID
group by i.ObjectID
having  DateSubmitted >= '9/1/2013' and count(OrderID)>2
于 2013-10-22T20:49:33.813 回答
0

令人费解,但我认为这会让你得到你想要的......

With    WeekSets As
(
        Select  i.ObjectID,
                j.DateSubmitted, 
                j2.OrderID
        From    dbo.Object i
        JOIN    dbo.Order j
                ON  i.ObjectID = j.ObjectID
        JOIN    dbo.Order j2
                On  j.DateSubmitted <= j2.DateSubmitted
                And j2.DateSubmitted < DateAdd(Day,7,j.DateSubmitted)
                And j.ObjectID = j2.ObjectID
        WHERE   j.DateSubmitted >= '9/1/2013'
),      GroupsOfThree As
(
        Select  ObjectID,
                DateSubmitted
        From    WeekSets
        Group   By  ObjectID,
                    DateSubmitted
        Having  Count(OrderID) >= 3
)
Select  Distinct j.OrderID, 
        j.DateSubmitted, 
        w.ObjectID
From    GroupsOfThree g
Join    WeekSets w
        On  g.ObjectID = w.ObjectID
        And g.DateSubmitted = w.DateSubmitted
Join    dbo.Order j
        On  w.ObjectID = j.ObjectID
        And w.OrderID = j.OrderID
于 2013-10-22T22:02:37.273 回答
0

根据您最后的评论,您要查找的查询非常简单。使用 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

在此处输入图像描述

于 2013-10-25T21:03:46.690 回答