dbTalk Databases Forums  

Getting values from other fields using min & max aggregates

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Getting values from other fields using min & max aggregates in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gabe Knuth
 
Posts: n/a

Default Getting values from other fields using min & max aggregates - 04-07-2004 , 02:11 PM






Hi all. I tried to search the newsgroup for this, but I'm not entirely sure how to phrase it to get the best results, so I figured I'd post and hope someone can help me out.

I've got a SQL statment that retrieves the minimum and maximum values from the Score field in a table called Scores (original, I know). What I'd like to do is retreive another field called PersonID for each of the two results I get. Basically, I'd like to have the output look like this

Record Score_Min Score_Min_PersonID Score_Max Score_Max_PersonI
1 ........41...........5.......................... 69............

The current SQL statement I have is below, and works great for returning only the min and max scores

SELECT Score_Min=MIN(dbo.Scores.Score), Score_Max=MAX(dbo.Scores.Score) FROM dbo.Scores WHERE Score <> 0

Thanks in advance for your help

Gabe

Reply With Quote
  #2  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Getting values from other fields using min & max aggregates - 04-07-2004 , 02:39 PM






Try:

SELECT Score_Min=MIN(b.Score),
Score_Min_PersonID=(select personid from Scores a
where a.score = min(b.score)),
Score_Max=MAX(b.Score),
Score_Max_PersonID=(select personid from Scores a
where a.score = max(b.score))
FROM dbo.Scores b
WHERE Score <> 0


Make sure that you have only one row for each of the max(score) and min(score) otherwise you
will get an error as

"Subquery returned more than 1 value......."

--
Vishal Parkar
vgparkar (AT) yahoo (DOT) co.in




Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Getting values from other fields using min & max aggregates - 04-07-2004 , 03:47 PM



On Wed, 7 Apr 2004 13:26:13 -0700, Gabe Knuth wrote:

Quote:
Ok, that works to the point where I get the error message that you mentioned.

The low score appears 3 times in the table, which I assume is causing this problem. Will having two recordsets solve the problem? One for min and one for max?
In that case, try:

SELECT Score_Min=MIN(b.Score),
Score_Min_PersonID=(select MAX(personid) from Scores a
where a.score = min(b.score)),
Score_Max=MAX(b.Score),
Score_Max_PersonID=(select MAX(personid) from Scores a
where a.score = max(b.score))
FROM dbo.Scores b
WHERE Score <> 0

This will give you one of the persons scoring the highest score and
one of the persones scoring the lowest score. In both cases: the one
with the highest personid.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.