| re: Query to find a value in column b based on an aggregate function on column a?
Hi
You could try:
SELECT Branch, Month, Sales
FROM MonthlySales
WHERE Sales =
( SELECT MAX(Sales) FROM MonthlySales )
If you need branch them:
SELECT M.Branch, M.Month, M.Sales
FROM MonthlySales M
WHERE M.Sales =
( SELECT MAX(S.Sales)
FROM MonthlySales S WHERE S.Branch = M.Branch )
John
"AndyL" <news@squit.co.uk.nospamplease.net> wrote in message
news:voq7iigetsa7ed@corp.supernews.com...[color=blue]
> Hi,
>
> Suppose I have a table containing monthly sales figures from my shop
> branches:
>
> Branch Month Sales
> -----------------------
> London Jan 5000
> London Feb 4500
> London Mar 5200
> Cardiff Jan 2900
> Cardiff Feb 4100
> Cardiff Mar 3500
>
> The question I am trying to ask is this: in which month did each branch
> achieve its highest sales? So I want a result set something like this:
>
> Branch Month
> ----------------
> London Mar
> Cardiff Feb
>
> I can do a "SELECT Branch, MAX(Sales) FROM MonthlySales GROUP BY Branch"[/color]
to[color=blue]
> tell me what the highest monthly sales figure was, but I just can't figure
> out how to write a query to tell me which month corresponded to[/color]
MAX(Sales).[color=blue]
> Ideas anyone?
>
> Cheers,
>
> ...Andy
>
>[/color] |