因此,我尝试合并具有几乎相同结构但不同where子句的代码。我现在唯一能做的就是结合他们。
这是我的代码片段:
insert into #tempAging
select
mm0.transid, max(mm0.mi) as Mi,
CASE
when (max(mm0.DueDate) > @StatementDate)
then
case
when sum(isnull(mm0.OverDueAmt, 0)) = 0
then '01 CURRENT AND NEW ACCOUNTS'
when ((((sum(isnull(mm0.OverDueAmt, 0))) / (max(mm0.MI))) > 0) AND ((((sum(isnull(mm0.OverDueAmt, 0))) / (max(mm0.MI))) <= 1) ))
then '02 ONE(1) MONTH'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 1 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=2) ))
then '03 TWO(2) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 2 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=3) ))
then '04 THREE(3) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 3 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=4) ))
then '05 FOUR(4) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 4 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=5) ))
then '06 FIVE(5) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 5 ))
then '07 SIX(6) MONTHS'
end
else
case
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 0
then '09 LAPCON ONE(1) MONTH'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 1
then '10 LAPCON TWO(2) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 2
then '11 LAPCON THREE(3) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 3
then '12 LAPCON FOUR(4) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 4
then '13 LAPCON FIVE(5) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) >= 5
then '14 LAPCON SIX(6) MONTHS'
end
END AS Aging
from
(select
e0.transid, e0.DueDate,
CASE
when e0.DueDate > @StatementDate then sum(e0.debit) - sum(isnull(e2.ReconSum,0))
else 0
end as CurrentBal,
CASE
when max(isnull(e0.Debit,0)) <> max(isnull(e2.ReconSum,0)) and max(e0.DueDate) <= @StatementDate
then sum(e0.debit) - sum(isnull(e2.ReconSum,0))
else 0
end as OverDueAmt,
(select top 1 insTotal
from inv6
where objType = '13'
and DocEntry = max(e1.docentry)) as MI
from
JDT1 e0
inner join
oinv e1 on e1.transid = e0.transid
LEFT OUTER JOIN
(select TransID, TransRowID, ReconSum, LastReconDate
from #tempRecon) e2 ON e2.[TransId] = e0.[TransId]
AND e2.[TransRowId] = e0.[Line_ID]
left outer join
OACT e3 on e3.AcctCode = e0.Account
where
isnull(e0.transcode, '') not in (N'BEG', N'ADJ')
and e3.Segment_0 = N'11210'
group by
e0.transid, e0.line_id, e0.DueDate) mm0
group by
mm0.transid
UNION ALL
select mm0.transid, max(mm0.mi) as Mi,
CASE when ( max(mm0.DueDate) > @StatementDate) then
case when sum(isnull(mm0.OverDueAmt,0)) = 0 then '01 CURRENT AND NEW ACCOUNTS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 0 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=1) )) then '02 ONE(1) MONTH'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 1 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=2) )) then '03 TWO(2) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 2 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=3) )) then '04 THREE(3) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 3 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=4) )) then '05 FOUR(4) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 4 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=5) )) then '06 FIVE(5) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 5 )) then '07 SIX(6) MONTHS'
end
else
case
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 0 then '09 LAPCON ONE(1) MONTH'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 1 then '10 LAPCON TWO(2) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 2 then '11 LAPCON THREE(3) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 3 then '12 LAPCON FOUR(4) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 4 then '13 LAPCON FIVE(5) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) >= 5 then '14 LAPCON SIX(6) MONTHS'
end
END as Aging
from
( select e0.transid, e0.DueDate,
CASE
when e0.DueDate > @StatementDate then sum(e0.debit) - sum(isnull(e2.ReconSum,0)) else 0 end as CurrentBal,
CASE
when max(isnull(e0.Debit,0)) <> max(isnull(e2.ReconSum,0)) and max(e0.DueDate) <= @StatementDate then sum(e0.debit) - sum(isnull(e2.ReconSum,0)) else 0 end as OverDueAmt,
(select top 1 insTotal from inv6 where objType = '13' and DocEntry = max(e1.docentry)) as MI
from JDT1 e0
inner join oinv e1 on e1.transid = e0.transid
LEFT OUTER JOIN (select TransID, TransRowID, ReconSum, LastReconDate from #tempRecon) e2 ON e2.[TransId] = e0.[TransId] AND e2.[TransRowId] = e0.[Line_ID]
left outer join OACT e3 on e3.AcctCode = e0.Account
where isnull(e0.transcode,'') in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210' and e0.TransType = N'30'
group by e0.transid, e0.line_id, e0.DueDate) mm0
group by mm0.transid
我希望有人可以帮助我。 TIA!
答案 0 :(得分:0)
根据我的理解,你基本上需要的是将两个逻辑表达式合二为一,避免结合。
where isnull(e0.transcode,'') not in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210'
OR
where isnull(e0.transcode,'') in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210' and e0.TransType = N'30'
基本上是一个,
(A and B and C) or ((not A) and B)
由于这已经是最简单的see here形式,您可以使用
where (isnull(e0.transcode,'') not in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210') or (isnull(e0.transcode,'') in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210' and e0.TransType = N'30')
在第一个选择并删除联盟的第二部分。
因此,您的最终查询将如下所示:
insert into #tempAging
select mm0.transid, max(mm0.mi) as Mi,
CASE when ( max(mm0.DueDate) > @StatementDate) then
case when sum(isnull(mm0.OverDueAmt,0)) = 0 then '01 CURRENT AND NEW ACCOUNTS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 0 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=1) )) then '02 ONE(1) MONTH'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 1 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=2) )) then '03 TWO(2) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 2 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=3) )) then '04 THREE(3) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 3 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=4) )) then '05 FOUR(4) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 4 ) AND ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) <=5) )) then '06 FIVE(5) MONTHS'
when ((((sum(isnull(mm0.OverDueAmt,0)))/(max(mm0.MI))) > 5 )) then '07 SIX(6) MONTHS'
end
else
case when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 0 then '09 LAPCON ONE(1) MONTH'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 1 then '10 LAPCON TWO(2) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 2 then '11 LAPCON THREE(3) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 3 then '12 LAPCON FOUR(4) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) = 4 then '13 LAPCON FIVE(5) MONTHS'
when DATEDIFF(month, max(mm0.DueDate) , @StatementDate) >= 5 then '14 LAPCON SIX(6) MONTHS'
end
END as Aging
from
( select e0.transid, e0.DueDate,
CASE
when e0.DueDate > @StatementDate then sum(e0.debit) - sum(isnull(e2.ReconSum,0)) else 0 end as CurrentBal,
CASE
when max(isnull(e0.Debit,0)) <> max(isnull(e2.ReconSum,0)) and max(e0.DueDate) <= @StatementDate then sum(e0.debit) - sum(isnull(e2.ReconSum,0)) else 0 end as OverDueAmt,
(select top 1 insTotal from inv6 where objType = '13' and DocEntry = max(e1.docentry)) as MI
from JDT1 e0
inner join oinv e1 on e1.transid = e0.transid
LEFT OUTER JOIN (select TransID, TransRowID, ReconSum, LastReconDate from #tempRecon) e2 ON e2.[TransId] = e0.[TransId] AND e2.[TransRowId] = e0.[Line_ID]
left outer join OACT e3 on e3.AcctCode = e0.Account
where (isnull(e0.transcode,'') not in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210') or (isnull(e0.transcode,'') in (N'BEG',N'ADJ') and e3.Segment_0 = N'11210' and e0.TransType = N'30')
group by e0.transid, e0.line_id, e0.DueDate) mm0
group by mm0.transid