协助创建SQL查询

时间:2013-10-22 20:36:00

标签: sql sql-server

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

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

根据您上次的评论,您查找的查询非常简单。使用DatePart函数找出该对象的订单日期的周数。检查以下查询。除非你的数据库已经默认配置,否则星期日(int 7)是SQL服务器中设置的第一天。因此,对于此查询目的,您需要将Monday(int 1)设置为Week的第一天。 您可以通过执行SELECT @@DATEFIRST

来检查当前设置

如果相同的对象在下面的多周内有3次或更多次的位置,则查询将返回该周的每个对象。如果您只需要对象,那么您可以从结果集中跳过weekNumber并执行Distinct ObjectID。

不要忘记将DATEFIRST设置重置为END的原始值。

    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

enter image description here