×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Find a value from a max count in a sub query

Find a value from a max count in a sub query

Find a value from a max count in a sub query

(OP)

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


 

RE: Find a value from a max count in a sub query

I haven't tested this, but I think it ought to give you what you want:

CODE

SELECT cd.region_code, cd.city, v.code_count
  FROM customer_dim cd
 INNER JOIN (SELECT city, max(n) AS code_count
               FROM (SELECT region_code, city, count(*) AS n
                       FROM customer_dim
                      GROUP BY region_code, city)
             )v
ON cd.city = v.city;

RE: Find a value from a max count in a sub query

Correction to the above:

CODE

SELECT cd.region_code, cd.city, v.code_count
  FROM customer_dim cd
 INNER JOIN (SELECT city, max(n) AS code_count
               FROM (SELECT region_code, city, count(*) AS n
                       FROM customer_dim
                      GROUP BY region_code, city)
               GROUP BY city) v
ON cd.city = v.city;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close