Sql 查询以显示相关表中的特定值

Sql 查询以显示相关表中的特定值,sql,sql-server,tsql,Sql,Sql Server,Tsql,就我而言,我有三张桌子。合同表、协议表和付款表。我有一个表,其中包含代表债务价值的合同,另一个表包含偿还债务条件的协议,另一个表与协议相关,该协议表将债务拆分为若干部分 ContractsTable (Id | Number | Name | Debt | ...) 1 | 1234 | AAAAAA | 1250,00 € | ... 2 | 1235 | BBBBBB | 5000,20 € | ... 3 | 1236 | CCCCCC | 500,0

就我而言,我有三张桌子。合同表、协议表和付款表。我有一个表,其中包含代表债务价值的合同,另一个表包含偿还债务条件的协议,另一个表与协议相关,该协议表将债务拆分为若干部分

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    | ...
因此,当我执行存储过程以获取包含合同的表时,我希望看到一列,其中包含上次付款的日期以及下次付款的日期。但是,如果合同中有与下一次付款相关的有效协议,我只能看到最后一次付款的日期和下一次付款的日期,如果协议有效,我才能看到下一次付款的日期

我正在使用MS Sql Server


提前谢谢

MaxPaymentsTable.Date不是您想要的。 请更好地定义上次付款日期和下次付款日期

select ContractsTable.Id,  ContractsTable.Number, ContractsTable.Debt 
, count(AgreementsTable.ID) as 'NumAgreements'
, max(PaymentsTable.Date) 
from ContractsTable 
left outter join AgreementsTable 
on ContractsTable.ID = AgreementsTable.ID 
left outter join PaymentsTable 
on ContractsTable.ID = PaymentsTable.ID
group by ContractsTable.Id,  ContractsTable.Number, ContractsTable.Debt

这对你有用吗

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
            ...
假设有多个下一个付款日期,我使用“min”作为下一个付款日期


你能提供一个你想要的数据和结果的例子吗?这几乎是我需要的,问题是它显示了所有的付款日期…根据你的样本数据,你的预期结果是什么?我需要得到所有没有任何相关协议的合同。我只得到那些有积极协议的。我正在使用MS Sql Server 2012,正如我所说的,查询不会给出与Sql FIDLE相同的结果。选择a.Id。。。从不存在的ContractsTable a中,从ContractId=a.Id的AgreementsTable中选择1。谢谢!几乎达到了我所需要的。好吧,我发现了他为什么要展示所有的付款。但是我必须替换b.construcd=a.Id上的内部连接协议_表b,用于不存在的位置从协议中选择1,稳定的位置construcd=a.Id?我试过了,但没有成功。。。