SQL Select 多个连接和 MAX(field) 结果

SQL Select multiple joins and MAX(field) result

我正在使用 IBM Cognos 创建一个允许相当标准的报告 SQL。我最终试图从 2 个表中获取数据并将它们相互比较以确保它们匹配。在 t1 和 t3 之间比较给定的 Contract_Number、Contract_Item 和 Stream_Type 时,Billing_Term、Bill_Period 和 Contract_Term_Amount 都需要匹配,但仅针对MAX Seq_No 从 t3.

我在 Seq_No 上尝试了一个简单的 MAX(),但这没有用,所以我正在寻求帮助,以将相关查询结果的结果限制为 MAX Seq_No。



    SELECT
        t1.Contract_Number, 
        t1.Contract_Item, 
        t1.Stream_Type, 
        t1.Billing_Term, 
        t1.Bill_Period, 
        t1.Contract_Term_Amount,
        t2.Reference_Document,
        t3.Billing_Term,
        t3.Bill_Period,
        t3.Contract_Term_Amount,
        t3.Seq_No
    FROM
        LeasingStreamData t1 
    INNER JOIN ContractsData t2 ON t1.Contract_Number = t2.Sales_Document
    LEFT JOIN LeasingStreamData t3 ON t2.Reference_Document = t3.Contract_Number AND
        t1.Contract_Item = t3.Contract_Item AND
        t1.Stream_Type = t3.Stream_Type
    WHERE 
    t1.Contract_Number IN ([some list of contracts])
    GROUP BY 
        t1.Contract_Number, 
        t1.Contract_Item, 
        t1.Stream_Type, 
        t1.Billing_Term, 
        t1.Bill_Period, 
        t1.Contract_Term_Amount,
        t2.Reference_Document,
        t3.Billing_Term,
        t3.Bill_Period,
        t3.Contract_Term_Amount,
        t3.Seq_No

我希望这足以获得一些急需的帮助,非常感谢您提供的任何帮助!

如果我理解您的要求,我认为您希望创建查询并以如下所示的方式加入它们 SQL:

SELECT
  t1.Contract_Number
, t1.Contract_Item
, t1.Stream_Type
, t1.Billing_Term
, t1.Bill_Period
, t1.Contract_Term_Amount
, t2.Reference_Document
, t3.Billing_Term
, t3.Bill_Period
, t3.Contract_Term_Amount
, t3.Seq_No

FROM (
    SELECT Contract_Number
    , Contract_Item
    , Stream_Type
    , Billing_Term
    , Bill_Period
    , Contract_Term_Amount
    FROM LeasingStreamData
    WHERE Contract_Number in ([some list of contracts])
  ) t1 
  INNER JOIN (
    SELECT Sales_Document
    , Reference_Document
    FROM ContractsData
    WHERE Sales_Document in ([some list of contracts])
  ) t2 ON t1.Contract_Number = t2.Sales_Document
  INNER JOIN LeasingStreamData t3 ON t2.Reference_Document = t3.Contract_Number 
                               AND t1.Contract_Item = t3.Contract_Item 
                               AND t1.Stream_Type = t3.Stream_Type
  INNER JOIN (
      select max(Seq_No) as Seq_No
      , Contract_Number
      , Contract_Item
      , Stream_Type
      from LeasingStreamData lsd
      group by Contract_Number
      , Contract_Item
      , Stream_Type
  ) maxseq on maxseq.Contract_Number = t3.Contract_Number
          and maxseq.Contract Item = t3.Contract_Item
          and maxseq.Stream_Type = t3.Stream_Type
          and maxseq.Seq_No = t3.Seq_No

WHERE t1.Billing_Term <> t3.Billing_Term
  and t1.Bill_Period <> t3.Bill_Period
  and t1.Contract_Term_Amount <> t3.Contract_Term_Amount

GROUP BY 
  t1.Contract_Number
, t1.Contract_Item
, t1.Stream_Type
, t1.Billing_Term
, t1.Bill_Period
, t1.Contract_Term_Amount
, t2.Reference_Document
, t3.Billing_Term
, t3.Bill_Period
, t3.Contract_Term_Amount
, t3.Seq_No

我认为这将过滤到 t3 的最大 Seq_No 和 return 您关心的记录(不匹配的记录)。

试试这个:

SELECT t1.Contract_Number
    ,t1.Contract_Item
    ,t1.Stream_Type
    ,t1.Billing_Term
    ,t1.Bill_Period
    ,t1.Contract_Term_Amount
    ,t2.Reference_Document
    ,t3.Billing_Term
    ,t3.Bill_Period
    ,t3.Contract_Term_Amount
    ,t3.Seq_No
FROM LeasingStreamData t1
INNER JOIN ContractsData t2 ON t1.Contract_Number = t2.Sales_Document
LEFT JOIN (
    SELECT Contract_Number
        ,Contract_Item
        ,Stream_type
        ,MAX(Seq_No) AS "Seq_No"
    FROM LeasingStreamData
    GROUP BY Contract_Number
        ,Contract_Item
        ,Stream_Type
    ) t3 ON t2.Reference_Document = t3.Contract_Number
    AND t1.Contract_Item = t3.Contract_Item
    AND t1.Stream_Type = t3.Stream_Type
WHERE t1.Contract_Number IN ([some list of contracts])

由于汇总函数 (MAX) 的汇总依赖于 group by 来确定上下文,因此您必须通过子查询将其隔离以在加入其余表之前获取最大值。

此外,由于您正在预聚合最大序列号,因此您不再需要在主查询中使用分组依据。