I have a MySQL table:
o_id | Salesperson | fax | Pincode
1 Sandy 2110271 12011
2 Sandy 2452356 24523
3 Ricky 1023564 45510
I need to get the following result:
Salesperson | fax | Pincode
Sandy 2452356 24523
Ricky 1023564 45510
(Note: I don't need o_id column to be displayed)
I have tried:
select distinct salesperson,faxno,pin
from OPPORTUNITY
and:
select salesperson,faxno,pincode
from OPPORTUNITY
Group By (salesperson)
…but still I am getting:
Salesperson | fax | Pincode
Sandy 2110271 12011
Ricky 1023564 45510
I have tried a lot of scenarios present on the web but still not able to achieve the expected result.
Is there any property or function that will give "Sandy" – whose o_id
is max.
Example:
Select distinct sales, faxno, pincode
from opportunity where o_id= max(o_id)
Although above code is not working, but anything like that can help?
Best Answer
The row:
is preferred because it has the largest
o_id
. You can pick the largesto_id
per salesperson via:An important property of SQL is that it is closed, i.e. a result of a query is a relation (table). So we can simply join this with the original table:
Even if question does not say so explicitly, there is reason to believe that
salesperson
is functionally dependent ofo_id
. If that is the case it is sufficient to join byo_id
alone:In SQL2003, window functions were introduced. They are a perfect fit for this kind of query. I have heard the next version of MySQL will support them, but I guess you are not using that.
You also mention this construction:
It is probably the construction in MySQL that causes the most confusion. It is invalid in most (all?) other products, because the result is non-deterministic or random. What happens is that MySQL randomly picks a
faxno, pincode
for eachsalesperson
.