简体   繁体   中英

Oracle SQL: Applying count to changing column value

The following code counts the amount of times NAME1 occurs.

Is there a way of applying this to each ad.ZIP and not to the whole report.

Ideally I would like to show the greatest occurring NAME1 against all of the lines per ZIP.

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

You can use Analytic Functions in order to avoid multiple queries at the same data if you need different types of aggregation.

Take a look: http://www.orafaq.com/node/55

Some data:

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');

Examples of aggregations:

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

You better see for yourself for understanding if this could apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM