Created
August 29, 2019 03:13
-
-
Save worstn8mare/1da09adb15e274612226c240a43c9a95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
t1b.id as contract_id, | |
t1a.unit_id, | |
t1a.asset_id, | |
t1b.date, | |
t1b.due_date, | |
SUM(t1b.rental) as rental, | |
SUM(t1b.advance_rental) as advance_rental, | |
SUM(t1b.security_deposit) as security_deposit, | |
SUM(t1b.surcharge) as surcharge, | |
SUM(t1b.others) as others | |
FROM contract_details as t1a | |
INNER JOIN ( | |
SELECT | |
a.id , b.date, b.due_date, | |
IF(charges_type = 1,c.amount,0) as rental, | |
SUM(IF(charges_type = 3,c.amount,0)) as advance_rental, | |
SUM(IF(charges_type = 4,c.amount,0)) as security_deposit, | |
SUM(IF(charges_type = 5,c.amount,0)) as surcharge, | |
SUM(IF(charges_type = 6,c.amount,0)) as others, | |
c.charges_type | |
from contract_headers as a | |
INNER JOIN property_charges_headers as b on a.id = b.contract_id | |
INNER JOIN property_charges_details as c on c.header_id = b.id | |
WHERE | |
a.status in("Approved","Renewed","Terminate") | |
and YEAR(b.date) = 2019 | |
and MONTH(b.date) = 4 | |
and b.status = "Approved" | |
and c.charges_type in (1,3,4,5,6) | |
GROUP BY a.id, c.charges_type | |
ORDER BY a.id | |
) as t1b on t1a.header_id = t1b.id | |
GROUP BY t1a.unit_id | |
ORDER BY t1b.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment