 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

September 22nd, 2006, 12:56 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 22nd, 2006, 01:04 PM
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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.
|

September 22nd, 2006, 01:10 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 22nd, 2006, 01:13 PM
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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.
|

September 22nd, 2006, 01:15 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 22nd, 2006, 01:18 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 22nd, 2006, 01:23 PM
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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.
|

September 22nd, 2006, 01:31 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|

September 22nd, 2006, 01:38 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 22nd, 2006, 01:40 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |