oracle sql,将值更改为最常见的值

问题描述 投票:0回答:3

以下脚本为我提供了送货地址信息。但是由于几年来系统设置不良,NAME1(地址名称)有几种变化。有没有办法我可以更改NAME1以显示最常用的NAME1但保留所有其他行信息?

select ad.name1, 
       ad.town, 
       replace(ad.zip, ' ', '') zip, 
       ad.country, 
       (select sum(dp1.palette) 
          from oes_dpos dp1 
         where dp1.dheadnr = dh.dheadnr) count_pallet, 
       (select sum(dp2.delqty) 
          from oes_dpos dp2 
         where dp2.dheadnr = dh.dheadnr) del_units, 
       ((select sum(dp3.delqty) 
            from oes_dpos dp3 
           where dp3.dheadnr = dh.dheadnr) * sp.nrunits) del_discs 
  from oes_dhead dh, 
       oes_dpos dp, 
       oes_address ad, 
       oes_opos op, 
       SCM_PACKTYP sp 
 where dh.actshpdate >= '01-DEC-2013' 
   and dh.actshpdate - 1 < '30-NOV-2014' 
   and dh.shpfromloc = 'W' 
   and ad.key = dh.delnr 
   and ad.adr = dh.deladr 
   and dp.dheadnr = dh.dheadnr 
   and op.ordnr = dp.ordnr 
   and op.posnr = dp.posnr 
   and op.packtyp = sp.packtyp 
   and upper(ad.name1) not like '%SONY%' 
   and replace(ad.zip, ' ', '') = 'CO77DW'
sql oracle
3个回答
2
投票

是的,您需要计算NAME1中的出现次数

更改如下

    select name1, count(name1) occurances, town, zip, country, count_palle,del_units,del_discs from (   
select ad.name1, 
   ad.town, 
   replace(ad.zip, ' ', '') zip, 
   ad.country, 
   (select sum(dp1.palette) 
      from oes_dpos dp1 
     where dp1.dheadnr = dh.dheadnr) count_pallet, 
   (select sum(dp2.delqty) 
      from oes_dpos dp2 
     where dp2.dheadnr = dh.dheadnr) del_units, 
   ((select sum(dp3.delqty) 
        from oes_dpos dp3 
       where dp3.dheadnr = dh.dheadnr) * sp.nrunits) del_discs 


from oes_dhead dh, 
       oes_dpos dp, 
       oes_address ad, 
       oes_opos op, 
       SCM_PACKTYP sp 
 where dh.actshpdate >= '01-DEC-2013' 
   and dh.actshpdate - 1 < '30-NOV-2014' 
   and dh.shpfromloc = 'W' 
   and ad.key = dh.delnr 
   and ad.adr = dh.deladr 
   and dp.dheadnr = dh.dheadnr 
   and op.ordnr = dp.ordnr 
   and op.posnr = dp.posnr 
   and op.packtyp = sp.packtyp 
   and upper(ad.name1) not like '%SONY%' 
   and replace(ad.zip, ' ', '') = 'CO77DW'
   )
   group by name1, town, zip, country, count_palle,del_units,del_discs

请记住,我将您的查询用作子查询,因为我不知道您的DDL会是什么样子。

您甚至可以按计数对结果进行排序。


0
投票

最简单的方法是用ad.name1替换

( SELECT name1 FROM 
  ( SELECT name1, COUNT(*) FROM oes_address GROUP BY name1 ORDER BY 2 DESC )
  WHERE ROWNUM = 1
)

这将计算整个表中name1的所有出现次数,计算它们并按此计数排序。所以最常见的名称1在第一行。此行已被选中。


0
投票

我想你想要这样的东西:

with names as(
select 'street 1' name1 from dual
union all
select 'str 1' from dual
union all
select 'str. 1' from dual
union all
select 'street 1' from dual
union all
select 'str. 1' from dual
union all
select 'str. 1' from dual
union all
select 'street 1' from dual
union all
select 'str 1' from dual
union all
select 'street 1' from dual
union all
select 'street 1' from dual
union all
select 'str 1' from dual
union all
select 'str 1' from dual
union all
select 'street 1' from dual
union all
select 'str 1' from dual
union all
select 'str 1' from dual
union all
select 'street 1' from dual
union all
select 'str 1' from dual
union all
select 'str 1' from dual)
select name1
from(
select name1
from names
group by name1
order by count(*) desc)
where rownum=1
© www.soinside.com 2019 - 2024. All rights reserved.