Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join multiple tables by date range in SQL?

Tags:

sql

sql-server

I'm relatively new to SQL. And I've been struggling mightily to compose a pretty straightforward query that returns a single row.

I'm trying to select multiple column value counts from several different tables, with each count pulled for the same date range.

The tables in my database resemble this:

|   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        |

All tables have a similar CreationDate column.

And my query right now is a single JOIN like the below (which seems to work). I'm trying to add one or several more JOINs so that I can return several more counts per table to the single row result. My current query:

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

Can I just add more JOINs? (I've tried this and stumbled.) Or is there another way to return just the COUNT(values) in a specified date range within each selected column.

Any help would be appreciated.

like image 955
samthebrand Avatar asked Feb 26 '14 04:02

samthebrand


Video Answer


1 Answers

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

If you want to add more eventtype, just add as ([1],[2],[3]..) inside PIVOT() also in SELECT

Add as many tables as you want.

like image 167
Habeeb Avatar answered Oct 31 '22 13:10

Habeeb