SELECT COUNT(TABLE1.COL1) AS Expr1, TABLE2.COL2
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2
ORDER BY expr1 DESC
RESULT
EXPR1 COL2
6 1212131
6 1212131
4 121121
2 1212121
what i want is the maximum values
RESULT
EXPR1 COL2
6 1212131
6 1212131
select * from (
SELECT COUNT(TABLE1.COL1) AS Expr1, TABLE2.COL2
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2
ORDER BY expr1 DESC
) where rownum = 1
Regards
Michel Cadot
select * from
(SELECT COUNT(TABLE1.COL1) AS Expr1, TABLE2.COL2
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2)
where expr1 = ( SELECT max(COUNT(TABLE1.COL1))
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2);
should return the result set you want. Michel's suggestion returns
only one row, no matter how many may match the max(expr1) value in the
output. This should return all matching rows to that maximum value.
Yes, it calls the joined query twice, but to return what you want you
need to compare expr1 to the max value for that column ( which you
don't return in your main query) to return all matching rows.
Of course, Jonathan Lewis may have a more elegant solution.
David Fitzjarrell
select expr1, col2 from (
SELECT COUNT(TABLE1.COL1) AS Expr1, TABLE2.COL2,
rank() over (order by COUNT(TABLE1.COL1) desc) rk
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2
) where rk = 1
/
Regards
Michel Cadot
Nicely done.
David Fitzjarrell
select * from
(SELECT COUNT(TABLE1.COL1) AS Expr1, TABLE2.COL2
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2)
where expr1 = ( SELECT TOP 1 COUNT(TABLE1.COL1) As Expr1
FROM TABLE1,TABLE2 where TABLE1.COL1 = TABLE2.COL1
GROUP BY TABLE2.COL2
ORDER BY Expr1 desc);
also tell me if there are any other alternatives i can use. and Thanku
all of u very much.
If you're using SQL Server why on EARTH are you posting this question
HERE???
You SHOULD be posting in comp.databases.sql-server.
David Fitzjarrell
Very simply:
Oracle <> SQL Server
Go to:
http://msdn2.microsoft.com/en-us/library/ms203721.aspx
--
Daniel A. Morgan
University of Washington
damo...@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org