0
Also ich suche nach Gesamtrechnungen pro Kunde, wo eine Rechnung in beiden Jahren existiert. So habe icherhalten Summen nach Jahr, wenn Rechnungen in beiden Jahren existieren
SELECT Sum((tbl_invoice_details.quantity
- ifnull(tbl_invoice_details.quantity_returned, 0)
) * tbl_invoice_details.price) AS dollarsSold,
tbl_customers.NAME,
cust_ID AS customer_id,
YEAR (tbl_invoice.invoice_date) AS invoiceYear
FROM `tbl_invoice_details`
INNER JOIN `tbl_invoice`
ON `tbl_invoice_details`.`invoice_ID` = `tbl_invoice`.`invoice_ID`
INNER JOIN `tbl_customers`
ON `tbl_customers`.`customer_ID` = `tbl_invoice`.`cust_ID`
WHERE MONTH (`invoice_date`) = '2'
AND (YEAR (`invoice_date`) = '2017'
OR
YEAR (`invoice_date`) = '2016')
AND EXISTS (SELECT *
FROM tbl_invoice
WHERE cust_ID = customer_id
AND MONTH (`invoice_date`) = '2'
AND YEAR (`invoice_date`) = '2016')
GROUP BY MONTH (tbl_invoice.invoice_date),
invoiceYear,
`tbl_customers`.`name`,
customer_id
ORDER BY name
So wie ich es Kunden beschränken, die sowohl eine Rechnung im Februar 2016 haben und 2017. Ich versuchte es mit den EXISTS
aber das ist nur halb erfolgreich.