2011-03-07 59 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的max seq_no。但是我得到错误“不是一个小组小组条款”。甲骨文 - 最大案例

此选择语句是较大插入的一部分。

谢谢!

更新:这是整个声明

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

MAX聚合函数忽略空值,所以你不需要用你想要的最大值在整个返回集case语句或组。

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

不完全,我实际上想要1返回,如果表中还没有一个id。 – kralco626 2011-03-07 14:59:00

+0

够公平,只需将它包裹在一起。看我的编辑。 – 2011-03-07 15:04:28

+0

不确定什么样的coalsce ......但是它起作用,如果我从nuwmsweb.cntrctr_lcns_info中选择coalesce(max(cntrctr_lcns_seq_no)+ 1,1)作为cntrctr_lcns_seq_no,其中third_party_id = thirdPartyId'注意我试过的'+ 1' – kralco626 2011-03-08 11:21:44

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还没有出现在表中时,这是返回null比1 – kralco626 2011-03-07 15:00:37

+0

@ kralco626:你对你的意思是“你不能插入错误”?你在声明中没有'INSERT'。你正在执行的查询的其余部分是什么? – Lamak 2011-03-07 15:02:55

0

编辑:

你所得到的错误是因为你不包括group by子句中third_party_id。

的Try ...

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已经忽略了空值,所以这样的查询不会更能描述你正在做什么吗?

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 2011-03-07 15:03:51

1

我想你想:

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

(或者你可以使用Oracle的nvl而不是ANSI的​​3210如果您愿意)。