473,413 Members | 1,798 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,413 developers and data experts.

Pivoting - 1

debasisdas
8,127 Expert 4TB
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.
=============================================
Expand|Select|Wrap|Line Numbers
  1. select deptno,count(deptno) from emp group by deptno;
Pivoting the same output.
=====================
Expand|Select|Wrap|Line Numbers
  1. select sum(case when deptno=10 then 1 else 0 end) as deptno_10,
  2.         sum(case when deptno=20 then 1 else 0 end) as deptno_20,
  3.         sum(case when deptno=30 then 1 else 0 end) as deptno_30,
  4.         sum(case when deptno=40 then 1 else 0 end) as deptno_40
  5.    from emp;
Selecting employees name group by there job.
======================================
Expand|Select|Wrap|Line Numbers
  1. select max(case when job='CLERK'
  2.                  then ename else null end) as clerks,
  3.         max(case when job='ANALYST'
  4.                  then ename else null end) as analysts,
  5.         max(case when job='MANAGER'
  6.                  then ename else null end) as mgrs,
  7.         max(case when job='PRESIDENT'
  8.                  then ename else null end) as presi,
  9.         max(case when job='SALESMAN'
  10.                  then ename else null end) as sales
  11.   from (
  12. select job,
  13.        ename,
  14.        row_number()over(partition by job order by ename) rn
  15.   from emp
  16.        ) x
  17.  group by rn
Reverse Pivoting
=============
Expand|Select|Wrap|Line Numbers
  1. select dept.deptno,
  2.            case dept.deptno
  3.                 when 10 then emp_cnts.deptno_10
  4.                 when 20 then emp_cnts.deptno_20
  5.                 when 30 then emp_cnts.deptno_30
  6.            end as counts_by_dept
  7.       from (
  8.     Select sum(case when deptno=10 then 1 else 0 end) as deptno_10,
  9.            sum(case when deptno=20 then 1 else 0 end) as deptno_20,
  10.            sum(case when deptno=30 then 1 else 0 end) as deptno_30
  11.       from emp
  12.            ) emp_cnts,
  13.            (select deptno from dept where deptno <= 30) dept

Display in single column
=====================
Expand|Select|Wrap|Line Numbers
  1. select case rn
  2.             when 1 then ename
  3.             when 2 then job
  4.             when 3 then cast(sal as char(4))
  5.        end emps
  6.   from (
  7. select e.ename,e.job,e.sal,
  8.        row_number()over(partition by e.empno
  9.                             order by e.empno) rn
  10.   from emp e,
  11.        (select *
  12.           from emp where job='CLERK') four_rows
  13.  where e.deptno=10
  14.        ) x
Suppressing Repeating Values from a Result Set
========================================
Expand|Select|Wrap|Line Numbers
  1. select to_number(
  2.               decode(lag(deptno)over(order by deptno),
  3.                     deptno,null,deptno)
  4.            ) deptno, ename
  5.       from emp
Findout the Difference of sum of sal among groups department wise
================================================== ==
Expand|Select|Wrap|Line Numbers
  1. select d20_sal - d10_sal as d20_10_diff,
  2.        d20_sal - d30_sal as d20_30_diff
  3.   from (
  4. select sum(case when deptno=10 then sal end) as d10_sal,
  5.        sum(case when deptno=20 then sal end) as d20_sal,
  6.        sum(case when deptno=30 then sal end) as d30_sal
  7.   from emp
  8.        ) totals_by_dept
Also Check Povoting - 2
Sep 17 '07 #1
0 4823

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:
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
debasisdas
by: debasisdas | last post by:
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.