2

This is my query:

SELECT Cottage15.CNUM,Cottage15.Rent,Assignment15.Hours
FROM Cottage15
INNER JOIN Assignment15
ON Cottage15.CNUM=Assignment15.CNUM
ORDER BY Assignment15.AID;

It works, but I can't figure out how to apply the Max function to hours? I was hoping that MAX(Assignment15.Hours) would work!?

This query shows all of the rows, but I only want it to show me certain rows that have the highest number based on the hour field.

2
  • 2
    You need to use GROUP BY along with it. Commented Apr 10, 2015 at 23:27
  • 1
    MAX is an aggregate function, you'll have to create groups to aggregate over. Commented Apr 10, 2015 at 23:27

3 Answers 3

1

you must add group aggregation to apply Max function

 SELECT Cottage15.CNUM,Cottage15.Rent,Max(Assignment15.Hours)
 FROM Cottage15
 INNER JOIN Assignment15
 ON Cottage15.CNUM=Assignment15.CNUM
 group by Cottage15.CNUM,Cottage15.Rent
 ORDER BY Assignment15.AID;
Sign up to request clarification or add additional context in comments.

Comments

1

I think this is what you want

SELECT Cottage15.CNUM,Cottage15.Rent,Assignment15.Hours
FROM Cottage15
INNER JOIN Assignment15
ON Cottage15.CNUM=Assignment15.CNUM
WHERE Assignment15.Hours = (SELECT MAX(Assignment15.Hours) FROM Assignment15)
ORDER BY Assignment15.Hours

4 Comments

It's still showing all of the data records (rows) when I ran this, I need it to only show the record with the highest number in hours field.
How about that? Sorry, it's hard to tell without any idea what your data means. If you could tell me what you wanted, it would really help me help you
That query didn't do anything. It didn't load any records When I run the query, it shows me all 15 records in the database. They are sorted. It looks great, however I only wana show records that have the number 5. 5 is the max value in my database, so I wanna show limited records. It's not doing that
I think what you need is to add a where clause
0

if you want to get max(hour) per each Cottage15.CNUM and Cottage15.Rent then use max(Assignment15.Hours) and group by Cottage15.CNUM,Cottage15.Rent:

SELECT Cottage15.CNUM,Cottage15.Rent,MAX(Assignment15.Hours)
FROM Cottage15
INNER JOIN Assignment15
ON Cottage15.CNUM=Assignment15.CNUM
GROUP BY Cottage15.CNUM,Cottage15.Rent

But if you want to get Cottage15.CNUM and Cottage15.Rent which have max(Assignment15.Hours) in the whole result set then you can get it by:

SELECT Cottage15.CNUM,Cottage15.Rent,Assignment15.Hours
FROM Cottage15
INNER JOIN Assignment15
ON Cottage15.CNUM=Assignment15.CNUM
WHERE Assignment15.Hours = ( SELECT MAX(Assignment15.Hours)
                             FROM Cottage15
                             INNER JOIN Assignment15
                             ON Cottage15.CNUM=Assignment15.CNUM
                            )

4 Comments

That seems to be bit more clear, but it didn't execute when I ran it :*(
why it didn't execute!!?? give a proper feedback, what do you mean by didn't execute ? it gives error or didn't give expected output?
you just missed a semicolon at the end of query ;
@rkail you are welcome, if it worked and was helpful you can accept it. by clicking on the check mark on the left side of answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.