Subquery and Grouping in Sybase

It may best to describe the problem with an example:
Is it possible to query for the below data and group by fruit so that we have for each fruit, the number of times the price is quoted and the latest price (the data is sorted by time).
Table data is as below:
Fruit      Price
apple      $11
apple      $12
apple      $12.5
grape      $10
grape      $9

Result expected:
fruit -count -price
Apple 3 $12.5
grape 2 $9

Using: Sybase ASE, Embarcadero Rapid SQL is the software.
Thank you.
SeskyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeyhimCommented:
select fruit , count(price) , sum (price) from table group by fruit
0
Joe WoodhousePrincipal ConsultantCommented:
Almost. "sum" is wrong here.


However we can't just say something like

     select fruit , count(price) , max(price) from table group by fruit

because we don't want the max price, we want the most recent price.

You say the data is sorted by time? Is there a time column, or do we just have to trust that it's in the right order?
0
cyberkiwiCommented:
select
 fruit,
 count(*),
 (select top 1 t2.price from mytable t2 where t2.fruit=mytable.fruit order by t2.mytime desc) as price
from mytable

If there is no time column, an ID or timestamp column will work too
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.