Sql查询以获得当前年份的总订单数量

时间:2013-10-24 05:49:41

标签: sql-server

我有一个表名“Orders”。

我想按当月的周计算总订单金额。

目前,我可以使用以下查询按月获得总订单: -

SELECT 
                    "Month" = month(o.OrderDate)
                     , "Year" = year(o.OrderDate)
                     , Amount = sum(o.Total)
                FROM
                    Orders o
                WHERE
                    o.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                    AND o.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
                GROUP BY
                    month(o.OrderDate)
                  , year(o.OrderDate)
                ORDER BY
                    year(o.OrderDate)
                  , month(o.OrderDate)

但是现在我想像以下几周那样得到这些数据: -

由于我们每年大约有52周,所以数据应该是这样的: -

周年金额   1 2013 200   2 2013 500 .......   52 2013 0

提前致谢。

3 个答案:

答案 0 :(得分:2)

您可以使用DATEPART获取周数。

要获得每周的行数,即使该周没有注册,您也可以使用numbers table并将左连接添加到订单表中。

此查询会为您提供恰好2013周的53年。

select N.Number as [Week],
       2013 as [Year],
       sum(O.Total) as Amount
from dbo.Numbers as N
  left outer join dbo.Orders as O    
    on n.Number = datepart(week, O.OrderDate) and
       O.OrderDate >= '20130101' and
       O.OrderDate < '20140101'
where n.Number between 1 and 53
group by N.Number
order by N.Number;

如果您没有或想要创建一个数字表,您可以使用它来在查询中为您创建一个。

select N.Number as [Week],
       2013 as [Year],
       sum(O.Total) as Amount
from (
     select top (53) row_number() over(order by 1/0) as Number
     from sys.all_objects
     ) as N
  left outer join dbo.Orders as O    
    on n.Number = datepart(week, O.OrderDate) and
       O.OrderDate >= '20130101' and
       O.OrderDate < '20140101'
where n.Number between 1 and 53
group by N.Number
order by N.Number;

答案 1 :(得分:2)

请尝试如下

           SELECT 
                 DATEPART (wk, o.OrderDate)as week,                    
                 year(o.OrderDate) as year
                 , Amount = sum(o.Total)
            FROM
                Orders o
            WHERE
                o.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
                AND o.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
            GROUP BY
                DATEPART(wk, o.OrderDate)
              , year(o.OrderDate)
            ORDER BY
                year(o.OrderDate)
              , DATEPART(wk, o.OrderDate)

答案 2 :(得分:0)

请试试这个 你可以得到一个数字表,其值为1 ... 53,并且左外连接与该表一起,它将导致0周,你没有数据。

       select numbers.value,numbers.[Year],amt from numbers left outer join (
        SELECT 
             DATEPART (wk, o.OrderDate)as week,                    
             2013 as [Year],
             ,sum(o.Total)as amt
        FROM
            Orders o
        WHERE
            o.OrderDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
            AND o.OrderDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
        GROUP BY
            DATEPART(wk, o.OrderDate)
          , year(o.OrderDate)
        ORDER BY
            year(o.OrderDate)
          , DATEPART(wk, o.OrderDate)
      )data on data.week=numbers.value