MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 26th, 2004, 06:15 PM
PFC PFC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 PFC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MAX/COUNT Query problem...

Firstly, I'd just like to say any help would be greatly appreciated as I'm completelly stumped. I've just started learning SQL and am using MS Access. My predicament is as follows:

I have a table called Course, containing crsId, crsLevel, crsCredits, and deptId. What I'm trying to do is find which department (deptId) offers the greatest number of courses (crsId).

Now I'm pretty sure, Oracle would let me do something like this:

SELECT deptId, COUNT(*)
FROM Course
GROUP BY deptId
HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM Course GROUP BY deptId)

But Access won't let me combine the functions (ie. I can't nest COUNT within MAX). I can do this:

SELECT deptId, COUNT(*) AS NumberOfCourses
FROM Course
GROUP BY deptId

and get a listing of the number of courses offered by each department, but I'm stuck there, and have no idea how to, for example, perform the MAX function on the newly created "NumberOfCourses" column.

I'm completelly stuck and would appreciate any help in resolving this problem of mine! Thank you very much in advance.

Reply With Quote
  #2  
Old September 26th, 2004, 10:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 39 sec
Reputation Power: 1018
Code:
select top 1 
       deptId
     , count(*)
  from Course 
group 
    by deptId
order
    by count(*) desc 
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old September 27th, 2004, 01:48 PM
PFC PFC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 PFC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank You very much!
This does precisely what I needed it to do. I'd just like to understand it now The part I'm confused about is "top 1" - what does this exactly do? Also, someone mentioned to me that I can write the SELECT statement to accomplish what I needed it to accomplish with domain functions or crosstab queries... would you mind just briefly (or otherwise) explaining these to me? Once again, I'm very greatful and thank You in advance...

Reply With Quote
  #4  
Old September 27th, 2004, 01:54 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 39 sec
Reputation Power: 1018
"top N" returns the first N rows from a result set

obviously, this is very much dependent on the sequence of the rows in the result set, which is determined by the ORDER BY clause

"domain functions or crosstab queries" ???

i don't have a clue what they were trying to tell you

i mean, i know what those are, but i just don't see how they would be pertinent to this particular situation

Reply With Quote
  #5  
Old September 27th, 2004, 04:07 PM
PFC PFC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 PFC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok thanks, but I'm still confused about how the max value was derrived without using the MAX function?

Reply With Quote
  #6  
Old September 27th, 2004, 04:17 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 39 sec
Reputation Power: 1018
because the highest value comes first when you sort a bunch of rows in descending order by count

example result set:

Engineering 937
Marketing 53
Sales 51
HR 25
Executive 3

"TOP 1" means take only the first row of the result set

MAX function not required, see?

another way to get the largest of something is to find all the rows where there is no row with a larger value (there will be one, possibly several if there are ties)

but let's not do the sql for that just yet

Reply With Quote
  #7  
Old September 27th, 2004, 05:34 PM
PFC PFC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 PFC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks again!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > MAX/COUNT Query problem...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT