I have this code:
SELECT tenant.scode
,tenant.sunitcode
,rtrim(tenant.sLastName) + ', ' + (tenant.sFirstName)
,charge.total
,tenant.istatus
,tenant.dtmovein
FROM tenant
LEFT OUTER JOIN (
SELECT sum(CASE
WHEN trans.itype = 6
THEN stotalamount * - 1
ELSE CASE
WHEN trans.itype = 3
THEN stotalamount * - 1
ELSE stotalamount
END
END) total
,hperson
FROM trans
where trans.itype in (6,7)
GROUP BY hperson
) charge ON tenant.hmyperson = charge.hperson
WHERE charge.total IS NOT NULL
AND charge.total <> 0
AND tenant.istatus IN (
0
,3
,4
)
and hproperty = 2396
UNION ALL
SELECT tenant.scode
,tenant.sunitcode
,rtrim(tenant.sLastName) + ', ' + (tenant.sFirstName)
,charge.total
,tenant.istatus
,tenant.dtmovein
FROM tenant
LEFT OUTER JOIN tenant t2 on (tenant.sleasefield36 = t2.scode)
LEFT OUTER JOIN (
SELECT sum(CASE
WHEN trans.itype = 6
THEN stotalamount * - 1
ELSE CASE
WHEN trans.itype = 3
THEN stotalamount * - 1
ELSE stotalamount
END
END) total
,hperson
FROM trans
where itype in (6,7)
GROUP BY hperson
) charge ON t2.hmyperson = charge.hperson
WHERE charge.total IS NOT NULL
AND charge.total <> 0
AND tenant.istatus IN (
0
,3
,4
)
and tenant.hproperty = 2396
ORDER BY total
It retrieves all the tenants from a certain property who are delinquent in their rent and other payments and also the money amounts they owe.
My problem is that one of the properties was recently sold so now I need to retrieve their old balance from the previous property and add that to their balance from the new property. I essentially have this working correctly, but the two amounts ( 1 from old property and 1 from new property) show up in my results as two separate rows. First balance amount is retrieved from first query, and the second from the query after the UNION ALL.
How can I make these two rows for each tenant combine balance amounts so I have one result row for each tenant?
Thanks in advance! 🙂 Let me know if I can provide any more details
Quick, brain-dead answer (note you have to give your last, first concatenation a name like “name”):