如何在SQL中按日期范围连接多个表?

时间:2014-02-26 04:43:09

标签: sql sql-server

我对SQL比较陌生。而且我一直在努力构建一个非常简单的查询,返回单行。

我正在尝试从多个不同的表中选择多个列值计数,每个计数在相同的日期范围内被拉出。

我的数据库中的表类似于:

|   CreationDate      | LastName    | EventType    |
|:--------------------|------------:|:------------:| ...
| 2013-01-02 18:00:21 | Doe         |     2        |  
| 2013-01-07 18:00:24 | Blanks      |     2        | ...
| 2013-01-09 17:00:21 | Puccini     |     1        |

所有表都有类似的CreationDate列。

现在我的查询是一个单一的JOIN,如下所示(似乎有效)。我正在尝试添加一个或多个JOIN,以便我可以将每个表的多个计数返回到单行结果。我目前的查询:

DECLARE @startdate DATETIME = '##startdate##';
DECLARE @enddate DATETIME = '##enddate##';

SELECT  ISNULL(t2.Year, t1.Year) ,
        ISNULL(t2.Month, t1.Month) ,
        t1.LastName1 ,
        t2.LastName2
FROM    ( SELECT    DATEPART(year, table1.CreationDate) Year ,
                    DATEPART(month, table1.CreationDate) Month ,
                    COUNT(table1.column2) LastName1
          FROM      table1
          WHERE     EventType = 2
                    AND CreationDate BETWEEN @startdate AND @enddate
          GROUP BY  DATEPART(year, table1.CreationDate) ,
                    DATEPART(month, table1.CreationDate)
        ) AS t1

        JOIN 

             ( SELECT   DATEPART(year, table2.CreationDate) Year ,
                        DATEPART(month, table2.CreationDate) Month ,
                        COUNT(table2.column2) LastName2
               FROM     table2
               WHERE    EventType = 1
                        AND CreationDate BETWEEN @startdate AND @enddate
               GROUP BY DATEPART(year, table2.CreationDate) ,
                        DATEPART(month, table2.CreationDate)
             ) AS t2 ON t1.Year = t2.Year
                        AND t1.Month = t2.Month
ORDER BY t1.Year ,
        t1.Month

我可以添加更多JOIN吗? (我试过这个并且发现了。)或者是否有另一种方法可以在每个选定列中的指定日期范围内返回COUNT(值)。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:7)

DECLARE @startdate DATETIME 
set @startdate= '2013-01-02 18:00:21.000';
DECLARE @enddate DATETIME 
set @enddate= '2013-01-09 17:00:21.000';


SELECT  YEAR ,
        MONTH ,
        [1] ,
        [2]
FROM    ( (SELECT    DATEPART(year, CreationDate) Year ,
                    DATEPART(month, CreationDate) Month ,
                    eventType ,
                    COUNT(LastName) namecount
          FROM      table1
          WHERE     CreationDate BETWEEN @startdate AND @enddate
          GROUP BY  DATEPART(year, CreationDate) ,
                    DATEPART(month, CreationDate) ,
                    EventType)
                    union all
          (SELECT    DATEPART(year, CreationDate) Year ,
                    DATEPART(month, CreationDate) Month ,
                    eventType ,
                    COUNT(LastName) namecount
          FROM      table2
          WHERE     CreationDate BETWEEN @startdate AND @enddate
          GROUP BY  DATEPART(year, CreationDate) ,
                    DATEPART(month, CreationDate) ,
                    EventType )         
        ) u PIVOT( SUM(namecount) FOR eventtype IN ( [1], [2] ) ) as pvt
ORDER BY Year ,
        Month

如果你想添加更多的eventtype,只需添加为([1],[2],[3] ..) 在PIVOT()

中的SELECT

根据需要添加任意数量的表。