LMS – SQL query #30052019

/*
Wynik zapytania:
- pokaż wszystkie faktury klienta (customerid), na których jest internet (23% VAT) oraz telewizja (8% VAT)

Legenda:
- taxid = 2 (internet 23% VAT)
- taxid = 3 (iptv 8% VAT)
- customerid = 43 (wynik dla konkretnego klienta)
- YEAR(FROM_UNIXTIME(TIME)) = 2018 (przeszukiwany rok)
*/
SELECT DISTINCT docid,
                SUM(CASE
                        WHEN taxid = 2 THEN 1
                        ELSE 0
                    END) AS NET,
                SUM(CASE
                        WHEN taxid = 3 THEN 1
                        ELSE 0
                    END) AS TV
FROM cash
WHERE YEAR(FROM_UNIXTIME(TIME))=2018
  AND customerid=43
  AND docid IN
    (SELECT docid
     FROM cash
     WHERE value < 0
       AND docid IS NOT NULL
     GROUP BY docid
     HAVING SUM(CASE
                    WHEN taxid = 2 THEN 1
                    ELSE 0
                END) >= 1
     AND SUM(CASE
                 WHEN taxid = 3 THEN 1
                 ELSE 0
             END) >= 1)
GROUP BY docid

Leave a Reply

Your email address will not be published. Required fields are marked *