473,490 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pivoting - 3

debasisdas
8,127 Recognized Expert Expert
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 3715

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

Similar topics

226
12312
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
2553
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
6272
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
1697
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
4830
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
1496
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
3808
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
1471
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
1517
by: MrMob | last post by:
Plz teach me to do that...
0
7112
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
7146
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
7183
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...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4878
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
4573
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...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.