以下脚本为我提供了送货地址信息。但是由于几年来系统设置不良,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'
答案 0 :(得分: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会是什么样子。
您甚至可以按计数对结果进行排序。
答案 1 :(得分:0)
最简单的方法是将ad.name1
替换为
( SELECT name1 FROM
( SELECT name1, COUNT(*) FROM oes_address GROUP BY name1 ORDER BY 2 DESC )
WHERE ROWNUM = 1
)
这将计算整个表中name1的所有出现次数,计算它们并按此计数排序。所以最常见的名字1在第一行。选中此行。
答案 2 :(得分: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