Skip to main content

Posts

Showing posts with the label group by

SQL TOP, RANK, ROW_NUMBER ,DENSE_RANK Functions

To use  SQL TOP, RANK,  ROW_NUMBER ,DENSE_RANK Functions  you can see below image. Select top 5 records from table. In below query We used SQL TOP, RANK,  ROW_NUMBER ,DENSE_RANK .You can check this with your table.

Example of rollup and cube in sql

SQL> SELECT DEPTNO,SUM(SAL)  2 FROM EMP  3 GROUP BY ROLLUP(DEPTNO)  4 /  DEPTNO SUM(SAL) ---------- ----------  10 8750  20 10875  30 9400  29025 SQL> ED Wrote file afiedt.buf  1 SELECT NVL(TO_CHAR(DEPTNO),'TOTAL'),SUM(SAL)  2 FROM EMP  3* GROUP BY ROLLUP(DEPTNO) SQL> / NVL(TO_CHAR(DEPTNO),'TOTAL') SUM(SAL) ---------------------------------------- ---------- 10 8750 20 10875 30 9400 TOTAL 29025 SQL> ED Wrote file afiedt.buf  1 SELECT NVL(TO_CHAR(DEPTNO),'TOTAL'),SUM(SAL)  2 FROM EMP  3* GROUP BY CUBE(DEPTNO) SQL> / NVL(TO_CHAR(DEPTNO),'TOTAL') SUM(SAL) ---------------------------------------- ---------- TOTAL 29025 10 8750 20 10875 30 9400 SQL> ED Wrote file afiedt.buf  1 SELECT NVL(TO_CHAR(DEPTNO),'TOTAL'),JOB,SUM(SAL)  2 FROM EMP  3* GROUP BY ROLLUP(DEPTNO,JOB) SQL> / NVL(TO_CHAR(DEPTNO),'TOTAL') JOB SUM(SAL) ---------------------------------------- --------- ---------- 10 CLERK 1300 10 MANAGER 2450 10 PR

How to Set Default Limit in Store Procedure in SQL

This is stored procedure which show you rows return as per mention in stored procedure. CREATE PROCEDURE get_categories( IN p_action_flag VARCHAR(50), IN p_firstrow int, IN p_lastrow int) BEGIN IF p_action_flag = 'all' THEN SELECT top 50 u.*, d.name, d.image FROM user u, member gu, detail d WHERE u.user_id = gu.user_id AND u.user_id = d.user_id AND u.status = '1' AND gu.group_id IN (1, 7) GROUP BY u.user_id ORDER BY d.name LIMIT p_firstrow,p_lastrow; ELSEIF p_action_flag = 'Most Viewed' THEN SELECT top 50 u.*, d.name, d.image FROM user u, member gu, detail d WHERE u.user_id = gu.user_id AND u.user_id = d.user_id AND u.status = '1' AND gu.group_id IN (1, 7) GROUP BY u.user_id ORDER BY u.views DESC, d.name ASC LIMIT p_firstrow,p_lastrow ; END IF END