我在sql server中有以下查询:
SELECT A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
,A.ANNUAL_STATEMENT_ID
,CASE WHEN DP_GROSS_DEPOSIT > 0 THEN DP_DEPOSIT_DATE
ELSE A.VALUATION_DATE
END AS VALUATION_DATE
,case when DP_GROSS_DEPOSIT < 0 THEN DP_GROSS_DEPOSIT
when DP_GROSS_DEPOSIT > 0 THEN DP_GROSS_DEPOSIT
WHEN DP_CURRENT_INTEREST_AMOUNT> 0 THEN DP_CURRENT_INTEREST_AMOUNT
WHEN WD_GROSS_WITHDRAWAL>0 THEN WD_GROSS_WITHDRAWAL * -1
WHEN IN_CURRENT_INTEREST_AMOUNT>0 THEN IN_CURRENT_INTEREST_AMOUNT
ELSE 0
END AS MPR
,B.IN_CURRENT_INTEREST_RATE AS IN_CURRENT_INTEREST_RATE
,IN_CURRENT_INTEREST_AMOUNT
,DP_GROSS_DEPOSIT
,0.00 DP_CURRENT_INTEREST_AMOUNT
,DP_DEPOSIT_DATE
,WD_GROSS_WITHDRAWAL
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY AS A
INNER JOIN (select C.VALUATION_DATE, C.ANNUAL_STATEMENT_ID, MAX(C.IN_CURRENT_INTEREST_RATE) "IN_CURRENT_INTEREST_RATE"
from (SELECT DISTINCT A.VALUATION_DATE, A.ANNUAL_STATEMENT_ID, B.IN_CURRENT_INTEREST_RATE, A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY AS A
INNER JOIN ( SELECT DISTINCT IN_CURRENT_INTEREST_RATE, ANNUAL_STATEMENT_ID, VALUATION_DATE, BENEFIT_SEQ, DP_GROSS_DEPOSIT, ANNUAL_STATEMENT_MPR_ACTIVITY_ID
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY
WHERE ANNUAL_STATEMENT_ID = 168811
AND (IN_CURRENT_INTEREST_AMOUNT > 0
OR DP_GROSS_DEPOSIT > 0
OR WD_GROSS_WITHDRAWAL > 0)) AS B ON A.ANNUAL_STATEMENT_ID = B.ANNUAL_STATEMENT_ID AND A.VALUATION_DATE = B.VALUATION_DATE AND A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID = B.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
WHERE A.ANNUAL_STATEMENT_ID = 168811) AS C
GROUP BY C.VALUATION_DATE, C.ANNUAL_STATEMENT_ID ) AS B ON A.VALUATION_DATE=B.VALUATION_DATE
AND A.ANNUAL_STATEMENT_ID=B.ANNUAL_STATEMENT_ID
--AND A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID = B.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
WHERE A.ANNUAL_STATEMENT_ID = 168811 AND ((IN_CURRENT_INTEREST_AMOUNT > 0 AND
B.VALUATION_DATE > 20190220)
OR (DP_DEPOSIT_DATE >= 20190220) OR WD_GROSS_WITHDRAWAL > 0)
UNION SELECT A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
,A.ANNUAL_STATEMENT_ID
,CASE WHEN DP_GROSS_DEPOSIT > 0 THEN DP_DEPOSIT_DATE
ELSE A.VALUATION_DATE
END AS VALUATION_DATE
,case when DP_GROSS_DEPOSIT < 0 THEN DP_GROSS_DEPOSIT
when DP_GROSS_DEPOSIT > 0 THEN DP_GROSS_DEPOSIT
WHEN DP_CURRENT_INTEREST_AMOUNT> 0 THEN DP_CURRENT_INTEREST_AMOUNT
WHEN WD_GROSS_WITHDRAWAL>0 THEN WD_GROSS_WITHDRAWAL * -1
WHEN IN_CURRENT_INTEREST_AMOUNT>0 THEN IN_CURRENT_INTEREST_AMOUNT
ELSE 0
END AS MPR
,B.IN_CURRENT_INTEREST_RATE AS IN_CURRENT_INTEREST_RATE
,IN_CURRENT_INTEREST_AMOUNT
,0.00 DP_GROSS_DEPOSIT
,DP_CURRENT_INTEREST_AMOUNT
,DP_DEPOSIT_DATE
,WD_GROSS_WITHDRAWAL
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY AS A
INNER JOIN (select C.VALUATION_DATE, C.ANNUAL_STATEMENT_ID, MAX(C.IN_CURRENT_INTEREST_RATE) "IN_CURRENT_INTEREST_RATE"
from (SELECT DISTINCT A.VALUATION_DATE, A.ANNUAL_STATEMENT_ID, B.IN_CURRENT_INTEREST_RATE, A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY AS A
INNER JOIN ( SELECT DISTINCT IN_CURRENT_INTEREST_RATE, ANNUAL_STATEMENT_ID, VALUATION_DATE, BENEFIT_SEQ, DP_GROSS_DEPOSIT, ANNUAL_STATEMENT_MPR_ACTIVITY_ID
FROM PANST_ANNUAL_STATEMENT_MPR_ACTIVITY
WHERE ANNUAL_STATEMENT_ID = 168811
AND (IN_CURRENT_INTEREST_AMOUNT > 0
-- OR DP_GROSS_DEPOSIT > 0
OR WD_GROSS_WITHDRAWAL > 0)) AS B ON A.ANNUAL_STATEMENT_ID = B.ANNUAL_STATEMENT_ID AND A.VALUATION_DATE = B.VALUATION_DATE AND A.ANNUAL_STATEMENT_MPR_ACTIVITY_ID = B.ANNUAL_STATEMENT_MPR_ACTIVITY_ID
WHERE A.ANNUAL_STATEMENT_ID = 168811) AS C
GROUP BY C.VALUATION_DATE, C.ANNUAL_STATEMENT_ID ) AS B ON A.VALUATION_DATE=B.VALUATION_DATE
AND A.ANNUAL_STATEMENT_ID=B.ANNUAL_STATEMENT_ID
WHERE A.ANNUAL_STATEMENT_ID = 168811 AND ((IN_CURRENT_INTEREST_AMOUNT > 0 AND
B.VALUATION_DATE > 20190220)
OR (DP_CURRENT_INTEREST_AMOUNT > 0 AND DP_DEPOSIT_DATE >= 20190220) OR WD_GROSS_WITHDRAWAL > 0)
此查询生成以下结果:
我想要的结果集应该是:
在计算总和(mpr)和总和(dp总额存款)列后,每个估价日期列的值只能有一行,年度报表、mpr活动、id列的值应取最大值(mpr)。我试了很多,但我觉得不可能。我说得对吗?
1条答案
按热度按时间6pp0gazn1#
如果两个突出显示的记录不是来自同一个联合部分(一个来自联合上面的select,另一个来自联合下面的select),那么向每个查询添加聚合函数将不起作用。考虑删除union并在一个select语句中组合这两个条件,或者在该语句周围 Package 一个cte并对该结果进行聚合。
此查询将聚合联合中两个选择的值,并使用rownumber字段(其中rownumber=1是最高mpr值)选择最高mpr值的年度报表活动id值(考虑将CTE重命名为您上下文中更有意义的内容)