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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Maximum Call History 1

Status
Not open for further replies.

abrewis

Programmer
Oct 16, 2001
37
0
0
GB
I have a view which produces three columns of data

Call_Number History Current_Group
1 1 5
1 2 8
1 3 6
2 1 6
2 2 6
3 1 8
3 2 9

I need to extract the call numer, the group and the history from the call's maximum row.

ie. Results should show

Call_Number History Current_Group
1 3 6
2 2 6
3 2 9

I have tried using serveral function without any luck.
Any help would be much appreciated.

Alasdair
 
This should do it
select t1.call_number,t1.history,t1.current_group
from table t1 join (
select call_number,max(history) as MaxHistory from
table group by call_number) t2 on t1.number = t2.number
and t1.history =t2.MaxHistory

Denis The SQL Menace
SQL blog:
Personal Blog:
 
There are several ways to do this. One such idea would be:
Code:
SELECT 		call_number,
		History,
		Current_Group
FROM		#TableA a,
		(SELECT 	call_number, 
				MAX(History) max_history
		 FROM		#TableA
		 GROUP BY	call_number) b
WHERE 		a.call_number = b.call_number
		and a.history = b.max_history

PS: Code Not Tested

Regards,
AA
 
Both work a treat, thank you very much.
Alasdair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top