添加子查询以删除不需要的条目

时间:2014-01-22 04:17:59

标签: sql access-vba ms-access-2010

我设置了一个查询,直到我有超过2个客户条目。我目前有一个查询结果如下:

Customer Days_Max Days_Min IntMax   IntMin  IntMin1 IntMax1
-----------------------------------------------------------
Street   0        0        0        0       217.39  260.18
Walter   0        0        0        0       454.45  303.70
George   0        13       0        5268.21 0       0
George   0        0        0        0       5673.45 6103.17
George   15       0        4809.09  0       0       0

我想要的是当有两个以上的客户,IntMin1& IntMax1列不返回结果。所以它看起来像:

Customer Days_Max Days_Min IntMax  IntMin  IntMin1 IntMax1
----------------------------------------------------------
Street   0        0        0       0       217.39  260.18
Walter   0        0        0       0       454.45  303.70
George   0        13       0       5268.21 0       0
George   0        0        0       0       0       0
George   15       0        4809.09 0       0       0

到目前为止,我的查询是:

SELECT 
    T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, 
    T1.ID_Date, T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, 
    T1.Loan_Balance, T1.Cum_Balance, T1.Days_BetweenMAX, T1.Days_BetweenMIN, 
    IIf([t1.Days_BetweenMax] > 0, Sum(CCur([t1.Cum_Balance] * [t1.InterestRate] / 365 * [t1.Days_BetweenMax])), "0") AS IntMax,    
    IIf([t1.Days_BetweenMin] > 0, Sum(CCur([t1.Loan_Balance] * [t1.InterestRate] / 365 * [t1.Days_BetweenMin])), "0") AS IntMin, 
    IIf([t1.Days_BetweenMin] = 0, IIf([t1.Days_BetweenMax] = 0, Sum(CCur([t1.Loan_Balance] * [t1.InterestRate] / 365 * ([t1.Pstng_Date] - [t1.ID_Date])))), "0") AS IntMin1, 
    IIf([t1.Days_BetweenMin] = 0, IIf([t1.Days_BetweenMax] = 0, Sum(CCur([t1.Cum_Balance] * [t1.InterestRate] / 365 * ([t1.Today_Date] - [t1.Pstng_Date])))), "0") AS IntMax1
FROM 
    InterestMax_Min AS T1 
INNER JOIN 
    InterestMax_Min AS t2 ON T1.FinanceSAPNumber = t2.FinanceSAPNumber
GROUP BY 
    T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, T1.ID_Date, 
    T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, T1.Loan_Balance, 
    T1.Cum_Balance, T1.Days_BetweenMAX, T1.Days_BetweenMIN
ORDER BY 
    T1.FinanceSAPNumber;

我尝试添加IIF(FinanceSAPNumber> FinanceSAPNumber,然后我收到了一个聚合函数错误

1 个答案:

答案 0 :(得分:0)

我无法保证它的效率,但这样做:

SELECT T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, T1.ID_Date, T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, T1.Loan_Balance, T1.Cum_Balance, T1.Days_BetweenMAX, T1.Days_BetweenMIN, IIf([t1.Days_BetweenMax]>0,Sum(CCur([t1.Cum_Balance]*[t1.InterestRate]/365*[t1.Days_BetweenMax])),"0") AS IntMax, IIf([t1.Days_BetweenMin]>0,Sum(CCur([t1.Loan_Balance]*[t1.InterestRate]/365*[t1.Days_BetweenMin])),"0") AS IntMin, IIf([t1.Days_BetweenMin]=0,IIf([t1.Days_BetweenMax]=0,Sum(CCur([t1.Loan_Balance]*[t1.InterestRate]/365*([t1.Pstng_Date]-[t1.ID_Date])))),"0") AS IntMin1, IIf([t1.Days_BetweenMin]=0,IIf([t1.Days_BetweenMax]=0,Sum(CCur([t1.Cum_Balance]*[t1.InterestRate]/365*([t1.Today_Date]-[t1.Pstng_Date])))),"0") AS IntMax1
FROM InterestMax_Min AS T1 
inner join
(
    select customer, count(*) count
    from interestMax_Min
    group by customer
) cnt on cnt.customer = t1.customer
INNER JOIN InterestMax_Min AS t2 ON T1.FinanceSAPNumber = t2.FinanceSAPNumber
where cnt.count < 3
GROUP BY T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, T1.ID_Date, T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, T1.Loan_Balance, T1.Cum_Balance, T1.Days_BetweenMAX, T1.Days_BetweenMIN
union all
SELECT T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, T1.ID_Date, T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, T1.Loan_Balance, T1.Cum_Balance, 0, 0 IIf([t1.Days_BetweenMax]>0,Sum(CCur([t1.Cum_Balance]*[t1.InterestRate]/365*[t1.Days_BetweenMax])),"0") AS IntMax, IIf([t1.Days_BetweenMin]>0,Sum(CCur([t1.Loan_Balance]*[t1.InterestRate]/365*[t1.Days_BetweenMin])),"0") AS IntMin, IIf([t1.Days_BetweenMin]=0,IIf([t1.Days_BetweenMax]=0,Sum(CCur([t1.Loan_Balance]*[t1.InterestRate]/365*([t1.Pstng_Date]-[t1.ID_Date])))),"0") AS IntMin1, IIf([t1.Days_BetweenMin]=0,IIf([t1.Days_BetweenMax]=0,Sum(CCur([t1.Cum_Balance]*[t1.InterestRate]/365*([t1.Today_Date]-[t1.Pstng_Date])))),"0") AS IntMax1
FROM InterestMax_Min AS T1 
inner join
(
    select customer, count(*) count
    from interestMax_Min
    group by customer
) cnt on cnt.customer = t1.customer
INNER JOIN InterestMax_Min AS t2 ON T1.FinanceSAPNumber = t2.FinanceSAPNumber
where cnt.count >= 3
GROUP BY T1.Customer, T1.FinanceSAPNumber, T1.InterestRate, T1.ID_Date, T1.Today_Date, T1.Pstng_Date, T1.SumOfAmount, T1.Loan_Balance, T1.Cum_Balance, T1.Days_BetweenMAX, T1.Days_BetweenMIN