|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
Code:
select top 1
deptId
, count(*)
from Course
group
by deptId
order
by count(*) desc
|
|
#3
|
|||
|
|||
|
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... |
|
#4
|
||||
|
||||
|
"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 |
|
#5
|
|||
|
|||
|
Ok thanks, but I'm still confused about how the max value was derrived without using the MAX function?
|
|
#6
|
||||
|
||||
|
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 ![]() |
|
#7
|
|||
|
|||
|
Thanks again!
![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > MAX/COUNT Query problem... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|