简体   繁体   中英

SSRS DatePart Quarter with all year quarters

I have a table with sales records. This table has a datetime column with closedealdate.

I´m using SSRS in order to show all quarters sales data.

My expression in order to sumarize quarters is:

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

But i only get quarters inside my datetime column, how can i get quarters with 0 amount or without data?

thanks!!

For this type of thing I usually use a calendar table that has a list of all the dates along with columns for year, quarter, date, day of week. In your case you could just have a base table with all the year/quarter combination that you need to support your data. Then the query start with this table and join in your sales data. When the sales data is null then you can replace with 0.

Jason is exactly right but I'll save you some time and link the calendar table script that I've successfully used in the past myself for a quick table.

For dead link sake, here is the relevant part to the quarters you are looking from although I'd advise reading the page I linked because it is a nice tutorial.

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;

Notice that this is a temp table above and you probably want to materialize it into a real table somewhere in your schema.

Once you have this generated table, you start your previous query by selecting the distinct set of quarters from the calendar table and left outer joining to your regular query. This is how you get those "missing" dates. Lastly, notice that I used coalesce to show a zero number for your USD measure instead of having nulls:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM