2016-09-22 4 views
2

Ich habe eine Abfrage welche Gruppen nach Ortsnamen und JJJJ-MM. Was ich möchte, ist eine kombinierte Summe der 3 Ortsnamen für jeden Monat. Zum Beispiel betrachten die unterhalb der Summe der 3 ‚Orte‘ für ‚total1‘ 14. März wäre, 4 + 5 + 5Untersummen in einer Gruppe von

place   yyyy-mm   total1  total2  total3 
    A   2016-03   4   4   5 
    B   2016-03   5   1   2 
    C   2016-03   5   4   2 
    A   2016-04   1   3   4 
    B   2016-04   2   3   4 
    C   2016-04   6   2   1 

So etwas wie diese

place  yyyy-mm   total1  total2  total3 
    A   2016-03   4   4   5 
    B   2016-03   5   1   2 
    C   2016-03   5   4   2 
    ALL   2016-03   14   9   9 
    A   2016-04   1   3   4 
    B   2016-04   2   3   4 
    C   2016-04   6   2   1 
    ALL   2016-04   9   8   9 
+0

Welche Version von SQL-Server Sie verwenden? Weil Sie die Window-Funktion 'Over()' ab 2008 benutzen können. – Nebi

+0

Verwendung von SQL Server 2012 -thanks, das ist keine Funktion, die ich kenne – whitz11

Antwort

2

können Sie auch verwenden Vereinigung alle, aber Rollup ist besser in Bezug auf Lesbarkeit und Leistung (nicht Tabelle zweimal Zugriff)

SELECT 
CASE WHEN PLACE IS NULL THEN 'ALL' ELSE PLACE END as place, 
YYYYMM, 
SUM(TOTAL1) AS TOTAL1,SUM(TOTAL2) AS TOTAL2,SUM(TOTAL3) AS TOTAL3 
FROM #TEMP 
GROUP BY YYYYMM,PLACE 
WITH ROLLUP 
HAVING GROUPING(YYYYMM)=0 

Ausgang:

place YYYYMM TOTAL1 TOTAL2 TOTAL3 
A  2016-03 4  4  5 
B  2016-03 5  1  2 
C  2016-03 5  4  2 
ALL  2016-03 14  9  9 
A  2016-04 1  3  4 
B  2016-04 2  3  4 
C  2016-04 6  2  1 
ALL  2016-04 9  8  9 
+0

meine JJJJ-MM-Spalte ist wie folgt eingerichtet Convert (char (7), [DateTimeOfCall], 121) "yyyy-mm" wie kann ich einfügen das bitte in die 'Gruppierung' richtig? – whitz11

+0

@ whitz11: Wie ist das abweichend von Ihrer erwarteten Ausgabe, es ist eine neue Frage, stellen Sie eine neue Frage oder aktualisieren Sie die Frage mit Details – TheGameiswar

2

Verwenden Group By mit ROLLUP:

DECLARE @tblTest AS Table(
    Place VARCHAR(5), 
    YearMonth DATE, 
    Total1 INT, 
    Total2 INT, 
    Total3 INT 
) 

INSERT INTO @tblTest VALUES 
('A','2016-03-01',4,4,5), 
('B','2016-03-01',5,1,2), 
('C','2016-03-01',5,4,2), 
('A','2016-04-01',1,3,4), 
('B','2016-04-01',2,3,4), 
('C','2016-04-01',6,2,1) 

;with X AS 
(
    SELECT 
     Place, 
     YearMonth,   
     SUM(Total1) AS Total1, 
     SUM(Total2) AS Total2, 
     SUM(Total3) AS Total3 
    FROM @tblTest 
    GROUP BY YearMonth,Place WITH ROLLUP 
) 
SELECT 
    CASE ISNULL(Place,'') WHEN '' THEN 'ALL' ELSE Place END AS Place, 
    YearMonth, 
    Total1, 
    Total2, 
    Total3 
FROM X 
WHERE X.YearMonth IS NOT NULL 

Ausgang:

enter image description here

3

könnten Sie verwenden GROUP BY GROUPING SETS

CREATE TABLE #MyTable(Place VARCHAR(10), Date VARCHAR(20), Total1 INT, Total2 INT, Total3 INT) 

INSERT INTO #MyTable (place, [date], Total1, Total2, Total3) 
VALUES(  'A',   '2016-03' ,   4,   4 ,   5) 
     , ('B',   '2016-03',   5,   1,   2) 
     , ('C',   '2016-03',   5,   4,   2) 
     , ('A',   '2016-04',   1,   3,   4) 
     , ('B',   '2016-04',   2,   3,   4) 
     , ('C',   '2016-04',   6,   2,   1) 


SELECT 
    [Date] 
    , ISNULL(Place , 'Total') 
    , SUM(Total1) AS Total1 
    , SUM(Total2) AS Total2 
    , SUM(Total3) AS Total3 
FROM #MyTable 
GROUP BY GROUPING SETS 
(Place, [Date]) 
, ([Date]) 


DROP TABLE #MyTable 
2

T Dies kann unter Verwendung von GROUPING SETS erreicht werden.

Abfrage

select 
    coalesce([place], 'ALL') as [place], 
    [yyyy-mm], 
    sum([total1]) as [total1], 
    sum([total2]) as [total2], 
    sum([total3]) as [total3] 
from [your_table_name] 
group by grouping sets(([place], [yyyy-mm]), ([yyyy-mm])); 

Find a demo here