【问题标题】:Left join and matching dates?离开加入和匹配日期?
【发布时间】:2018-03-22 09:48:20
【问题描述】:

我正在尝试将我的dimdate 表中的日期与我的销售表中的日期相匹配。使用left join 的原因是我们可以看到2017-10-052017-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'

问题是没有人有 '10-05-2017' 所以fname 为空,这会搞砸报告,因为它显示了一个额外的行。

另一个问题是我不想看到lastsale2017-10-10 的任何人,但是一旦我取消注释and LastSale &lt; '2017-10-10',它就会变成inner join 并且我没有看到 2017-10-052017-10-10

理想的输出应该是:

fulldate     fname   sales    SaleDate
2017-10-05   Carl    NULL     NULL
2017-10-06   Carl    NULL     NULL
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-10   Carl    NULL     NULL
2017-10-05   Mary    NULL     NULL
2017-10-06   Mary    1        2017-10-06
2017-10-07   Mary    3        2017-10-07
2017-10-08   Mary    5        2017-10-08
2017-10-09   Mary    NULL     NULL
2017-10-10   Mary    NULL     NULL

我只需要 1 个fname 应该包括从2017-10-052017-10-10 的所有fulldates,因为这样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

我假设我可以创建一个包含所有日期的临时表并用一行更新该表,然后以某种方式使用联合并排除该行,但我知道必须有另一种方法。

感谢您的帮助。

【问题讨论】:

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

标签: sql-server tsql reporting-services sql-server-2008-r2


【解决方案1】:

对于您的 lastDate 问题,将条件从 WHERE 更改为 JOIN 条件,如下所示

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多