select
case when substring(Tilno from 1 for 1) = '1' then cast(sum(COALESCE(amnt,0)) as numeric(13))
else 0
end "1번계",
case when substring(Tilno from 1 for 1) = '2' then cast(sum(COALESCE(amnt,0)) as numeric(13))
else 0
end "2번계",
case when substring(Tilno from 1 for 1) = '1' then 0
when substring(Tilno from 1 for 1) = '2' then 0
else cast(sum(COALESCE(amnt,0)) as numeric(13))
end "기타계",
cast(sum(COALESCE(amnt,0)) as numeric(13)) "총 계"
from tr110t
Tilno의 값에 따라 계를 내고 싶습니다.
위처럼 1번계, 2번계, 1번과 2번을 제외한 기타계, 전체를 합한 총계를 내고 싶습니다.
물론 한줄로요...
위문장은 Group By 절이 없어 에러를 냅니다.
Group by substring(Tilno from 1 for 1) 이렇게 하면 여러줄로 늘어나게 되지요...
max(substring(Tilno from 1 for 1)) = '1' ....
Group by substring(Tilno from 1 for 1) 이렇게 하면 한줄로 되는데...문제는 값이 정확하지 않다는 겁니다
자... 이럴때요...
어떻게 위의 SQL문으로 한줄의 합계를 나타낼 수 있을까요?
이것이 안되면 또 최대한으로 불러와서 루프를 돌려야 합니다....임시 테이블 만들고..더하고...지겨워라..
|