Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how works having max()?

Status
Not open for further replies.

Mariellelis

Programmer
Oct 23, 2002
2
0
0
FR
Hi!

First, excuse me, my english is not good at all.

So I try to explain :
I have 2 tables : "person" and "stay".
For one person, we can have one or more stay.
I want to select all the person and their last stay and I don't know how to do it.

I tried like that :
"select nom,numstay, placestay
from person,stay
where person.numperson=stay.numperson
group by person.numperson
having max(numstay);"
But I have no result.

So how can I do?

Thanks in advance for your help!

 
Your English is fine. I don't know the exact structure of your tables, but something like this should work:

select person.numperson, person.nom,
stay.placestay, max(stay.numstay)
from person, stay
where person.numperson = stay.numperson
group by person.numperson

Note that this will work fine in MySQL, however Oracle or SQL Server will have problems returning columns that are not in the "Group By" expression...
 
Thanks for your answer.

While doing this, I have the placestay of the first numstay found, and not the one corresponding to the max numstay.

A not perfect solution would be to add a field max_numstay in the person table, but it's not really good...
Have you another idea?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top