2017-03-06 18 views
2

我有一个大的类型2维度表,并导致选择查询中的性能问题...我想根据报表开始和结束日期来限制维度...但我努力得到了正确的查询......这里是什么我寻找...从类型2基于维度的报表开始和结束日期

declare @DimCustomers table (CKey int, ID nvarchar(20), Customer nvarchar(50), StartDate datetime, EndDate datetime) 

insert into @DimCustomers values 
(100, 'C1', 'Customer1', '2010-01-01', '2010-12-31'), 
(101, 'C1', 'xCustomer1', '2011-01-01', '2011-12-31'), 
(102, 'C1', 'xxCustomer1', '2012-01-01', '2012-12-31'), 
(103, 'C1', 'xxxCustomer1', '2013-01-01', NULL) 

declare @ReportStartDate datetime = '2010-05-01', @ReportEndDate datetime = '2011-03-01' 

select 
* from @DimCustomers 

人们期望的例子是,当有人运行2010-02-01之间”的报告'和'2011-02-01',我得到了Ckey 100和101的输出。

对于'2011-02-01'和当前日期之间的报告 - > 101,102和103

为“2015年2月1日”和当前日期间的报告 - > 103

我希望解释什么,我期待的......应该怎么我的条款看起来像WHERE上@DimCustomers?

感谢

注:我不想与事实表开始加入...

回答

0

您在结束日期NULL值。你将不得不处理它。 例如:

SELECT * 
FROM @DimCustomers 
WHERE StartDate >= @ReportStartDate 
     AND ISNULL(EndDate,GETDATE()) <= @ReportEndDate 
0
select 
    * 
from 
    @DimCustomers 
where 
    --gets where @ReportStartDate falls in range 
    (@ReportStartDate >= StartDate and @ReportStartDate <= isnull(EndDate,getdate())) 
    or 
    --gets where @ReportEndDate falls in range 
    (@ReportEndDate <= isnull(EndDate,getdate()) and @ReportEndDate >= StartDate) 
    or 
    --gets where the range in data falls inside parameter range 
    (@ReportStartDate < StartDate and @ReportEndDate > isnull(EndDate,getdate())) 
+1

@Aquillo所以它会是多余的,将评估第一和第二种情况属实。虽然公平的想法,当然。 – scsimon

相关问题