Unix Technical Forum

SQL question using MAX function

This is a discussion on SQL question using MAX function within the SQL Server forums, part of the Microsoft SQL Server category; --> I am trying to select many rows, but only the MAX rows of each distinct lead_seq. I don't want ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:06 PM
derochema
 
Posts: n/a
Default SQL question using MAX function

I am trying to select many rows, but only the MAX rows of each
distinct lead_seq. I don't want to actually select the MAX rows, just
make it a condition.

This is what I have:

declare @in_report_date datetime
SET @in_report_date = '07/06/2003'
select lah.lead_seq
FROM lead_action_history lah
RIGHT outer join lead_master lm on lm.lead_seq = lah.lead_seq
WHERE lah.lead_action_date = (select max(lah.lead_action_date) from
lead_action_history)
GROUP BY lah.lead_seq, lah.lead_action_date

I've tried implementing a HAVING with no luck. Is there a simple
solution?
Any ideas would be much appreciated.

Thanks!

Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:06 PM
Anith Sen
 
Posts: n/a
Default Re: SQL question using MAX function

Seems like you posted only part of the code, where is the use of the locally
declared variable? Also there is no correlation in your subquery. Based on
some guesses, here is a try:

SELECT lah.lead_seq,....
FROM lead_action_history lah
RIGHT OUTER JOIN lead_master lm
ON lm.lead_seq = lah.lead_seq
AND lah.lead_action_date = (SELECT MAX(lah1.lead_action_date)
FROM lead_action_history lah1
WHERE lah1.lead_seq = lah.lead_seq)
GROUP BY lah.lead_seq, lah.lead_action_date ;

If this is not what you are looking for, please post your table structures &
sample data along with expected results so that others can understand your
requirements better.

--
- Anith
( Please reply to newsgroups only )


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:31 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com