Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 左连接和匹配日期?_Sql Server_Tsql_Reporting Services_Sql Server 2008 R2 - Fatal编程技术网

Sql server 左连接和匹配日期?

Sql server 左连接和匹配日期?,sql-server,tsql,reporting-services,sql-server-2008-r2,Sql Server,Tsql,Reporting Services,Sql Server 2008 R2,我正在尝试将dimdate表中的日期与sales表中的日期进行匹配。使用左连接的原因是,我们可以看到2017-10-05到2017-10-10之间的所有日期,SSRS矩阵为每个日期显示一列,即使它没有任何数据 这是一个示例查询: declare @table table ( fname varchar(20), Sales int, SaleDate date, LastSale date ) insert into @table select 'John',

我正在尝试将dimdate表中的日期与sales表中的日期进行匹配。使用左连接的原因是,我们可以看到2017-10-05到2017-10-10之间的所有日期,SSRS矩阵为每个日期显示一列,即使它没有任何数据

这是一个示例查询:

declare @table table
(
    fname varchar(20),
    Sales int,
    SaleDate date,
    LastSale date
)

insert into @table
select 'John', 1, '2017/10/08', '2017/10/10'
union
select 'John', 3, '2017/10/09', '2017/10/10'
union
select 'John', 5, '2017/10/10', '2017/10/10'
union
select 'Mary', 1, '2017/10/06', '2017/10/08'
union
select 'Mary', 3, '2017/10/07', '2017/10/08'
union
select 'Mary', 5, '2017/10/08', '2017/10/08'
union
select 'Carl', 10, '2017/10/07', '2017/10/09'
union
select 'Carl', 13, '2017/10/08', '2017/10/09'
union
select 'Carl', 32, '2017/10/09', '2017/10/09'


select dim.fulldatealternatekey as 'fulldate', fname, sales, t.SaleDate
from dimdate dim left join @table t
on dim.fulldatealternatekey = t.SaleDate
where FullDateAlternateKey between '2017-10-05' and '2017-10-10'
and LastSale < '2017-10-10'
我只需要1个fname应该包括2017-10-05到2017-10-10的所有完整日期,因为这样SSRS就可以显示所有日期,所以类似这样的东西也很好:

fulldate     fname   sales   SaleDate
2017-10-05    Carl   NULL    NULL        -- It can be Carl or Mary, doesn't matter
2017-10-07    Carl   10      2017-10-07
2017-10-08    Carl   13      2017-10-08
2017-10-09    Carl   32      2017-10-09
2017-10-06    Mary   12      2017-10-06
2017-10-07    Mary   32      2017-10-07
2017-10-08    Mary   52      2017-10-08
2017-10-10    Mary   NULL    NULL        -- It can be Carl or Mary, doesn't matter
我假设我可以创建一个包含所有日期的临时表,并用一行更新该表,然后以某种方式使用联合并排除该行,但我知道必须有另一种方法


感谢您的帮助。

有关您的最新问题,请更改“从何处加入”条件,如下所示

select dim.fulldatealternatekey as 'fulldate', fname, sales, t.SaleDate
from dimdate dim left join @table t
on dim.fulldatealternatekey = t.SaleDate and LastSale < '2017-10-10'
where FullDateAlternateKey between '2017-10-05' and '2017-10-10'
对于最终输出,查询应该是

select 
  dim.fulldatealternatekey as 'fulldate', 
  coalesce(fname, (SELECT TOP 1 fname from @table)) as fname,
  sales, 
  t.SaleDate
from dimdate dim left join @table t
   on dim.fulldatealternatekey = t.SaleDate and LastSale < '2017-10-10'
where FullDateAlternateKey between '2017-10-05' and '2017-10-10'
order by dim.fulldatealternatekey asc

对于lastDate问题,请更改“从何处加入”条件,如下所示

select dim.fulldatealternatekey as 'fulldate', fname, sales, t.SaleDate
from dimdate dim left join @table t
on dim.fulldatealternatekey = t.SaleDate and LastSale < '2017-10-10'
where FullDateAlternateKey between '2017-10-05' and '2017-10-10'
对于最终输出,查询应该是

select 
  dim.fulldatealternatekey as 'fulldate', 
  coalesce(fname, (SELECT TOP 1 fname from @table)) as fname,
  sales, 
  t.SaleDate
from dimdate dim left join @table t
   on dim.fulldatealternatekey = t.SaleDate and LastSale < '2017-10-10'
where FullDateAlternateKey between '2017-10-05' and '2017-10-10'
order by dim.fulldatealternatekey asc

请提供dimdate表的结构和简单数据请提供dimdate表的结构和简单数据