我正在使用IBM Cognos创建一个报告,该报告允许使用相当标准的SQL。我最终尝试从2个表中获取数据,并将它们相互比较以确保它们匹配。在比较t1和t3之间的给定Contract_Number,Contract_Item和Stream_Type时,Billing_Term,Bill_Period和Contract_Term_Amount都需要匹配,但仅限于t3中的MAX Seq_No。
我在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
我希望这足以获得一些急需的帮助,非常感谢您能提供的任何帮助!
答案 0 :(得分:0)
If I understand you requirement, I think you want to create queries and join them in a way that will look like this 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
I think this will filter to only the max Seq_No for t3 and return only the records that you care about (the ones that don't match).
答案 1 :(得分:0)
尝试一下:
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)的汇总取决于分组依据来确定上下文,因此在连接其余表之前,您必须通过子查询将其隔离以获取最大值。
此外,由于您正在预先汇总最大序列号,因此在主查询中不再需要分组依据。