Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 22nd, 2006, 12:56 PM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Min Function

Can someone help me?
I'm trying to get a record for a table with the least amount of calls. I'm using this code in SQL but it is not working:

select MIN(vchNumberCalls)as 'Number of Calls', vchSpecialistName
from tblConsumerSpecialists
When I run this code it returns the correct number, but I need the Name that goes along with that number.

select MIN(vchNumberCalls)as 'Number of Calls'
from tblConsumerSpecialists


Alexander Nelson
Programmer
__________________
Alexander Nelson
Programmer
Reply With Quote
  #2 (permalink)  
Old September 22nd, 2006, 01:04 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

What is the relationship between Numberofcalls and vchSpecialistName?

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
Reply With Quote
  #3 (permalink)  
Old September 22nd, 2006, 01:10 PM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's a phone rotation list is certain people are getting more calls than others. So everytimes someone gets a call it will add one to number of calls, and when they hit next it will run the query again to pull the next person with the least number of calls to received the next call an so on and so forth.

Alexander Nelson
Programmer
Reply With Quote
  #4 (permalink)  
Old September 22nd, 2006, 01:13 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

If number of calls is unique i would do something like

SELECT name from table where numberCalls = min(vchnumbercalls)

again though, I assume that number of calls is a unique number else you will return multiple names here.

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
Reply With Quote
  #5 (permalink)  
Old September 22nd, 2006, 01:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, if a ConsumerSpecialist gets a few long duration calls, that ConsumerSpecialist will get the next call as opposed to a ConsumerSpecialist who gets a lot of short duration calls.


Rand
Reply With Quote
  #6 (permalink)  
Old September 22nd, 2006, 01:18 PM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the error I get when I do that:
"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."

Alexander Nelson
Programmer
Reply With Quote
  #7 (permalink)  
Old September 22nd, 2006, 01:23 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Due dilligence. Anyway: What you would do is declare a variable in SQL = to the value of min(vchnumbercalls) then you would call your select statement and base your where clause off of your varaible.



--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
Reply With Quote
  #8 (permalink)  
Old September 22nd, 2006, 01:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your 'vch' prefix doesn't indicate the calls column is a varchar, does it?

Assuming not, use a subquery. Try:
Code:
SELECT vchNumberCalls as 'Number of Calls', vchSpecialistName
FROM tblConsumerSpecialists
WHERE vchNumberCalls = (SELECT MIN(vchNumberCalls
                            FROM tblConsumerSpecialists)
but as was pointed out earlier, this could return more than one row if the same minimum value was present in more then one row of the table.

You can't avoid that unless you can define what conditions distigush one row with the same number of calls from another with that same value.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #9 (permalink)  
Old September 22nd, 2006, 01:38 PM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff, But for some reason I'm getting this error:
Incorrect syntax near the keyword 'FROM'. I don't know why it is throwing this error.

Alexander Nelson
Programmer
Reply With Quote
  #10 (permalink)  
Old September 22nd, 2006, 01:40 PM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff I figured it out.
SELECT vchNumberCalls as 'Number of Calls', vchSpecialistName
FROM tblConsumerSpecialists
WHERE vchNumberCalls = ((SELECT MIN(vchNumberCalls)
                            FROM tblConsumerSpecialists))

Alexander Nelson
Programmer
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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one get the Max or Min Filename? crabjoe ASP.NET 1.0 and 1.1 Basics 4 January 15th, 2008 04:48 PM
MAX/MIN Value Help slbibs SQL Server 2005 4 November 6th, 2007 04:00 AM
Min / Max followup danbush XSLT 2 August 31st, 2005 12:02 PM
min-height in IE cgallagr98 CSS Cascading Style Sheets 5 April 8th, 2005 12:47 PM
MIN() function mohiddin52 Access 1 October 29th, 2004 07:09 AM



All times are GMT -4. The time now is 04:26 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.