SQL Server:联接CALENDAR表和DATA表

时间:2019-03-21 19:04:16

标签: sql-server

有人可以告诉我为什么我不能将这些表加入吗?第一个表是仅带有日期的动态Calendar表,第二个表是具有日期/时间,行为名称(字符串),帐户号(整数)的数据表。我需要加入这些表,以便所有日期都出现,即使该Account#当天没有任何行为。

我当前正在获取如下所示的数据:

ACCT#     Date      Behavior 1        Behavior 2
-------------------------------------------------
123456   2/3/2018      1               3
123456   2/5/2018      1               2
123456   2/10/2018     1               0
123456   2/11/2018     1               0

我希望/需要成为它:

ACCT#       Date      Behavior 1        Behavior 2
----------------------------------------------------
123456      2/1/2018      0               0
123456      2/2/2018      0               0
123456      2/3/2018      1               3
123456      2/4/2018      0               3
123456      2/5/2018      1               0
123456      2/6/2018      0               0
123456      2/7/2018      0               0
123456      2/8/2018      0               0
123456      2/9/2018      0               1
123456      2/10/2018     1               0
123456      2/11/2018     1               0

这是我正在使用的查询

DECLARE @Start DATE, @End DATE, @Account# INT, @behavior varchar(50)
SET @Start = '2/1/2019';
SET @End = '3/20/2019';
SET @FSH = '123456';
SET @Behavior = 'Attack';

;WITH MonthDates AS 
(
    SELECT
        DATEADD(DAY, n1.number * 10 + n0.number, @Start) AS date
    FROM
        (SELECT 1 AS number UNION 
         SELECT 2 UNION 
         SELECT 3 UNION 
         SELECT 4 UNION 
         SELECT 5 UNION 
         SELECT 6 UNION 
         SELECT 7 UNION 
         SELECT 8 UNION 
         SELECT 9 UNION 
         SELECT 0) AS n0
    CROSS JOIN
        (SELECT 1 AS number UNION 
         SELECT 2 UNION 
         SELECT 3 UNION 
         SELECT 4 UNION 
         SELECT 5 UNION 
         SELECT 6 UNION 
         SELECT 7 UNION 
         SELECT 8 UNION 
         SELECT 9 UNION 
         SELECT 0) AS n1
    WHERE
        DATEADD(DAY, n1.number * 10 + n0.number, @Start) BETWEEN @Start AND @End
)
SELECT
    md.date,
    ACCT#,
    COUNT(CASE Behavior WHEN 'Behavior1' THEN 1 ELSE NULL END) AS Behavior1Alias,
    COUNT(CASE Behavior WHEN 'Behavior1' THEN 1 ELSE NULL END) AS Behavior2Alias
FROM
    vw_MaladaptiveDailySummary as vwMals
FULL JOIN 
    MonthDates AS md ON FORMAT(md.Date,'yyyy-MM-dd') = FORMAT(vwMals.Date,'yyyy-MM-dd')    
WHERE
    vwMals.Date >= @start AND vwMals.date <= @end 
    AND Account# = @FSH
GROUP BY
    md.date
ORDER BY
    md.Date

我已将查询更新为以下内容:

declare @Start date, @End date, @FSH int, @behavior varchar(max)
Set @Start='2/1/2018';
Set @End ='12/20/2019';
Set @FSH ='123456';
set @Behavior = 'Attack';

SELECT

    MonthDates.Date
        ,count(case Behavior when 'Behavior1' then 1 else NULL end) as BehaviorsAlias1
    ,count(case Behavior when 'Behavior2' then 1 else null end) as BehaviorAlias2


FROM

    (   

        SELECT

            Date = DateAdd( Day, n1.number * 10 + n0.number, @Start )

        FROM

            (SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) as n0

        CROSS JOIN

            (SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) as n1

        WHERE   DateAdd( Day, n1.number * 10 + n0.number,@Start ) BETWEEN @Start AND @End


    ) AS MonthDates

LEFT JOIN vw_MaladaptiveDailySummary 

    ON vw_MaladaptiveDailySummary.DATE = MonthDates.Date

GROUP BY

    MonthDates.Date

ORDER BY

    MonthDates.Date

这确实产生了我想要的一半;另一半将能够过滤ACCT#和/或行为。我现在得到的结果是当天所有ACCT#的总数。

Date      Behavior 1        Behavior 2
----------------------------------------------------
2/1/2018      5               0
2/2/2018      3               0
2/3/2018      1               3
2/4/2018      0               3
2/5/2018      1               0
2/6/2018      9               5
2/7/2018      6               0
2/8/2018      3               0
2/9/2018      0               8
2/10/2018     1               0
2/11/2018     1               6


1 个答案:

答案 0 :(得分:0)

尽管使用FULL JOIN,但您只会得到vw_MaladaptiveDailySummary中存在的行,因为您在WHERE子句中引用了它:

vwMals.Date >= @start AND vwMals.date <= @end 
AND Account# = @FSH