将多个日期字段连接到日历表

时间:2017-10-11 20:58:11

标签: sql-server tsql join calendar qliksense

我有一张桌子,我们称之为记录,其中相关数据按此组织:

| Employee | SubmissionDate | FirstReviewDate | SecondReviewDate |
  Anne       2017-10-02       2017-10-03        2017-10-10          
  Bernard    2017-10-03       2017-10-05        2017-10-10
  Charlene   2017-10-06       2017-10-09        2017-10-09
  Danielle   2017-10-02       2017-10-03        2017-10-09
  Anne       2017-10-03       2017-10-03        2017-10-09

每次员工提交时,都会添加一个带有SubmissionDate的新条目。稍后编辑该记录以包括然后进行第一次和第二次评论。

我还有一个名为Calendar的日历表,其中有一个名为TheDate的字段,其中包含今年每天的日期。我想做的是将SubmissionDate,FirstReviewDate和SecondReview日期与Calendar.TheDate相关联,以便我可以对任何给定日期的所有三个字段进行计数。

我尝试过以下代码:

SELECT Employee,
       Count(SubmissionDate) AS "Submissions",
       Count(FirstReviewDate) AS "First Reviews",
       Count(SecondReviewDate) AS "Second Reviews"
  FROM Records
  LEFT JOIN Calendar ON Records.SubmissionDate = Calendar.TheDate
   AND Records.FirstReviewDate = Calendar.TheDate
   AND Records.SecondReviewDate = Calendar.TheDate
 WHERE TheDate = '2017-10-11'

我的所有变化都试过输出:

| Employee | Submissions | First Reviews | Second Reviews |

我想要的代码如下:

SELECT Employee,
       Count(SubmissionDate),
       Count(FirstReviewDate),
       Count(SecondReviewDate)
  FROM ???
 WHERE TheDate = '2017-10-03'

在哪里???将是正确的加入。使用所需的代码块(包括where子句),所需的输出对于提供的示例数据是这样的:

| Employee | Submissions | First Reviews | Second Reviews |
  Anne       1             2               0
  Bernard    1             0               0
  Charlene   0             0               0
  Danielle   0             1               0

我不知道怎么做这个加入。我已经阅读了很多关于日历表的资源,但这些示例总是包括连接表,每个表都有一个日期标识符。我的问题是我有一个表有三个日期字段,需要与Calendar.TheDate。

相关联

我正在安排这样的数据,以便可以在Qlik Sense中显示数据。我希望用户能够从过滤器面板中选择TheDate并让它在指定的日期聚合所有三个字段(基本上与我的示例代码中的WHERE子句相同)。

2 个答案:

答案 0 :(得分:2)

尝试以下代码

create table #recs (
  Employee varchar(10), 
  SubmissionDate date, 
  FirstReviewDate date, 
  SecondReviewDate date
)

insert into #recs values
('Anne',       '2017-10-02', '2017-10-03', '2017-10-10'),          
('Bernard',    '2017-10-03', '2017-10-05', '2017-10-10'),
('Charlene',   '2017-10-06', '2017-10-09', '2017-10-09'),
('Danielle',   '2017-10-02', '2017-10-03', '2017-10-09'),
('Anne',       '2017-10-03', '2017-10-03', '2017-10-09')

select Employee
    , sum(IIF(SubD.Date_Value = #recs.SubmissionDate, 1, 0)) AS Submissions 
    , sum(IIF(SubD.Date_Value = #recs.FirstReviewDate, 1, 0)) AS [First Reviews]
    , sum(IIF(SubD.Date_Value = #recs.SecondReviewDate, 1, 0)) AS [Second Reviews]
from #recs
    left join DimDate SubD on SubD.Date_Value = '2017-10-03' and
        (SubD.Date_Value = #recs.SubmissionDate
        or SubD.Date_Value = #recs.FirstReviewDate
        or SubD.Date_Value = #recs.SecondReviewDate)
group by Employee

如果您的SQL Server早于2012,请使用CASE而不是IIF

select Employee
    , sum(case when SubD.Date_Value = #recs.SubmissionDate then 1 else 0 end) AS Submissions 
    , sum(case when SubD.Date_Value = #recs.FirstReviewDate then 1 else 0 end) AS [First Reviews]
    , sum(case when SubD.Date_Value = #recs.SecondReviewDate then 1 else 0 end) AS [Second Reviews]
from #recs
    left join DimDate SubD on SubD.Date_Value = '2017-10-03' and
        (SubD.Date_Value = #recs.SubmissionDate
        or SubD.Date_Value = #recs.FirstReviewDate
        or SubD.Date_Value = #recs.SecondReviewDate)
group by Employee

答案 1 :(得分:0)

这应该有用 - 我为样本数据添加了一些简单的DML: -

--Table Setup
IF OBJECT_ID('Records') IS NOT NULL
  DROP TABLE Records;
IF OBJECT_ID('Calendar') IS NOT NULL
  DROP TABLE Calendar;

CREATE TABLE Records (
  Employee varchar(100),
  SubmissionDate datetime,
  FirstReviewDate datetime,
  SecondReviewDate datetime
)


CREATE TABLE Calendar (
  TheDate datetime
)

INSERT Records
  VALUES ('Anne', '2017-10-02', '2017-10-03', '2017-10-10'),
  ('Bernard', '2017-10-03', '2017-10-05', '2017-10-10'),
  ('Charlene', '2017-10-06', '2017-10-09', '2017-10-09'),
  ('Danielle', '2017-10-02', '2017-10-03', '2017-10-09'),
  ('Anne', '2017-10-03', '2017-10-03', '2017-10-09')


INSERT Calendar
  VALUES ('2017-10-03'), ('2017-10-04'), ('2017-10-05')



--Main Query
DECLARE @TheDate datetime = '2017-10-03'

SELECT
  Employee,
  SUM(CASE
    WHEN c1.TheDate IS NOT NULL THEN 1
    ELSE 0
  END),
  SUM(CASE
    WHEN c2.TheDate IS NOT NULL THEN 1
    ELSE 0
  END),
  SUM(CASE
    WHEN c3.TheDate IS NOT NULL THEN 1
    ELSE 0
  END)
FROM Records r
LEFT JOIN Calendar c1
  ON r.SubmissionDate = c1.TheDate
  AND c1.thedate = @TheDate
LEFT JOIN Calendar c2
  ON r.FirstReviewDate = c2.TheDate
  AND c2.thedate = @TheDate
LEFT JOIN Calendar c3
  ON r.SecondReviewDate = c3.TheDate
  AND c3.thedate = @TheDate
GROUP BY Employee