Sie sind auf Seite 1von 6

To eliminate duplicates rows from a table.

Method 1: DELETE FROM table_name A WHERE ROWID > ( SELECT min(rowid) FROM table_name B WHERE A.key_values = B.key_values); Method 2: create table table_name2 as select distinct * from table_name1; drop table_name1; rename table_name2 to table_name1; Method 3: Delete from table_name where rowid not in( select max(rowid) from table_name group by key_column_value );

to select the top N rows from a table. SELECT * FROM emp a WHERE 3 >= (SELECT COUNT(DISTINCT sal) FROM emp b WHERE a.sal >= b. sal) ORDER BY sal DESC SELECT * FROM (SELECT * FROM emp ORDER BY sal DESC) WHERE ROWNUM <>

To Use CUBE in Group by Clause For n-dimensional cube, n2 SELECT statements are needed, but in Rollup ( N+1) Select statement, linked with UNION ALL is needed. Thus with Rollup only subset of all possible combination obtained, while CUBE calculate subtotals for all possible combinations.
SELECT Region, Area, Department, SUM(Profit) FROM Sales GROUP BY Region, Area, Department UNION ALL SELECT Region, area, '' , SUM(Profit) FROM Sales GROUP BY Region, area UNION ALL SELECT Region, , Department, SUM(Profit) FROM Sales GROUP BY Region, Department UNION ALL SELECT Region , '', '', SUM(Profits) FROM Sales GROUP BY Region UNION ALL SELECT , Area , Department , SUM(Profits) FROM Sales GROUP BY Area , Department UNION ALL SELECT , Area , , SUM(Profits) FROM Sales GROUP BY Area UNION ALL SELECT , , Department , SUM(Profits) FROM Sales GROUP BY Department UNION ALL SELECT '', '', '', SUM(Profits) FROM Sales;

CUBE can, easily obtain this Query. SELECT Region, Area, Department, sum(Profit) AS Profit FROM sales GROUP BY CUBE(Region, area, Dept)

echnical

Oracle Fusion

SQL/PLSQL OraTips

Essential Unix

Oracle Datawarehouse

Monday, May 28, 2007


Grouping Function with ROLLUP and CUBE
The Grouping Function is used in Conjunction with ROLLUP and CUBE. Function Grouping(column_name) returns either 1 or 0 depend on whether column is used in subtotal using ROLLUP or CUBE. If Subtotal obtained using ROOLUP/ GROUPING, then column appearing in the subtotal, column value will be displayed as NULL and grouping(column_name) will return 1. Function grouping(column_name) can be used to give the meaning full name rather then display NULL in the Rows.

SELECT decode(grouping(region), 1, 'All Regions', 0, null)) as Region, decode(grouping(Area), 1, 'All Area', Area) as Area, sum(Profit) AS Profit from Sales group by CUBE(Area, Region)

hnical

Oracle Fusion

SQL/PLSQL OraTips

Essential Unix

Oracle Datawarehouse

Sunday, May 27, 2007


Retrieve Nth row from a table
Retrieve only the Nth row from a table SELECT * FROM emp WHERE rowid = (SELECT rowid FROM emp WHERE rownum <= 5 MINUS SELECT rowid FROM emp WHERE rownum 5);

SELECT * FROM emp

WHERE rownum=1 AND rowid NOT IN (SELECT rowid FROM emp WHERE rownum 5); Retrieve only the x to yth row from a table SELECT *FROM emp WHERE rowid in ( SELECT rowid FROM emp WHERE rownum <= 7 MINUS SELECT rowid FROM emp WHERE rownum 5); To get every Nth row from a table. SELECT * FROM emp WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4) FROM emp); SELECT * FROM ( SELECT rownum rn, empno, ename FROM emp ) a WHERE MOD(a.rn,4) = 0;

To Use Rollup in Group by Clause SELECT Region, Area, Department, SUM(Profit) FROM Sales GROUP BY Region, Area, Department UNION ALL SELECT Region, Area, '' , SUM(Profit) FROM Sales GROUP BY Region, Area UNION ALL SELECT Region , '', '', SUM(Profits) FROM Sales GROUP BY Region UNION ALL SELECT '', '', '', SUM(Profits) FROM Sales; Rollup can, easily obtain this Query. SELECT Region, Area, Department, sum(Profit) AS Profit FROM salesGROUP BY ROLLUP(Region, Area, Dept)

Das könnte Ihnen auch gefallen