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

  • Home
  • SEARCH
  • 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 8494899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:25:24+00:00 2026-06-10T23:25:24+00:00

In my case i have three tables. The Contracts table, the agreements table and

  • 0

In my case i have three tables. The Contracts table, the agreements table and the payments table. What i have is a table with contracts which represents the value in debt, another table with the agreements of the conditions to pay off the debt, and another table related to the agreements table who has the debt split in parcels.

ContractsTable
(Id | Number |  Name  |    Debt   | ...)
 1  |  1234  | AAAAAA | 1250,00 € | ...
 2  |  1235  | BBBBBB | 5000,20 € | ...
 3  |  1236  | CCCCCC | 500,00 €  | ...

AgreementsTable
(Id | ContractId |    Debt   | IsValid | ...)
 1  |     1      | 1250,00 € |    1    | ...
 2  |     2      | 5000,20 € |    0    | ...
 3  |     2      | 5000,20 € |    1    | ...
 4  |     3      | 500,00 €  |    0    | ...

PaymentsTable
(Id  | AgreementId |    Date    |  Amount   | IsPaid | ...)
  1  |      1      | 01/08/2012 | 500,00 €  |   1    | ...
  2  |      1      | 01/09/2012 | 500,00 €  |   1    | ... -> Last payment
  3  |      1      | 01/10/2012 | 250,00 €  |   0    | ... -> Next Payment
  4  |      3      | 01/08/2012 | 1000,00 € |   1    | ...
  5  |      3      | 01/09/2012 | 1000,00 € |   1    | ...
  6  |      3      | 01/10/2012 | 1000,00 € |   0    | ...
  7  |      3      | 01/11/2012 | 1000,00 € |   0    | ...
  8  |      3      | 01/12/2012 | 1000,20 € |   0    | ...

So when i execute the stored procedure to get the table with the contracts, i want to somehow see a column with the date of last payment that was done and the date of next payment to be done too. But i will only be able to see the date of the last payment done and the date of the next payment to be done if the contract has a valid agreement related with and in the case of the next payment only if the agreement is valid.

I am working with MS Sql Server.

Thanks in advance!

  • 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-10T23:25:26+00:00Added an answer on June 10, 2026 at 11:25 pm

    Will this work for you?

    select aa.Id
            , aa.Number
            , aa.Name
            , aa.Debt
            ...
            , max(aa.Date_LastPayment) as Date_LastPayment
            , min(aa.Date_NextPayment) as Date_NextPayment
    from    
    (       
        select a.Id
                , a.Number
                , a.Name
                , a.Debt
                ...
                , case when c.IsPaid = 1 then c.[Date]
                        else null
                        end as Date_LastPayment
                , case when c.IsPaid = 0 then c.[Date]
                        else null
                        end as Date_NextPayment
        from ContractsTable a
        inner join AgreementsTable b on b.ContractId = a.Id
        inner join PaymentsTable c on c.AgreementId = b.Id
        where b.IsValid = 1
    ) aa
    group by aa.Id
                , aa.Number
                , aa.Name
                , aa.Debt
                ...
    

    I used ‘min’ for the Next Payment Date assuming there’s multiple next payment dates.

    SQL Fiddle

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

Sidebar

Related Questions

The case: I have 2 tables, 'contracts' and 'members' tied with contract.id = members.cid.
I'm using MS SQL 2008 R2, have three tables with following schema: Table 1:
I have three tables: Users, Groups and GroupsUsers User table: id: 1 username: admin
I have three tables: categories, languages and categories_languages. Categories_languages is many to many table
I have three tables that I use which has proved this problem to be
I have a case where i have three entities with one-to-many and one-to-many relationships:
I have three tables, one of users, one of cases, and one with links
I have three tables, 'A', 'B' and 'C'. I have query on 'A' and
I have three tables STUDENT, DEPARTMENT and COURSE in a University database... STUDENT has
I have three tables called: users , facilities , and staff_facilities . users contains

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.