以下代码计算NAME1发生的次数。
是否可以将此方法应用于每个ad.ZIP,而不是整个报告。
理想情况下,我想针对每个ZIP的所有行显示最大的NAME1。
select name1, count(name1) occurances, town, zip, country, count_pallet, 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%'
)
group by name1, town, zip, country, count_pallet, del_units, del_discs
答案 0 :(得分:1)
如果您需要不同类型的聚合,可以使用分析函数以避免对同一数据进行多次查询。
看看: http://www.orafaq.com/node/55
一些数据:
create table t1 (zip number, name varchar2(100));
insert into t1 (zip, name) values (101, 'Maria');
insert into t1 (zip, name) values (101, 'Ana');
insert into t1 (zip, name) values (101, 'Ana');
insert into t1 (zip, name) values (101, 'Ana');
insert into t1 (zip, name) values (101, 'Ema');
insert into t1 (zip, name) values (101, 'Ema');
insert into t1 (zip, name) values (102, 'Maria');
insert into t1 (zip, name) values (102, 'Ana');
insert into t1 (zip, name) values (102, 'Ana');
insert into t1 (zip, name) values (102, 'Ana');
insert into t1 (zip, name) values (102, 'Ema');
insert into t1 (zip, name) values (102, 'Ema');
insert into t1 (zip, name) values (102, 'Joana');
insert into t1 (zip, name) values (102, 'Joana');
insert into t1 (zip, name) values (102, 'Joana');
insert into t1 (zip, name) values (102, 'Joana');
insert into t1 (zip, name) values (102, 'Joana');
聚合示例:
col name format a10
select zip, name
,count(*) a
,count(*) over (partition by zip) b
,count(distinct zip) over () c
,count(*) over () d
from t1
group by zip, name;
ZIP NAME A B C D
---------- ---------- ---------- ---------- ---------- ----------
101 Ana 3 3 2 7
101 Ema 2 3 2 7
101 Maria 1 3 2 7
102 Ana 3 4 2 7
102 Ema 2 4 2 7
102 Joana 5 4 2 7
102 Maria 1 4 2 7
A - Normal count(*) -> "101 and Ana" appear 3 times whereas "101 and Ema" appear only 2 times
B - 101 zip appears for 3 different names, and 102 zip appears for 4 different names
C - over () means over the whole data -> 2 distinct zip values
D - number of rows returned over the whole data -> 7 rows
如果可以适用,你最好自己了解一下。