Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:59:24+00:00 2026-06-16T10:59:24+00:00

I have a table data structure similar to this: Landlord table Id Name Email

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:59:25+00:00Added an answer on June 16, 2026 at 10:59 am

    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).

    SELECT Landlord.Id as LandlordId, Landlord.Name, 
           ISNULL(TotalRent,0) AS TotalRent, 
           ISNULL(TotalCost,0) AS TotalCost
    FROM Landlord
    LEFT JOIN 
        (SELECT SUM(Rent) as TotalRent, LandLordId 
         FROM Property
         GROUP BY LandLordId) Rents
    ON Landlord.Id = Rents.LandlordId
    LEFT JOIN 
        (SELECT LandLordId,SUM(Cost) AS TotalCost
         FROM Property
         INNER JOIN Maintenance
            ON Property.Id = Maintenance.PropertyId
         GROUP BY LandLordId
         ) MaintenanceCosts
    ON Landlord.Id = MaintenanceCosts.LandlordId
    ORDER BY Landlord.Name
    

    Just noticed you also wrote:

    Is there a way to select all landlords, their properties, and the amount payable to them in one query, please?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My table have data structure like this cate_id task_id date_start date_end other 34 14
I have a table emp with following structure and data: name dept salary -----
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a data structure similar to the following picture. Table A has_many Table
I have a table structure similar to this: id | order1_id | order1_type |
I have an HTML table of similar structure to this, however: rowspans are not
I have a MySQL table with a tree data structure. The fields are _id
I'm a bit confused on this. I have a data table structured like this:
I have table in data base name train delay, with columns train number(int), DelayTime(int),
Say I have a data structure like a Dictionary<string, Dictionary<string, int>> or similar, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.