473,396 Members | 1,917 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,396 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 4820

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
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,...

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.