Skip to content

Instantly share code, notes, and snippets.

@worstn8mare
Created September 2, 2019 06:44
Show Gist options
  • Save worstn8mare/0733365975576d29c82a002d8ae2822d to your computer and use it in GitHub Desktop.
Save worstn8mare/0733365975576d29c82a002d8ae2822d to your computer and use it in GitHub Desktop.
SELECT
contract_id,
date,
due_date,
SUM(rental) as rental,
SUM(advance_rental) as advance_rental,
SUM(security_deposit) as security_deposit,
SUM(surcharge) as surcharge,
SUM(others) as others,
unit_id
FROM (
SELECT
a.contract_id as contract_id,
a.date,
a.due_date,
IF(b.charges_type = 1,b.amount,0) as rental,
IF(b.charges_type = 3,b.amount,0) as advance_rental,
IF(b.charges_type = 4,b.amount,0) as security_deposit,
IF(b.charges_type = 5,b.amount,0) as surcharge,
IF(b.charges_type = 6,b.amount,0) as others,
IF(b.unit_id is null,(
SELECT cd.unit_id FROM contract_details cd where cd.header_id = a.contract_id limit 1
),b.unit_id) as unit_id
FROM property_charges_headers as a
INNER JOIN property_charges_details as b on a.id = b.header_id
WHERE
YEAR(a.date) = 2019
and MONTH(a.date) = 4
and a.status = "Approved"
and b.charges_type in (1,3,4,5,6)
ORDER BY a.contract_id
) as t1
GROUP BY unit_id
ORDER BY contract_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment