2012-01-17 2 views
2

참고 : 비슷한 스레드를 읽었지만이 쿼리에는 도움이되지 않았습니다.데이터 형식을 varchar를 float로 변환하는 중 오류가 발생했습니다. ISNULL 관련

나는 실제 문제와 유사한 다음 코드를 재현했습니다. 설득력있는 조언이없는 한 이미 데이터베이스에서 사용 되었기 때문에이 구문을 사용해야합니다. 논리가 잘못되었습니다.

내 실제 문제는이 쿼리는 의 시간은 대부분이지만 특정 시간에는 실패합니다. 조사 후 나는 문제가 어떤 범주가 모두 널 (null), 0,0 또는 두 값 NULL 값 경우는, ISNULL 그들에게 문자열을하게되기 때문에, 따라서 나는

오류가

ISNULL(amount,0) 

것을 발견 변환 데이터 유형 VARCHAR는

다음

댓글 내 테스트 코드입니다 떠오르는

create table #table1 (
id int not null primary key identity, 
category varchar(10), 
amount float null 
) 

insert into #table1 values('A',23) 
insert into #table1 values('A',23) 
insert into #table1 values('B',NULL) 
insert into #table1 values('B',0) 
insert into #table1 values('C',NULL) 
insert into #table1 values('C',NULL) 
insert into #table1 values('D',0) 
insert into #table1 values('D',0) 

select * from #table1 -- works 

select category, sum1 -- works 
from 
(select category, SUM(Round(ISNULL(amount,0),0)) as Sum1 from #table1 
group by category) D 

select category, sum2 = -- does not work 
case Sum1 
    when 'A' then Sum1 * 1 -- my problem is here 
    when 'B' then Sum1 * 2 -- this is the logic in my actual code 
    when 'C' then Sum1 * 3 -- would like to make this query work 
    when 'D' then Sum1 * 4 
    else Sum1 
end 
from 
(select category, SUM(Round(ISNULL(amount,0),0)) as Sum1 from #table1 
group by category) D 

내가 뭘 잘못 했니?

답변

3

"case sum1"이 아니라 "case category"가되어야합니까?

변경하면 쿼리가 예상대로 작동합니다.

+0

SUM1. 감사. 이것은 대답이다. –

2

사건이 카테고리 볼 필요가없는 나는 실제 코드에서 만든 정확한 실수를했다

select category, sum2 = -- does not work 
case category 
    when 'A' then Sum1 * 1 -- my problem is here 
    when 'B' then Sum1 * 2 -- this is the logic in my actual code 
    when 'C' then Sum1 * 3 -- would like to make this query work 
    when 'D' then Sum1 * 4 
    else Sum1 
end 
from 
(select category, SUM(Round(ISNULL(amount,0),0)) as Sum1 from #table1 
group by category) D 
관련 문제