SSRS DatePart季度全年均有

时间:2017-07-29 21:17:32

标签: sql tsql reporting-services reporting datepart

我有一张包含销售记录的表格。此表的日期时间列为closedealdate。

我使用SSRS来显示所有季度的销售数据。

我表达的意思是:

="Q" & DatePart(DateInterval.Quarter,
                Fields!ActualCloseDate.Value,FirstDayOfWeek.System,
                FirstWeekOfYear.System)

但是我只在我的日期时间栏内得到了四分之一,我如何获得0金额或没有数据的季度?

谢谢!

2 个答案:

答案 0 :(得分:0)

对于这种类型的东西,我通常使用一个日历表,其中包含所有日期的列表以及年,季度,日期,星期几的列。在您的情况下,您可以拥有一个基表,其中包含支持数据所需的所有年/季度组合。然后查询从此表开始并加入您的销售数据。当销售数据为空时,您可以用0替换。

答案 1 :(得分:0)

杰森是完全正确的,但我会为您节省一些时间和link我过去成功使用过的日历表脚本,以便快速制作表格。

对于死链接的缘故,这里是您正在寻找的宿舍的相关部分,尽管我建议您阅读我链接的页面,因为它是一个很好的教程。

DECLARE @StartDate DATE = '20000101', @NumberOfYears INT = 30;

-- prevent set or regional settings from interfering with 
-- interpretation of dates / literals

SET DATEFIRST 7;
SET DATEFORMAT mdy;
SET LANGUAGE US_ENGLISH;

DECLARE @CutoffDate DATE = DATEADD(YEAR, @NumberOfYears, @StartDate);

-- this is just a holding table for intermediate calculations:

CREATE TABLE #dim
(
  [date]       DATE PRIMARY KEY, 
  [day]        AS DATEPART(DAY,      [date]),
  [month]      AS DATEPART(MONTH,    [date]),
  FirstOfMonth AS CONVERT(DATE, DATEADD(MONTH, DATEDIFF(MONTH, 0, [date]), 0)),
  [MonthName]  AS DATENAME(MONTH,    [date]),
  [week]       AS DATEPART(WEEK,     [date]),
  [ISOweek]    AS DATEPART(ISO_WEEK, [date]),
  [DayOfWeek]  AS DATEPART(WEEKDAY,  [date]),
  [quarter]    AS DATEPART(QUARTER,  [date]),
  [year]       AS DATEPART(YEAR,     [date]),
  FirstOfYear  AS CONVERT(DATE, DATEADD(YEAR,  DATEDIFF(YEAR,  0, [date]), 0)),
  Style112     AS CONVERT(CHAR(8),   [date], 112),
  Style101     AS CONVERT(CHAR(10),  [date], 101)
);

-- use the catalog views to generate as many rows as we need

INSERT #dim([date]) 
SELECT d
FROM
(
  SELECT d = DATEADD(DAY, rn - 1, @StartDate)
  FROM 
  (
    SELECT TOP (DATEDIFF(DAY, @StartDate, @CutoffDate)) 
      rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
    FROM sys.all_objects AS s1
    CROSS JOIN sys.all_objects AS s2
    -- on my system this would support > 5 million days
    ORDER BY s1.[object_id]
  ) AS x
) AS y;

请注意,这是上面的临时表,您可能希望将其实现为模式中的某个实际表格。

获得此生成的表后,您可以通过从日历表中选择不同的季度集并从左外部加入常规查询来启动上一个查询。这就是你如何得到那些"失踪"日期。最后,请注意我使用coalesce为您的USD度量显示零数字而不是空值:

SELECT distinct
  'Q' + CAST(quarter as varchar(1)) as Quarter,
  COALESCE(total, 0) as USD
FROM #dim as d
LEFT OUTER JOIN YourTable yt
  ON d.quarter = yt.quarter