两个表之间的SQL日期范围比较

时间:2017-05-26 15:19:15

标签: sql-server join

我有两张桌子。一个是具有日期范围的日历表。另一张桌子有日期。我想在与Calendar表比较后选择没有数据的日期。例如:

    Declare @Calendar table
(
[Month] varchar(5),
CalDate varchar(10)
)
INSERT into @Calendar values ('May', '05/14/2017')
INSERT into @Calendar values ('May', '05/15/2017')
INSERT into @Calendar values ('May', '05/16/2017')
INSERT into @Calendar values ('May', '05/17/2017')
INSERT into @Calendar values ('May', '05/18/2017')
INSERT into @Calendar values ('May', '05/19/2017')
INSERT into @Calendar values ('May', '05/20/2017')

select * from @Calendar
Declare @ReportTable table
(
ReportID int,
ReportName varchar(10),
ReportData int,
ReportDate varchar(10)
)
INSERT into @ReportTable values ('3456', 'MyReport01', 4563, '05/17/2017')
INSERT into @ReportTable values ('3456', 'MyReport01', 6553, '05/18/2017')
INSERT into @ReportTable values ('3456', 'MyReport01', 432, '05/19/2017')
INSERT into @ReportTable values ('3456', 'MyReport01', 123, '05/20/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 9088, '05/14/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 4545, '05/15/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 144, '05/16/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 676, '05/17/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 4454, '05/18/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 2324, '05/19/2017')
INSERT into @ReportTable values ('7888', 'MyReport02', 68467, '05/20/2017')
select * from @ReportTable  

如果任何ReportID在@ReportTable中缺少日期,我想返回0。正如您在ReportID 3456中看到的那样,缺少日期。请指导如何执行此操作。

1 个答案:

答案 0 :(得分:0)

这里的关键是到达每个报告的一行 - 日期组合。你可以这样做:

SELECT ReportTypes.ReportID, ReportTypes.ReportName, c.CalDate
FROM @Calendar c
    CROSS JOIN (SELECT DISTINCT ReportID, ReportName 
                FROM @ReportTable) as ReportTypes
ORDER BY ReportTypes.ReportID, ReportTypes.ReportName, c.CalDate

结果:

ReportID    ReportName  CalDate
3456        MyReport01  05/14/2017
3456        MyReport01  05/15/2017
3456        MyReport01  05/16/2017
3456        MyReport01  05/17/2017
3456        MyReport01  05/18/2017
3456        MyReport01  05/19/2017
3456        MyReport01  05/20/2017
7888        MyReport02  05/14/2017
7888        MyReport02  05/15/2017
7888        MyReport02  05/16/2017
7888        MyReport02  05/17/2017
7888        MyReport02  05/18/2017
7888        MyReport02  05/19/2017
7888        MyReport02  05/20/2017

然后,您只需添加数据:

SELECT ReportTypes.ReportID, ReportTypes.ReportName, 
    ISNULL(r2.ReportData, 0) AS ReportData, C.CalDate
FROM @Calendar c
    INNER JOIN (SELECT DISTINCT ReportID, ReportName 
                FROM @ReportTable) as ReportTypes
         ON 1 = 1
    LEFT JOIN @ReportTable r2
        ON ReportTypes.ReportID = r2.ReportID 
            AND c.CalDate = r2.ReportDate
ORDER BY ReportTypes.ReportID, ReportTypes.ReportName, c.CalDate AS ReportDate

您必须使用CalDate,因为ReportDate在某些时候将为null。

这给出了这些结果:

ReportID    ReportName  ReportData  ReportDate
3456        MyReport01  0           05/14/2017
3456        MyReport01  0           05/15/2017
3456        MyReport01  0           05/16/2017
3456        MyReport01  4563        05/17/2017
3456        MyReport01  6553        05/18/2017
3456        MyReport01  432         05/19/2017
3456        MyReport01  123         05/20/2017
7888        MyReport02  9088        05/14/2017
7888        MyReport02  4545        05/15/2017
7888        MyReport02  144         05/16/2017
7888        MyReport02  676         05/17/2017
7888        MyReport02  4454        05/18/2017
7888        MyReport02  2324        05/19/2017
7888        MyReport02  68467       05/20/2017