2011-03-07 2 views
0
select case when cntrctr_lcns_seq_no is null 
            then 1 
            else max(cntrctr_lcns_seq_no) 
            end as cntrctr_lcns_seq_no 
            from nuwmsweb.cntrctr_lcns_info 
            where third_party_id = thirdPartyId 
            group by third_party_id 

나는 당신이 뭘하려고하는지 생각해. 특정 ID에 대해 최대 seq_no를 가져옵니다. 하지만 "단일 그룹 그룹 절이 아닙니다"라는 오류가 나타납니다.오라클 - 케이스 포함 최대

이 select 문은 더 큰 삽입 부분입니다.

감사합니다.

업데이트 : 이것은 당신이 당신이 전체 반환 설정을 통해 최대 원하는에 의한 경우 문 또는 그룹을 필요가 없습니다 최대 집계 함수가 null 값을 무시합니다

insert into nuwmsweb.CNTRCTR_LCNS_INFO 
    (third_party_id,cntrctr_lcns_seq_no,cntrctr_lcns_no, 
    lcns_st_cd,certfn_level_type_cd,cntrctr_type_cd,cut_tap_authy_ind, 
    stat_type_nm) 
    VALUES(thirdPartyId,(select max(case when cntrctr_lcns_seq_no is null 
            then 1 
            else cntrctr_lcns_seq_no 
            end) as cntrctr_lcns_seq_no 
            from nuwmsweb.cntrctr_lcns_info 
            where third_party_id = thirdPartyId 
            group by third_party_id 
            ), 
      licenseNumber,licenseState,licenseLevel,licenseType,cutTap,status); 

답변

1

전체 성명 .

select 
    coalesce(max(cntrctr_lcns_seq_no), 1) as cntrctr_lcns_seq_no 
from nuwmsweb.cntrctr_lcns_info 
where third_party_id = thirdPartyId 
+0

아직 테이블에 ID가없는 경우 실제로는 1을 반환하고 싶습니다. – kralco626

+0

충분히 공정한, 그것을 합병하여 포장하십시오. 내 편집을 참조하십시오. –

+0

coalsce가 무슨 일을하는지 잘 모르겠지만 ... 'coalesce (max (cntrctr_lcns_seq_no) + 1, 1)를 선택하면 nuwmsweb.cntrctr_lcns_info에서 cartrctr_lcns_seq_no로 third_party_id = thirdPartyId'를 메모하십시오.'+ 1'을 적어주세요. – kralco626

0

이 시도 :

select max(case when cntrctr_lcns_seq_no is null then 1 
     else cntrctr_lcns_seq_no end) as cntrctr_lcns_seq_no 
from nuwmsweb.cntrctr_lcns_info 
where third_party_id = thirdPartyId          
group by third_party_id 
+0

나는 오류에 삽입 할 수 없습니다. 다른 말로하면,이 ID가 아직 테이블에없는 경우 1보다 큰 null을 반환합니다. – kralco626

+0

@ kralco626 : "오류로 삽입 할 수 없습니다"라는 메시지가 무엇입니까?라는 문구에는 INSERT가 없습니다. 실행중인 나머지 쿼리는 무엇입니까? – Lamak

0

편집 :

당신이 조항에 의해 그룹의 third_party_id을 포함하지 않기 때문에 당신이 점점 오류입니다.

시도 ...

select max(case when cntrctr_lcns_seq_no is null 
            then 1 
            else cntrctr_lcns_seq_no 
            end) as cntrctr_lcns_seq_no 
            from nuwmsweb.cntrctr_lcns_info 
            where third_party_id = thirdPartyId 
            group by third_party_id 

당신이 정말로 다른 (더 복잡한 쿼리)이 구문을 사용합니다. 또한 group by 절에 third_party_id를 추가해야합니다.

하지만 max는 이미 null을 무시하므로 이와 같은 쿼리가 사용자가 수행하려고 시도하는 것을 더 잘 설명하지는 않습니까?

select third_party_id, max(nvl(cntrctr_lcns_seq_no,1)) 
     from nuwmsweb.cntrctr_lcns_info 
     where third_party_id = thirdPartyId 
     group by third_party_id 
+0

나중에 나는 그것을 좋아한다. 널 오류를 삽입 할 수 없기 때문에 뭔가 잘못하고있다. – kralco626

1

난 당신이 원하는 생각 :

select coalesce (max(cntrctr_lcns_seq_no),1) as cntrctr_lcns_seq_no 
    from nuwmsweb.cntrctr_lcns_info 
where third_party_id = thirdPartyId 

가 (또는 당신이 사용할 수있는 오라클의 nvl 대신 ANSI의 coalesce 당신이 선호하는 경우).