2017-03-01 3 views
0

Wirklich einfach (kann hier kein Beispiel finden). Grundsätzlich möchte ich die Nullwerte für fehlende Monate hinzufügen.Mysql-Gruppe nach Monat mit Null-Werten, wenn 0 Entitäten

Zur Zeit habe ich

|month| total | 
---------------- 
    2 | 2454.34 
    3 | 1254.34 

Wo, wie ich

|month| total | 
---------------- 
    1 | 0 
    2 | 2454.34 
    3 | 1254.34 
    4 | 0 
    5 | 0 
    6 | 0 

Und so weiter wollen.

Meine Anfrage bisher

SELECT MONTH(bookings.booking) as month, SUM(bookings.tendered) as total 
FROM bookings 
INNER JOIN salons ON salons.id = bookings.salon_id 
WHERE bookings.paid = 1 
AND YEAR(bookings.booking) = 2017 
GROUP BY MONTH(bookings.booking) 
ORDER BY MONTH(bookings.booking); 

Ich habe folgendes versuchen aber scheint nicht so, wie ich es will zu arbeiten? Entschuldigen Sie die verschiedenen Formate von oben Beispiel

SUM(IF(MONTH(bookings.booking) = 'Jan', bookings.tendered, 0)) AS 'Jan', 
     SUM(IF(MONTH(bookings.booking) = 'Feb', bookings.tendered, 0)) AS 'Feb', 
     SUM(IF(MONTH(bookings.booking) = 'Mar', bookings.tendered, 0)) AS 'Mar', 
     SUM(IF(MONTH(bookings.booking) = 'Apr', bookings.tendered, 0)) AS 'Apr', 
     SUM(IF(MONTH(bookings.booking) = 'May', bookings.tendered, 0)) AS 'May', 
     SUM(IF(MONTH(bookings.booking) = 'Jun', bookings.tendered, 0)) AS 'Jun', 
     SUM(IF(MONTH(bookings.booking) = 'Jul', bookings.tendered, 0)) AS 'Jul', 
     SUM(IF(MONTH(bookings.booking) = 'Aug', bookings.tendered, 0)) AS 'Aug', 
     SUM(IF(MONTH(bookings.booking) = 'Sep', bookings.tendered, 0)) AS 'Sep', 
     SUM(IF(MONTH(bookings.booking) = 'Oct', bookings.tendered, 0)) AS 'Oct', 
     SUM(IF(MONTH(bookings.booking) = 'Nov', bookings.tendered, 0)) AS 'Nov', 
     SUM(IF(MONTH(bookings.booking) = 'Dec', bookings.tendered, 0)) AS 'Dec', 
     SUM(tendered) AS total 

AKTUALISIERT

select `a`.`month` as `month_int`, IFNULL(SUM(bookings.tendered), 0) as total from `bookings` right join (
SELECT 1 as month 
UNION SELECT 2 as month 
UNION SELECT 3 as month 
UNION SELECT 4 as month 
UNION SELECT 5 as month 
UNION SELECT 6 as month 
UNION SELECT 7 as month 
UNION SELECT 8 as month 
UNION SELECT 9 as month 
UNION SELECT 10 as month 
UNION SELECT 11 as month 
UNION SELECT 12 as month\n 
) a on `a`.`month` = MONTH(bookings.booking) where `bookings`.`paid` = ? and date(`bookings`.`booking`) > ? and `salon_id` in (?, ?, ?) group by `a`.`month` 
+0

Sie können Ihre Anfrage mit 'verbinden (SELECT 1 AS Monat UNION ALLE SELECT 2 ... UNION ALLE SELECT 12)' – shmosel

+0

@shmosel das ist eine gute Idee, danke ich werde es versuchen! Laravel Query Builder ist nicht das greeet – sourRaspberri

Antwort

0

Dieser Code funktioniert

SELECT m.MONTH as month, 
CASE 
    WHEN SUM(b.total) > 0 THEN SUM(b.total) 
    ELSE 0 END as total 
FROM 
(
SELECT '01' AS 
MONTH 
UNION SELECT '02' AS 
MONTH 
UNION SELECT '03' AS 
MONTH 
UNION SELECT '04' AS 
MONTH 
UNION SELECT '05' AS 
MONTH 
UNION SELECT '06' AS 
MONTH 
UNION SELECT '07' AS 
MONTH 
UNION SELECT '08' AS 
MONTH 
UNION SELECT '09' AS 
MONTH 
UNION SELECT '10' AS 
MONTH 
UNION SELECT '11' AS 
MONTH 
UNION SELECT '12' AS 
MONTH 
) AS m 
LEFT JOIN 
(SELECT MONTH(b.bookings) AS 'month', SUM(b.tendered) AS 'total' 
FROM bookings AS b 
INNER JOIN salons AS s 
ON s.id = b.salon_id 
WHERE b.paid = 1 
AND YEAR(b.booking) = 2017) AS n 
ON 
m.MONTH = n.month 
GROUP BY 1 
ORDER BY 1; 
0

Sie dieses Beispiel Online sehen: http://sqlfiddle.com/#!9/8bbf0/1

SELECT 
    idMonth, 
    MONTHNAME(STR_TO_DATE(idMonth, '%m')) as m, 
    IFNULL(sum(Bookings.price), 0) as total 
FROM Bookings 
RIGHT JOIN (
    SELECT 1 as idMonth 
    UNION SELECT 2 as idMonth 
    UNION SELECT 3 as idMonth 
    UNION SELECT 4 as idMonth 
    UNION SELECT 5 as idMonth 
    UNION SELECT 6 as idMonth 
    UNION SELECT 7 as idMonth 
    UNION SELECT 8 as idMonth 
    UNION SELECT 9 as idMonth 
    UNION SELECT 10 as idMonth 
    UNION SELECT 11 as idMonth 
    UNION SELECT 12 as idMonth 
) as Month 
ON Month.idMonth = month(`date`) 
GROUP BY Month.idMonth 
+0

Ich mag deine Antwort :) Obwohl ich versucht habe, dies anzuwenden, und ich bekomme das gleiche Ergebnis wie zuvor. Ich benutze 5.7.14, wenn das einen Unterschied macht? Ich habe den vollständigen Abfrage-Ausdruck aus dem Abfrage-Generator in der Antwort – sourRaspberri

+0

Thx für Ihr neues Codebeispiel hinzugefügt, aber was ist Ihr Ergebnis? Ich sehe kein Problem – arnolem

Verwandte Themen