Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT tbl_account.*,
- SUM(IF(tbl_invoice.fld_status IN (1, 2), tbl_invoice.fld_brutto - tbl_invoice.fld_unsettled_amount,
- 0)) AS invoices_brutto_paid,
- SUM(IF(tbl_invoice.fld_due_date < NOW() && tbl_invoice.fld_status IN (0, 2), tbl_invoice.fld_unsettled_amount,
- 0)) AS invoices_brutto_overtime_not_paid,
- SUM(IF(tbl_invoice.fld_due_date < NOW() && tbl_invoice.fld_status IN (0, 2), 1,
- 0)) AS invoices_number_overtime_not_paid,
- SUM(IF(tbl_invoice.fld_due_date < NOW(), 1, 0)) AS invoices_number_overtime_all,
- (SELECT COUNT(tbl_invoice.fld_id)
- FROM tbl_invoice
- WHERE tbl_invoice.fld_account_id = tbl_account.fld_id
- AND tbl_invoice.fld_date >= "2018-10-01"
- AND tbl_invoice.fld_date <= "2018-10-31"
- AND tbl_invoice.fld_deleted = 0) AS current_month_invoice,
- (SELECT COUNT(tbl_invoice.fld_id)
- FROM tbl_invoice
- WHERE tbl_invoice.fld_account_id = tbl_account.fld_id
- AND tbl_invoice.fld_date >= "2018-10-01"
- AND tbl_invoice.fld_date <= "2018-10-31"
- AND tbl_invoice.fld_deleted = 0
- AND tbl_invoice.fld_status = 1) AS is_paid,
- COUNT(tbl_invoice.fld_id) AS invoices_number_all,
- tbl_account.fld_vindication AS fld_vindication,
- (SELECT COUNT(tbl_sale.fld_id)
- FROM tbl_sale # JOIN tbl_sale ON tbl_account.fld_id = tbl_sale.fld_account_id
- JOIN `tbl_product` ON `tbl_sale`.`fld_product_id` = `tbl_product`.`fld_id`
- WHERE tbl_sale.fld_account_id = tbl_account.fld_id
- AND tbl_product.fld_type IN (1, 2, 3, 4)) AS ilosc_sprzedazy
- FROM tbl_account
- JOIN tbl_invoice ON tbl_invoice.fld_account_id = tbl_account.fld_id
- JOIN tbl_sale ON tbl_account.fld_id = tbl_sale.fld_account_id
- JOIN `tbl_product` ON `tbl_sale`.`fld_product_id` = `tbl_product`.`fld_id`
- WHERE tbl_invoice.fld_deleted = "0"
- GROUP BY tbl_account.fld_id HAVING ilosc_sprzedazy>1
- ORDER BY tbl_account.fld_company_name, tbl_account.fld_last_name, tbl_account.fld_first_name, tbl_account.fld_last_name,
- tbl_account.fld_first_name
- LIMIT 20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement