Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Query Problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-04, 12:18
abe6162 abe6162 is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
Query Problem

I have a table with columns -- ims_num, last_name, pres_num, territory_num

I need retrieve the max pres_num grouped by territory

I got so far

select max(pres_num) from table
group by territory_num

I need to show all the data -- ims_num, last_name, pres_num, territory_num
Reply With Quote
  #2 (permalink)  
Old 06-11-04, 23:24
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
select *
from tableA ta
where pres_num = (select max(presnum) from tableA where territory_num = ta.territory_num)
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 06-12-04, 11:23
abe6162 abe6162 is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
I tired this, but it didn't work.

The problem is the the max(pres_num) of one territory can be the pres_num in another terriotory, and the pres_num might not be the max(pres_num) in that territory.

I hope that made sense.
Thanks so much for helping!
Reply With Quote
  #4 (permalink)  
Old 06-12-04, 19:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,555
"I hope that made sense."

nope, it didn't

it made things a lot worse

perhaps you could show some sample rows, and then an example of what the result set should be
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #5 (permalink)  
Old 06-13-04, 01:16
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Consider the following example.

Code:
SQL> select * from a; NAME DEPT AMT ---------- ---------- ---------- EMPA DEPTA 80000 EMPB DEPTA 60000 EMPC DEPTB 80000 EMPD DEPTB 60000 SQL> select dept, max(amt) 2 from a 3 group by dept; DEPT MAX(AMT) ---------- ---------- DEPTA 80000 DEPTB 80000 SQL> select * 2 from a a1 3 where a1.amt = (select max(amt) from a where dept = a1.dept); NAME DEPT AMT ---------- ---------- ---------- EMPA DEPTA 80000 EMPC DEPTB 80000 SQL> select a1.name, V.* 2 from a a1 3 INNER JOIN 4 (select dept, max(amt) MAX_AMT 5 from a 6 group by dept) V ON 7 V.dept = a1.dept AND 8 V.MAX_AMT = a1.amt; NAME DEPT MAX_AMT ---------- ---------- ---------- EMPA DEPTA 80000 EMPC DEPTB 80000
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On