I have a table data structure similar to this:
Landlord table
Id Name Email
```````````````````````
1 J Johnson ...
2 R Kelly ...
Property table
Id Address Rent LandlordId
```````````````````````````````````
1 .... 400 1
2 .... 600 1
3 .... 750 2
Maintenance table
Id Details Cost MaintenanceDate PropertyId
`````````````````````````````````````````````````````
1 .... 25 20/12/2012 1
2 .... 120 22/12/2012 2
3 .... 35 24/12/2012 3
Essentially, a Landlord has multiple properties.. Each month, I need to produce an invoice for the landlord which includes all his properties, all the maintenance done on his property. To calculate how much I need to pay to the landlord, I need to sum all his properties’ Rent and subtract that with the sum of all his maintenance costs for that month.
So, amount payable to landlord L = Sum(Rent of properties of L) – Sum(maintenance costs on all properties of L during this month)
I am using telerik reporting, thought I could achieve it with some clever grouping but that was a waste of my time so I am now going to try and achieve this with SQL and sub reports instead.
The SQL query that I’m trying is this:
SELECT l.Name, p.[Address], p.Rent, c.Details, c.Cost,
(select Rent From Property where Id = p.Id) -
(select SUM(cost) from CarriedOutJobs where PropertyId = p.Id)
as PayableToLandlord
FROM Landlord l JOIN PROPERTY p ON p.LandlordId = l.Id
LEFT OUTER JOIN Maintenance c ON c.PropertyId = p.Id
ORDER BY l.Fullname
This doesn’t seem to work properly as it produces multiple fields
Because I am going to breakup the report into sub report, I thought I would just get landlord details first but I still need to calculate the amount payable to Landlord even in this case.. So I rewrote the query to this:
SELECT distinct l.Name,
(SELECT SUM(Rent) FROM PROPERTY WHERE LandlordId = l.Id) -
(SELECT COALESCE(SUM(cost), 0)
FROM CarriedOutJobs WHERE PropertyId = p.Id) AS PayableToLandlord
FROM Landlord l
JOIN PROPERTY p ON p.LandlordId = l.Id
ORDER BY l.Fullname
I thought this worked okay, but even though I have used distinct, this seems to produce a duplicate row with different PayableToLandlord amount and I can’t seem to figure out why.
Is there a way to select all landlords, their properties, and the amount payable to them in one query, please?
I have removed date where clause for the sake of simplicity here.
Thanks.
I suggest keep it simple and use sub-queries as these are easy to work out what’s going on. Hopefully you can then do your subtraction etc either in the report or quite simply as additional return columns.
Any date parameters you should pass in as variables in to the sub-queries (probably).
Just noticed you also wrote:
You can further join in the list of properties but you’ll end up repeating the sum on each line. This is fine as long as you don’t try to SUM it within your reporting package.
If you do want the property details then my suggestion is to pull in each property rent and cost and then do the SUM / net figure in the reporting package.