If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > join and only include rows where a value is max()?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-06, 16:40
waskelton4 waskelton4 is offline
Registered User
 
Join Date: Jul 2004
Posts: 7
join and only include rows where a value is max()?

lets see if i can get this to make any sense at all

i have an application that has a table that holds active visits to our hospital.

i join the "state" table to the visit table to get current diagnoses for the visit.

the state table has multiple states for each visit so as to track what happened at each visit.

my question is..

is there an easy way withing the join syntax to join only the row in the state table with the maximum value for that visit?

something that might look like this..

Code:
.....
inner join state s on v.vID = s.vID AND MAX(s.id)
...
i have been successful doing this with the where clause but i don't think that is an option in this case. I would try and explain how this all fits together but i'm afraid i would fall short of making good sense and everyone would just be lost.

any ideas?

thanks
will
Reply With Quote
  #2 (permalink)  
Old 10-26-06, 17:26
waskelton4 waskelton4 is offline
Registered User
 
Join Date: Jul 2004
Posts: 7
maybe this will help in the understanding of my complicated explanation of a not-so-complicated problem

as the query exists currently i get data like so..


charge visit state diagID
1 100 101 1
1 100 101 2
1 100 102 1
1 100 102 3
1 100 103 1
1 100 103 2
2 101 104 5
2 101 104 6
2 101 105 5


I'd like to use the join syntax to only pull the most recent (or highest) state id.
in this case for visit 100 it would be state 103 and for visit 101 state 105

The result set above is pretty much exactly what i want only i need to get rid of the rows that have rows with a state value that isn't the max state value.

ws
Reply With Quote
  #3 (permalink)  
Old 10-28-06, 12:16
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
As you didn't provide CREATE TABLE statements and INSERT sample data (or query which you used to retreive records showed in your #2 post), let me improvize: query you're looking for might lok like this:
Code:
SELECT charge, visit, state, diagID
FROM TEST t
WHERE t.state = (SELECT MAX(t1.state)
                 FROM TEST t1
                 WHERE t1.visit = t.visit
                );
"TEST" table here is, in fact, result of your query. It could then be
Code:
SELECT charge, visit, state, diagID
FROM (<your_query_here) t
WHERE t.state = ...
I guess this might be done easier (without inline queries) but - do provide something more than you already have.
Reply With Quote
  #4 (permalink)  
Old 10-30-06, 02:27
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
If state numbers are unique, i.e., no two visits can have the same state, the following might be what you're looking for:
Code:
SELECT * FROM test
WHERE state IN (
    SELECT MAX(state) FROM test
    GROUP BY visit )
Be sure to add any additional conditions (limiting the states to be seen) to the inner query, for performance reasons.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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

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