我可以在 sql 中进行两次分组吗?

can i do two times grouping in sql?

SELECT
     CASE WHEN "dbo"."ClaimDataFormat"."RELATION" = 'DAUGHTER'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'SON'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'SISTER'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'FATHER'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'MOTHER'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'WIFE'
          OR   "dbo"."ClaimDataFormat"."RELATION" = 'HUSBAND'
          THEN 'SPOUSE'
          ELSE 'SELF'
          END AS RELATION,

     sum("dbo"."ClaimDataFormat"."CLAIM_AMT") AS TOTAL_CLAIM_AMOUNT
FROM
     "dbo"."ClaimDataFormat"
GROUP BY
     "dbo"."ClaimDataFormat"."RELATION"

上面查询的结果集类似于下面,但我只想要两行,第一行添加 "SPOUSE" 的所有值,第二行只添加 "SELF" 的值为 1,689,292.0

RELATION TOTAL_CLAIM_AMOUNT
-------------------
SPOUSE  121724.0
SPOUSE  2440297.0
SPOUSE  145586.0
SPOUSE  853361.0
SELF    1689292.0
SPOUSE  1.0
SPOUSE  373190.0
SPOUSE  1984198.0

我需要帮助才能做到这一点。请建议我该怎么做

您可以像这样用外部 select 在同一列上应用相同的组来包装您的查询,您将得到结果 want.Also 您可以在其中使用 IN 运算符您 CASE 缩短查询。

 SELECT SUM(TOTAL_CLAIM_AMOUNT) AS TotalClaimAmount, Relation
 FROM
    (
      SELECT
         CASE WHEN "dbo"."ClaimDataFormat"."RELATION" IN ('DAUGHTER','SON','SISTER','FATHER','MOTHER','WIFE','HUSBAND')THEN 'SPOUSE'
              ELSE 'SELF'
          END AS RELATION,
          SUM("dbo"."ClaimDataFormat"."CLAIM_AMT") AS TOTAL_CLAIM_AMOUNT
     FROM
        "dbo"."ClaimDataFormat"
     GROUP BY
         "dbo"."ClaimDataFormat"."RELATION"
    )As innerData
  GROUP BY RELATION