Sql oracle-max与case

Sql oracle-max与case,sql,oracle,Sql,Oracle,我想你会明白我想做什么。获取特定id的max seq_no。但是我得到错误“not a single group子句” 此select语句是较大insert的一部分 谢谢 更新:这是全部声明 select case when cntrctr_lcns_seq_no is null then 1 else max(cntrctr_lcns_seq_no)

我想你会明白我想做什么。获取特定id的max seq_no。但是我得到错误“not a single group子句”

此select语句是较大insert的一部分

谢谢

更新:这是全部声明

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

max-aggregate函数将忽略null值,因此您不需要case语句或group-by,因为您希望最大值覆盖整个返回集

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);
试试这个:

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

编辑:

您得到的错误是因为您没有在group by子句中包含第三方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 
如果您真的想将此语法用于不同的(更复杂的查询)。此外,还应在GROUPBY子句中添加第三方id

但是max已经忽略了nulls,所以像这样的查询不是更能描述您要做的事情吗

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
我想你想要:

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

(如果您愿意,也可以使用Oracle的
nvl
而不是ANSI的
coalesce

不完全是这样,如果表中还没有id,我实际上希望返回1。不确定coalsce做什么。。。但是如果我从nuwmsweb.cntrctr\u lcns\u info中选择coalesce(max(cntrctr\u lcns\u seq\u no)+1,1)作为cntrctr\u lcns\u seq\u no,其中第三方id=thirdPartyId注意,
+1
我尝试过,但我得到了一个无法插入错误。换言之,当该id尚未在列表中时,这将返回大于1的空ratertable@kralco626:您得到“无法插入错误”是什么意思?语句中没有
insert
。您正在执行的查询的其余部分是什么?是的,我喜欢这样。我一定是做错了什么,因为我一直收到一个无法插入的空错误
select coalesce (max(cntrctr_lcns_seq_no),1) as cntrctr_lcns_seq_no
  from nuwmsweb.cntrctr_lcns_info
 where third_party_id = thirdPartyId