2017-02-15 1 views
1

입금을 날짜 범위 (1-7 일, 3-6 개월 등)로 그룹화하는 쿼리가 있으며 나는 희망했던대로 크게 작용합니다. 그러나 날짜 범위에서 소득이 없을 것으로 예상되는 행이 0 인 행을 반환하고 싶습니다.그룹에서 CASE 문으로 쿼리 WHEN에서 결과가 나타나지 않는 항목을 생성해야합니다.

같은 외모로 그룹 :

group by 

CASE WHEN timestampdiff(day,curdate(),data.duedate) between 0 and 7 then 1 
     WHEN timestampdiff(day,curdate(),data.duedate) between 8 and 14 then 2 
     WHEN timestampdiff(day,curdate(),data.duedate) between 15 and 30 then 3 
     WHEN timestampdiff(month,curdate(),data.duedate) between 1 and 2 then 4 
     WHEN timestampdiff(month,curdate(),data.duedate) between 2 and 3 then 5 
     WHEN timestampdiff(month,curdate(),data.duedate) between 3 and 6 then 6 
     WHEN timestampdiff(month,curdate(),data.duedate) between 6 and 12 then 7 
     WHEN timestampdiff(year,curdate(),data.duedate) between 1 and 2 then 8 
     WHEN timestampdiff(year,curdate(),data.duedate) between 2 and 3 then 9 
     WHEN timestampdiff(year,curdate(),data.duedate) between 3 and 4 then 10 
     WHEN timestampdiff(year,curdate(),data.duedate) between 5 and 6 then 11 
     WHEN timestampdiff(year,curdate(),data.duedate) >= 7 then 12 

이가 나에게 정확한 금액을 줄 것이다,하지만 난 나에게주는 코드를 강제로 원하는에서 제대로 작동하는 0 그래서 현재이 얻을 :

1 300000 
5 150000 
8 300000 

은 내가 실제로 원하는 것은 이것이다 :

1 300000 
2 0 
3 0 
4 0 
5 150000 
6 0 
7 0 
8 300000 
etc. 

이 전체 쿼리입니다 - 나는를 사용하여 시도했다 n IFNULL() 성공했지만 성공하지 못했습니다 :

select 

sum(data.principaldue+data.interestdue) as m 

from 
(select 
    la.id 
    ,rep.duedate 
    ,rep.PRINCIPALDUE 
    ,rep.INTERESTDUE 
    from repayment rep 
    join loanaccount la on la.ENCODEDKEY = rep.PARENTACCOUNTKEY 
    join loanproduct lp on lp.ENCODEDKEY = la.PRODUCTTYPEKEY 

group by 

CASE WHEN timestampdiff(day,curdate(),data.duedate) between 0 and 7 then 1 
     WHEN timestampdiff(day,curdate(),data.duedate) between 8 and 14 then 2 
     WHEN timestampdiff(day,curdate(),data.duedate) between 15 and 30 then 3 
     WHEN timestampdiff(month,curdate(),data.duedate) between 1 and 2 then 4 
     WHEN timestampdiff(month,curdate(),data.duedate) between 2 and 3 then 5 
     WHEN timestampdiff(month,curdate(),data.duedate) between 3 and 6 then 6 
     WHEN timestampdiff(month,curdate(),data.duedate) between 6 and 12 then 7 
     WHEN timestampdiff(year,curdate(),data.duedate) between 1 and 2 then 8 
     WHEN timestampdiff(year,curdate(),data.duedate) between 2 and 3 then 9 
     WHEN timestampdiff(year,curdate(),data.duedate) between 3 and 4 then 10 
     WHEN timestampdiff(year,curdate(),data.duedate) between 5 and 6 then 11 
     WHEN timestampdiff(year,curdate(),data.duedate) >= 7 then 12 

END 

Order by 

CASE WHEN timestampdiff(day,curdate(),data.duedate) between 0 and 7 then 1 
     WHEN timestampdiff(day,curdate(),data.duedate) between 8 and 14 then 2 
     WHEN timestampdiff(day,curdate(),data.duedate) between 15 and 30 then 3 
     WHEN timestampdiff(month,curdate(),data.duedate) between 1 and 2 then 4 
     WHEN timestampdiff(month,curdate(),data.duedate) between 2 and 3 then 5 
     WHEN timestampdiff(month,curdate(),data.duedate) between 3 and 6 then 6 
     WHEN timestampdiff(month,curdate(),data.duedate) between 6 and 12 then 7 
     WHEN timestampdiff(year,curdate(),data.duedate) between 1 and 2 then 8 
     WHEN timestampdiff(year,curdate(),data.duedate) between 2 and 3 then 9 
     WHEN timestampdiff(year,curdate(),data.duedate) between 3 and 4 then 10 
     WHEN timestampdiff(year,curdate(),data.duedate) between 5 and 6 then 11 
     WHEN timestampdiff(year,curdate(),data.duedate) >= 7 then 12 

END 
+0

당신이 바로 가입 할 수 숫자 표가 있나요? – user5226582

+0

슬프게도이 스키마에는 숫자 테이블이 없으며 테이블을 만들 권한도 없습니다. – monkeyb33f

+0

이에 대한 테이블 변수를 사용하십시오. – user5226582

답변

1

이것은 완전한 대답은 아니지만 의견이 너무 큽니다.

수있는 임시 테이블이 유용 할 수있다 :

MySql temporary tables :

CREATE TEMPORARY TABLE TempTable (num int); 
INSERT INTO TmpTable VALUES(1,2,3,4,5,6,7,8,9,10,11,12 ...); 

그런 다음 바로 확인 누락 된 값이 포함되어 있는지 확인하기 위해이 테이블에 가입 할 수있다.

results(num, val): 
1 300000 
5 150000 
8 300000 

이 원하는 출력 발생한다 :이 도움이

SELECT numbers.num, COALESCE(results.val, 0) as val 
FROM results RIGHT JOIN TempTable numbers on results.num = numbers.num 
WHERE numbers.num <= 12 --or other max number 

1 300000 
2 0 
... 
5 150000 
... 

희망을

당신이 가지고 말할 수 있습니다.

편집 :

SELECT @row := @row + 1 as row, t.* 
FROM some_table t, (SELECT @row := 0) r 

some_table 충분한 행이있는 테이블 : 당신이 연속 정수를 선택하는 해결 방법을 찾아, 임시 테이블을 만들 수있는 권한, for example이없는 경우

.

아마 위에 N을 사용하십시오. 당신이 많은 번호가 필요하지 않은 경우

또 다른 더러운 해결 방법은 충분 있습니다

SELECT 1 num 
UNION 
SELECT 2 num 
UNION 
... 

편집 :

약간 정돈 해결 방법 :

SELECT * FROM (VALUES (1), (2), (3), ...) x(i) 
+0

안녕하세요 - 귀하의 답변을 주셔서 감사합니다,하지만 난 스키마에서 임시 테이블을 만들 관련 권한이 없어 걱정됩니다. – monkeyb33f

+0

게시 한 후 내 의견을 보았습니다. 다른 것을 생각할 수 있다면 대답을 업데이트 할 것입니다. – user5226582

+0

업데이트했습니다. – user5226582

관련 문제