SSRS Report/SQL 服务器:显示不同的列值,即使在特定日期没有数据

SSRS Report/SQL Server: Display distinct column values even if there is no data on particular dates

我想为 SSRS 报告解决这个问题。我想显示@startdate 和@enddate 之间的所有日期。这部分我已经完成了。接下来,我想为不存在数据的日期显示不同的列值(对于名为“name”的列)。 (即我生成的所有日期的 aa,bb)

我得到了什么:

Date name value
01/01/2021 00:00:00 no data no data
02/01/2021 00:00:00 no data no data
03/01/2021 00:00:00 aa 23
03/01/2021 00:01:00 bb 32
04/01/2021 00:00:00 aa 54
04/01/2021 00:01:00 bb 90

我想要什么:

Date name value
01/01/2021 00:00:00 aa no data
01/01/2021 00:00:00 bb no data
02/01/2021 00:00:00 aa no data
02/01/2021 00:00:00 bb no data
03/01/2021 00:00:00 aa 23
03/01/2021 00:01:00 bb 32
04/01/2021 00:00:00 aa 54
04/01/2021 00:01:00 bb 90

这是我的尝试:

DECLARE @STARTDATE DateTime
DECLARE @ENDDATE DateTime
SET @STARTDATE = '2021-01-01'
SET @ENDDATE = '2021-01-04'
;with dates ([Date]) as (
    Select convert(date,@STARTDate) as [Date] 
    union all 
    Select dateadd(day, 1, [Date])
    from dates
    where [Date] <= @ENDDATE 
)

select 
isnull(n.Created_Date,d.date) Date,
isnull(n.[NAME],'No Data') [NAME],
isnull(n.[VALUE],'No Data') [VALUE]
from dates d
left join Table_A n on convert(varchar, n.Created_Date, 101) = d.Date
where

([Created_Date] between (@StartDate) and (@EndDate) or
[Date] between (@StartDate-1) and (@EndDate) )
option (maxrecursion 32767)


任何帮助将不胜感激。

我将这种模式称为“交叉连接、左连接”。您使用交叉联接创建所有可能值的矩阵,然后使用左联接填充可用值。

所以您需要 table 的所有可能 Names 来交叉连接。例如

DECLARE @STARTDATE DateTime
DECLARE @ENDDATE DateTime
SET @STARTDATE = '2021-01-01'
SET @ENDDATE = '2021-01-04'
;with dates ([Date]) as (
    Select convert(date,@STARTDate) as [Date] 
    union all 
    Select dateadd(day, 1, [Date])
    from dates
    where [Date] <= @ENDDATE 
), names as
(
  select distinct Name 
  from Table_A
)
select 
isnull(n.Created_Date,d.date) Date,
names.Name [NAME],
isnull(n.[VALUE],'No Data') [VALUE]
from dates
cross join names
left join Table_A n 
  on n.Created_Date = dates.Date
  and n.Name = names.Name
where dates.[Date] between dateadd(day,-1,@StartDate) and @EndDate 
option (maxrecursion 32767)