2014-02-26 9 views
6

Ich bin relativ neu in SQL. Und ich habe mich mächtig angestrengt, eine ziemlich einfache Abfrage zu erstellen, die eine einzelne Zeile zurückgibt.Wie verbinden Sie mehrere Tabellen nach Datumsbereich in SQL?

Ich versuche, mehrere Spaltenwerte aus mehreren verschiedenen Tabellen auszuwählen, wobei jede Anzahl für denselben Zeitraum gezogen wird.

Die Tabellen in meiner Datenbank diese ähneln:

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

Alle Tabellen haben eine ähnliche ErstellDatum Spalte.

Und meine Abfrage ist jetzt ein einzelnes JOIN wie das unten (das scheint zu funktionieren). Ich versuche, ein oder mehrere JOINs hinzuzufügen, so dass ich mehrere Zähler pro Tabelle zum Ergebnis der einzelnen Zeile zurückgeben kann. Meine aktuelle Abfrage:

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 

Kann ich einfach weitere JOINs hinzufügen? (Ich habe das versucht und gestolpert.) Oder gibt es eine andere Möglichkeit, nur die COUNT (Werte) in einem bestimmten Datumsbereich innerhalb jeder ausgewählten Spalte zurückzugeben.

Jede Hilfe wäre willkommen.

+1

Bitte zeigen Sie uns die Abfrage, die Sie versucht haben, und ein Beispiel der Daten in Ihren Tabellen. – wruckie

+0

@wruckie - Codebeispiel + Datenbankschema hinzugefügt. Etwas besser? – samthebrand

Antwort

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 

Wenn Sie mehr zu Eventtype hinzufügen, fügen Sie einfach als ([1], [2], [3] ..) innen PIVOT() auch in SELECT

Fügen Sie so viele Tabellen, wie Sie wollen .

+0

Das hat geholfen. Danke vielmals! – samthebrand

Verwandte Themen