473,421 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 developers and data experts.

Pivoting - 3

debasisdas
8,127 Expert 4TB
CUBE:-IT GENERATES SUBTOTAL FOR ALL POSSIBLE COMBINATION OF GROUPED COLUMNS.

GROUPING SETS:-GENERATES SUMMARY INFORMATION AT THE CHOOSEN LEVEL,WITHOUT INCLUDING ALL THE ROWS PRODUCED BY REGULAR GROUP BY OPERATION.

GROUPING,GROUPING_ID,GROUP_ID :-HELPS TO CORRECTLY INTERPRET RESULTS GENERATED USING ROLLUP,CUBE AND GROUPING SETS.

Expand|Select|Wrap|Line Numbers
  1. select empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno);
  2.  
  3. select decode(grouping(empno),1,'all empno',empno) as no,empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno);
  4.  
  5.  
  6. select decode(grouping(empno),1,'all empno',empno) as no,empno,ename,sum(sal),avg(sal) from emp group by cube(ename,empno)
  7.  
  8. select empno,ename,avg(sal) from emp group by grouping sets(empno,ename)
The value from GROUPING(JOB) will be 1 or 0 depending on whether or not the values for SAL are due to the GROUP BY or the CUBE. If the results are due to the CUBE, the value will be 1, otherwise it will be 0.
----------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select deptno,
  2.        job,
  3.        case grouping(deptno)||grouping(job)
  4.             when '00' then 'TOTAL BY DEPT AND JOB'
  5.             when '10' then 'TOTAL BY JOB'
  6.             when '01' then 'TOTAL BY DEPT'
  7.             when '11' then 'GRAND TOTAL FOR TABLE'
  8.        end category,
  9.        sum(sal) sal
  10.   from emp
  11.  group by cube(deptno,job)
  12.  order by grouping(job),grouping(deptno)
Few more sample query
==================
Sample #1
===========
Expand|Select|Wrap|Line Numbers
  1. select deptno, job, sum(sal) sal,
  2.        grouping(deptno) deptno_subtotals,
  3.        grouping(job) job_subtotals
  4.   from emp
  5.  group by cube(deptno,job)
Sample #2
===========
Expand|Select|Wrap|Line Numbers
  1. select ename,
  2.         job,
  3.         case when job = 'CLERK'
  4.              then 1 else 0
  5.         end as is_clerk,
  6.         case when job = 'SALESMAN'
  7.              then 1 else 0
  8.         end as is_sales,
  9.         case when job = 'MANAGER'
  10.              then 1 else 0
  11.         end as is_mgr,
  12.         case when job = 'ANALYST'
  13.             then 1 else 0
  14.         end as is_analyst,
  15.         case when job = 'PRESIDENT'
  16.             then 1 else 0
  17.         end as is_prez
  18.    from emp
  19.   order by 2
Sample #3
===========
Expand|Select|Wrap|Line Numbers
  1. select max(case deptno when 10 then ename end) d10,
  2.         max(case deptno when 20 then ename end) d20,
  3.         max(case deptno when 30 then ename end) d30,
  4.         max(case job when 'CLERK' then ename end) clerks,
  5.         max(case job when 'MANAGER' then ename end) mgrs,
  6.         max(case job when 'PRESIDENT' then ename end) prez,
  7.         max(case job when 'ANALYST' then ename end) anals,
  8.         max(case job when 'SALESMAN' then ename end) sales
  9.    from (
  10.  select deptno, job, ename,
  11.         row_number()over(partition by deptno order by empno) rn
  12.    from emp
  13.         ) x
  14.   group by rn
Performing Aggregations over Different Groups/Partitions Simultaneously
================================================== ====
Expand|Select|Wrap|Line Numbers
  1. select ename,deptno, count(*)over(partition by deptno) deptno_cnt, job,
  2. count(*)over(partition by job) job_cnt,
  3. count(*)over() total from emp
Performing Aggregations over a Moving Range of Values
==========================================
Expand|Select|Wrap|Line Numbers
  1. select hiredate,
  2.        sal,
  3.        sum(sal)over(order by hiredate
  4.                        range between 90 preceding
  5.                          and current row) spending_pattern
  6.   from emp e
Pivoting a Result Set with Subtotals
============================
Expand|Select|Wrap|Line Numbers
  1. select mgr,
  2.        sum(case deptno when 10 then sal else 0 end) dept10,
  3.        sum(case deptno when 20 then sal else 0 end) dept20,
  4.        sum(case deptno when 30 then sal else 0 end) dept30,
  5.        sum(case flag when '11' then sal else null end) total
  6.   from (
  7. select deptno,mgr,sum(sal) sal,
  8.        cast(grouping(deptno) as char(1))||
  9.        cast(grouping(mgr) as char(1)) flag
  10.   from emp
  11.  where mgr is not null
  12.  group by rollup(deptno,mgr)
  13.        ) x
  14.  group by mgr
Sep 17 '07 #1
0 3712

Sign in to post your reply or Sign up for a free account.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
2
by: bher2 | last post by:
gud day. please help me. im working right now on a case study that will retrieve/produce a simple report on sql. my problem is I dont know how to pivot queries like in access. please help me....
3
by: Gert v O | last post by:
Can someone help me parsing this ms-access PIVOT sql-statement to a ms-sql-server sql-statement? Many thanks in advance TRANSFORM Count(KlantenStops.id) AS AantalVanid SELECT...
7
by: FiveFootUnder | last post by:
Hi. Ow. I have a bruised forehead from banging my head against a brick wall and would really appreciate some help here. As long as it's not a suggestion that I shouldn't be trying this!! I'm...
0
debasisdas
by: debasisdas | last post by:
This article contains some of the tips for PIVOTING the recordset (output of the query) . PIVOTING is mainly used for reporting purpose. Displaying the total number of employees department wise....
0
debasisdas
by: debasisdas | last post by:
Displaying Histogram-Horizontal =========================== select deptno,lpad('*',count(*),'*') as cnt from emp group by deptno Histogram-Vertical =============== select row_number(...
0
debasisdas
by: debasisdas | last post by:
Displaying Histogram-Horizontal =========================== select deptno,lpad('*',count(*),'*') as cnt from emp group by deptno Histogram-Vertical =============== select row_number(...
0
by: Orbie | last post by:
Hi all, I have the following SQL Server query which retrieves rows i'm interested in: Select Temp_Product_ID, Dept_Name, Section_Name, Commodity_Name, Product_Description, Rank, Guide_Price,...
1
by: MrMob | last post by:
Plz teach me to do that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.