Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Max value of a count 1

Status
Not open for further replies.

twifosp

Programmer
Jul 25, 2003
186
US
I have some data that looks like this:

Code:
region_code city zip 
AAA     Houston 77076
AAA     Houston 77024
AAB     Houston 77024

The region_code is entered manually by a multitude of different people. It should always be the same thing for each city, but some times it gets messed up.

I am trying to write a query that will let me find the cities that have more than one region code per city, and then update them based on which region code appears more than the other region code.

So far I have:

Code:
select city, count(*) as n
  from (
    select distinct pid, city, region_code
    from  customer_dim
   ) a
  group by city
  having count(*) > 1
  order by count(*) desc

Which finds all the cities that have more than one region code assigned to them per city.

The below code is my attempt to find the max value for each city and return its region code, but of course it doesn't work because I am not doing the sub queries right. Can someone help?

Code:
select  region_code, city
from  (
     select  region_code, city, count(*) as n
  from  customer_dim
  where city = 'HOUSTON'
  group by region_code, city) a
join  (
  select max(n) as maxn
  from  (
  select  region_code, city, count(*) as n
  from  customer_dim
  group by region_code, city) aa
  ) b
on a.city = b.city
and b.maxn = a.n


 
Twifosp,

There are many ways one can solve your need. Here is one that comes to mind:
Code:
select * from customer_dim;

PID CITY    REGION_CODE
--- ------- -----------
AAA Houston 77076
AAA Houston 77024
AAB Houston 77024
AAC Dallas  75201

select pid,city,cnt
  from (select pid,city,count(*)cnt
          from customer_dim
          where city = 'Houston'
          group by pid,city
         having count(*) > 1) x
 where cnt =
       (select max(cnt)
          from (select count(*)cnt
                  from customer_dim
                 where city = 'Houston'
                 group by pid,city
                )
        );

PID CITY           CNT
--- ------- ----------
AAA Houston          2
Let us know if this solution works for you.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
SantaMufasa,

Thank you for the response. It helps me out, but I should have been more clear about my data.

I need to look at a data set that has many cities.

Essentially I want to return something like this:

Code:
City    dstnct_rgn_codes max_rgn_code  max_rgn_code_count
houston    2                   aaa           47
new york   1                   abc           34
boston     4                   efg           12

This way I can use the data set to see which cities are assigned to more than one region code and make a database wide update.
 
I assume that, by maximum region_code, what you really mean is the most commonly occurring one. Something of this sort is probably what you need.

Code:
select *
from
(select city, region_code, rec_num, rank() over (partition by city order by rec_num desc) as ranking 
from 
(select city,
        region_code,
        count(*) as rec_num
   from customer_dim
 group by city, region_code))
where ranking=1
order by city, region_code
 
Dagon -- Thank you. I did not know about the partition and ranking functions in Oracle. Good to learn something new.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top