group-by

91zkwejq  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(416)

我在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)。我试了很多,但我觉得不可能。我说得对吗?

6pp0gazn

6pp0gazn1#

如果两个突出显示的记录不是来自同一个联合部分(一个来自联合上面的select,另一个来自联合下面的select),那么向每个查询添加聚合函数将不起作用。考虑删除union并在一个select语句中组合这两个条件,或者在该语句周围 Package 一个cte并对该结果进行聚合。

;WITH CTE1 AS (
    /* Replace this test data with your query */
    SELECT
        2122807 AS ANNUAL_STATEMENT_MPR_ACTIVITY_ID,
        168811 AS ANNUAL_STATEMENT_ID,
        20190220 AS VALUATION_DATE,
        -9763.03 AS MPR,
        0 AS IN_CURRENT_INTEREST_RATE,
        0 AS IN_CURRENT_INTEREST_AMOUNT,
        -9763.03 AS DP_GROSS_DEPOSIT,
        0 AS DP_CURRENT_INTEREST_AMOUNT,
        20190220 AS DP_DEPOSIT_DATE,
        0 AS WD_GROSS_WITHDRAWAL
    UNION
    SELECT
        2122808 AS ANNUAL_STATEMENT_MPR_ACTIVITY_ID,
        168811 AS ANNUAL_STATEMENT_ID,
        20190220 AS VALUATION_DATE,
        20549.50 AS MPR,
        0 AS IN_CURRENT_INTEREST_RATE,
        0 AS IN_CURRENT_INTEREST_AMOUNT,
        20549.50 AS DP_GROSS_DEPOSIT,
        0 AS DP_CURRENT_INTEREST_AMOUNT,
        20190220 AS DP_DEPOSIT_DATE,
        0 AS WD_GROSS_WITHDRAWAL
),
CTE2 AS (
SELECT
    ROW_NUMBER() OVER(PARTITION BY VALUATION_DATE ORDER BY MPR DESC) AS RowNumber, 
    ANNUAL_STATEMENT_MPR_ACTIVITY_ID,
    VALUATION_DATE
FROM CTE1
)
SELECT
    C2.ANNUAL_STATEMENT_MPR_ACTIVITY_ID,
    C1.ANNUAL_STATEMENT_ID,
    C1.VALUATION_DATE,
    SUM(C1.MPR) AS MPR,
    C1.IN_CURRENT_INTEREST_RATE,
    C1.IN_CURRENT_INTEREST_AMOUNT,
    SUM(C1.DP_GROSS_DEPOSIT) AS DP_GROSS_DEPOSIT,
    C1.DP_CURRENT_INTEREST_AMOUNT,
    C1.DP_DEPOSIT_DATE,
    C1.WD_GROSS_WITHDRAWAL
FROM CTE1 AS C1
    JOIN CTE2 AS C2 ON
        C1.VALUATION_DATE = C2.VALUATION_DATE
        AND C2.RowNumber = 1
GROUP BY
    C2.ANNUAL_STATEMENT_MPR_ACTIVITY_ID,
    C1.ANNUAL_STATEMENT_ID,
    C1.VALUATION_DATE,
    C1.IN_CURRENT_INTEREST_RATE,
    C1.IN_CURRENT_INTEREST_AMOUNT,
    C1.DP_CURRENT_INTEREST_AMOUNT,
    C1.DP_DEPOSIT_DATE,
    C1.WD_GROSS_WITHDRAWAL

此查询将聚合联合中两个选择的值,并使用rownumber字段(其中rownumber=1是最高mpr值)选择最高mpr值的年度报表活动id值(考虑将CTE重命名为您上下文中更有意义的内容)

相关问题