Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Problem Group by
"Arun Goel" <goel_ar1_at_hotmail.com> schrieb :
> Hi Dieter,
>
> I meant to say that
> for same A,B I need the rows with maximum of same C. & also want to know
> which C.
>
> I am able to find
> A1 B1 3
> A2 B2 3
> using this query.
> select A, B , MAX(Count_C ) as MAXC from
> (
> SELECT Testtable.A, Testtable.B, Count(Testtable.C) AS Count_C
> FROM Testtable
> GROUP BY Testtable.A, Testtable.B , Testtable.C
> ) GROUP BY A, B
>
> But I am mainly interested in what is the corresponding value of C for
both
> rows.
> So what I need as result is:
> A1 B1 C1
> A2 B2 C1
>
> Count is optional.
> I hope I am clear.
>
> Thanks
> --
> ----------------
> Arun Goel
> http://www.crml.uab.edu/~ag
Hi Arun,
but what is the result from:
A B C
I have no idea how to do that with a simple Select.
But you can try something like that (not testet): (assuming A, B is int)
Declare @LastA int, @LastB int
Set @LastA = 0
Set @LastB = 0
DECLARE TestC CURSOR FOR
select A,B,C,count(*) D
from temp group by A,B,C
Set @LastA = A WHERE CURRENT OF TestC Set @LastB = B WHERE CURRENT OF TestC Set @LastB = C WHERE CURRENT OF TestCPRINT @LastA,@LastB,@LastC
Regards
Dieter
Received on Sat Nov 30 2002 - 16:59:08 CST