Thursday, February 28, 2008

Group by SQL Query Tuning.

Comparison of two group by Queries:
-- slower query

SQL > SELECT prod_id, cust_id, channel_id, SUM(quantity_sold)
FROM sales
WHERE cust_id <>


-- faster query
SQL> SELECT prod_id, cust_id, channel_id, SUM(quantity_sold)
FROM sales
WHERE cust_id <>


SQL> select deptno, empno, ename, sum(nvl(sal,0)) from scott.emp group by grouping sets ((deptno),(empno, ename));

-- display totals for the deptno's.
SQL> select deptno, sum(nvl(sal,0))
from scott.emp
group by grouping sets ((deptno));