Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I re-write the query below using Rank or Dense_rank in Oracle 10g?

SQL
SELECT sqthree.employeeid,
       sqthree.employeename, 
       to_char(sqthree.Topprofit,'$99,999.99') as TopProfit
FROM (
        /* #3 - select First and Second position */
        SELECT sqtwo.employeeid, sqtwo.employeename, sqtwo.TopProfit
        FROM (
                /* #2 - order the innermost */
                SELECT innermost.employeeid, innermost.employeename, innermost.TopProfit
                FROM (
                        /* #1 - innermost subquery */
                        SELECT s.empid employeeid,
                           s.ename employeename,
                           /* used NVL here */
                           NVL(SUM(i.price * oi.qty),0) - NVL(s.salary,0) TopProfit
                        FROM salespersons s
                        LEFT JOIN orders o ON s.empid = o.empid
                        LEFT JOIN orderitems oi ON o.orderid = oi.orderid
                        LEFT JOIN inventory i ON oi.partid = i.partid
                        GROUP BY s.empid, s.ename, s.salary
                      ) innermost
                ORDER BY TopProfit DESC
            ) sqtwo
      
        WHERE ROWNUM = 1
        ORDER BY TopProfit DESC
     ) sqthree

WHERE ROWNUM = 1 and sqthree.Topprofit is not null;
Posted
Updated 9-Feb-13 21:09pm
v3
Comments
Sergey Alexandrovich Kryukov 9-Feb-13 19:32pm    
Why? Re-write to achieve what?
—SA
7prince 9-Feb-13 20:09pm    
the query gets the TopProfitable salesperson. so instead of using ROWNUM, How can I do the somEthing using RANK or DENSE_RANK ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900