T SQL Datepart和Count - 显示零计数的周数

时间:2016-03-06 16:21:04

标签: sql sql-server tsql count datepart

我有以下SQL语句:

declare @dateFrom datetime = '2015-01-01'; 
declare @dateTo datetime = '2015-12-31'; 

select 
    DATEPART(WEEK, OrderDate) Week, Count(*) Number
from 
    table 
where 
    OrderDate between @dateFrom and @dateTo 
group by 
    DATEPART(WEEK, OrderDate) 
order by 
    Week

它返回每周的订单数量,但如果根本没有订单,则省略相应的一周。

如何更改语句,以便包含0个订单的周数?

1 个答案:

答案 0 :(得分:2)

Gofr1走在正确的轨道上,但查询存在问题。

1 - 您不希望使用开头和结尾的datediff()作为停止条件。它可以工作一整年但不适用于部分范围。

2 - 我会在密钥上添加年份,因为这样可以让您处理跨年案例。

3 - 您需要在使用Year Week Common Table Expression之前汇总销售。否则,您只需使用WHERE子句再次抛出空值(订单日期)。

请记住,逻辑上应用连接然后是where子句。

下面的代码使用Adventure Works 2012 DW数据库并获得正确答案。

  • 对某些数字使用计数表。
  • 生成每周日期并计算给定范围的年/周密钥。
  • 从事实表中提取给定范围的销售额。
  • 左键将键加入销售,并将零总计变为零。

代码:

-- Declare start and end date
DECLARE @dte_From datetime = '2005-07-01'; 
DECLARE @dte_To datetime = '2007-12-31'; 

-- About 200K numbers
WITH cte_Tally (n) as
(
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM sys.all_views a 
    CROSS JOIN sys.all_views b
),

-- Create year/week key
cte_YearWeekKey (MyKey) as
(
SELECT 
    year(dateadd(week, t.n, @dte_from)) * 1000 + 
    datepart(week, dateadd(week, t.n, @dte_from)) as MyKey
FROM 
    cte_Tally as t
WHERE 
    dateadd(week, t.n, @dte_from) < @dte_To
),

-- Must roll up here
cte_Sales (MyKey, MyTotal) as
(
SELECT 
    YEAR(F.OrderDate) * 1000 +
    DATEPART(WEEK, F.OrderDate) as MyKey, 
    COUNT(*) as MyTotal
FROM
    [AdventureWorksDW2012].[dbo].[FactResellerSales] F
WHERE 
    F.OrderDate between @dte_From and @dte_To 
GROUP BY 
    YEAR(F.OrderDate) * 1000 +
    DATEPART(WEEK, F.OrderDate)
)

-- Join the results
SELECT 
    K.MyKey, ISNULL(S.MyTotal, 0) as Total
FROM
    cte_YearWeekKey as K
LEFT JOIN 
    cte_Sales as S
ON 
    k.MyKey = S.MyKey

enter image description here