首页 > 代码库 > case when 与group by 联用问题

case when 与group by 联用问题

今天遇到一个问题,以下是两种查询方式,但是查询出来的结果不一样,第二种查询出来的结果是正确的,如果将第一种查询的lendrequestId去掉,查询结果就会和第二种一样,这是为什么?

1.SELECT  rr.lendRequestId,
 (case when rr.repayState = ‘OVER_DUE_REPAID‘ then sum(rr.repayPrincipal) end) as 逾期还款本金
FROM dwloanrepayrecordfact rr
GROUP BY rr.lendRequestId
2.SELECT rr.lendRequestId, sum(rr.repayPrincipal)
FROM dwloanrepayrecordfact rr
WHERE rr.repayState = ‘OVER_DUE_REPAID‘
GROUP BY rr.lendRequestId

 

case when 与group by 联用问题