This document summarizes a presentation titled "Make your Data Dance: UNPIVOT, PIVOT and GROUP BY Extensions" given by Stew Ashton at the DOAG 2018 conference. The presentation demonstrates how to use the UNPIVOT, PIVOT, and GROUP BY clauses in SQL to transform data between row-column formats, calculate subtotals and grand totals, and generate spreadsheet-like reports from transactional data. Examples using sales data illustrate techniques for pivoting, transposing, and grouping data at multiple levels to provide summary views.
This document is a world map that shows the names and locations of countries and territories around the world. It includes the names of all countries located in North America, South America, Europe, Africa, Asia, Australia, and Antarctica. Various oceans, seas, gulfs, and bays are also labeled on the map.
This document provides examples of PL/SQL code blocks and procedures to perform various tasks like checking if a year is a leap year, calculating employee bonuses based on salary, using cursors to retrieve and output employee data, using associative arrays to store and retrieve employee data, handling exceptions, inserting/updating/deleting records in tables using procedures and functions, creating packages with public and private constructs, overloading procedures and functions, and more. It includes 14 questions with examples to write and test PL/SQL code.
Tree is a non-linear data structure that can represent hierarchical relationships. It consists of nodes connected by edges. The root node has child nodes, and nodes may be leaf nodes or have child nodes themselves. Binary trees restrict nodes to having at most two children. General trees can be converted to binary trees by making the first child the left child and next siblings the right child. Binary trees can be built from input data by comparing values to determine left or right placement, or from traversal operations.
How to Attract Top Talent: Transforming Your Startup Into a Talent MagnetDavid Ehrenberg
Ìý
Your talent is a key driver for the long-term success of your company. If you don’t have cash, you need to use the tools of stock options, benefits, and payroll to attract top talent. This deck looks at the current marketplace, stock options vs restricted stock, using 409a valuations to set the strike price for employee stock options, PEO or open market for benefits, payroll options, employment law considerations, and more.
This document discusses B-trees and B+-trees, which are data structures used to store and retrieve data in databases. It provides details on the characteristics and functioning of B-trees and B+-trees, including how they are implemented, how nodes are structured, and how inserts and deletes are handled. Examples are given of building B-trees and B+-trees of order 5 through inserting values in order and deleting values.
1. A multi-way search tree allows nodes to have up to m children, where keys in each node are ordered and divide the search space.
2. B-trees are a generalization of binary search trees where all leaves are at the same depth and internal nodes have at least m/2 children.
3. Searching and inserting keys in a B-tree starts at the root and proceeds by comparing keys to guide traversal to the appropriate child node. Insertion may require splitting full nodes to balance the tree.
The document discusses using Oracle SQL functions like UNPIVOT, PIVOT, and GROUP BY extensions to transform and summarize data in different ways. It provides examples of using these functions to transpose columns and rows, calculate subtotals and grand totals, and generate spreadsheet-like reports from sales data with multiple grouping levels. The techniques shown allow flexible reshaping and analysis of transactional data in the database.
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensionsstewashton
Ìý
This document discusses using UNPIVOT, PIVOT and GROUP BY extensions in SQL to transform and summarize data in various ways. Specifically, it shows how to:
1) Use UNPIVOT to transform columns into rows and PIVOT to transform rows into columns.
2) Use GROUP BY with ROLLUP, CUBE and GROUPING SETS functions to generate subtotals and totals across multiple levels or dimensions of data.
3) Apply techniques like UNPIVOT, PIVOT and GROUP BY to real-world sales data to transform it into a spreadsheet-like format with rows, columns, subtotals and totals.
This document discusses movement types in Materials Management and Inventory Management (MM-IM) in SAP. It defines movement types, explains how they influence goods movements and valuations, and describes the key tables that define movement types. Movement types determine stock and value updates, screen layouts, and more. The document provides examples of common movement type configurations.
The document discusses using geodata and geospatial functions in MySQL. It provides examples of finding zip codes and points of interest within a given distance, converting a table to use spatial data types and indexes for more efficient geospatial queries, and examples querying weather data from a Mesonet database using spatial functions.
Photon Technical Deep Dive: How to Think VectorizedDatabricks
Ìý
Photon is a new vectorized execution engine powering Databricks written from scratch in C++. In this deep dive, I will introduce you to the basic building blocks of a vectorized engine by walking you through the evaluation of an example query with code snippets. You will learn about expression evaluation, compute kernels, runtime adaptivity, filter evaluation, and vectorized operations against hash tables.
An unconventional approach for ETL of historized dataAndrej Pashchenko
Ìý
Maintaining a data historization is a very common but time consuming task in a data warehouse environment. The common techniques used involve outer joins and some kind of change detection. This change detection must be done with respect of Null-values and is possibly the most trickiest part. But, on the other hand, SQL offers standard functionality with exactly desired behaviour: Group By or Partitioning with analytic functions. Can it be used for this task?
Oren nakdimon oh really... i didn't know it is supported in standard editionOren Nakdimon
Ìý
As Oracle Standard Edition developers, we tend to complain about the features that we don't have. But at the same time, there are good and useful features that are supported in Standard Edition but many developers either don't know about them or assume they are supported only in Enterprise Edition.
This session focuses on such features - introducing them, exploring them, showing when they are useful and how to use them.
4,encoder & decoder MUX and DEMUX EEng - Copy.pdfDamotTesfaye
Ìý
This document discusses various combinational logic functions including decoding, encoding, multiplexing, and decoding. It provides details on decoder and encoder circuits. Decoders accept a binary input and activate only one output corresponding to that input. Encoders have multiple inputs but activate only one at a time, producing a binary output code. Examples of 3-line to 8-line decoders and 8-line to 3-line encoders are shown with their truth tables.
This document provides information on 8088 microprocessor instruction set. It discusses:
1) The basic components of a program including instructions and machine code.
2) Examples of instruction formats and operations for data transfer, arithmetic, logical, and shift instructions.
3) Details on multiplication and division instructions including examples of multiplying and dividing operations.
4) Key benefits of assembly language such as taking up less memory and executing faster than high-level languages.
The document provides an overview of microprocessors and microcontrollers. It discusses the history and evolution of microprocessors from early minicomputers in the 1960s to modern microcontrollers that integrate CPU, memory and I/O onto a single chip. The document outlines common microprocessor components like registers, ALU, buses and control units. It also covers digital logic gates, memory systems, number representations and instruction sets.
Latin America Tour 2019 - 10 great sql featuresConnor McDonald
Ìý
By expanding our knowledge of SQL facilities, we can let all the boring work be handled via SQL rather than a lot of middle-tier code, and we can get performance benefits as an added bonus. Here are some SQL techniques to solve problems that would otherwise require a lot of complex coding, freeing up your time to focus on the delivery of great applications.
An assembler is a program that translates assembly language code into machine language code that can be executed by a computer. It performs this translation in two passes. In the first pass, it identifies symbols and calculates addresses. It builds symbol tables to track symbol values and locations. In the second pass, it uses the symbol tables to replace symbols with values or addresses and generate the machine code instructions and data. The summarizes the key components and processes of an assembler, including its data structures like symbol tables, and how it performs tasks like symbol resolution and machine code generation through its two passes.
This document discusses improvements to histograms in MariaDB. It provides background on how query optimizers use histograms to estimate condition selectivity. It describes the basic equi-width and improved equi-height histograms. It outlines how MariaDB 10.8 introduces a new JSON-based histogram type that stores exact bucket endpoints to improve accuracy, especially for popular values. The new type fixes issues the previous approaches had with inaccurate selectivity estimates for certain conditions. Overall, the document presents histograms as an important tool for query optimization and how MariaDB is enhancing their implementation.
The document discusses new features in PostgreSQL that help with creating simple sharding schemes. It describes how PostgreSQL implements partitioning via table inheritance and declarative partitioning. It also discusses how the foreign data wrapper (FDW) feature can be used with partitioning to implement sharding across multiple servers. The document notes improvements in PostgreSQL version 11 for declarative partitioning and optimization features for the postgres_fdw extension such as predicate pushdown and join pushdown to remote servers.
The document discusses various topics related to microcontroller simulation and interfacing including:
1) A simulation represents a simpler model of a real situation that can be manipulated to determine experimental results. The document discusses using a simulator to show the effects of a given situation.
2) It describes Top View Simulator which simulates the popular 8-bit MCS 51 microcontroller family and includes windows to view the program, registers, memory, and other components.
3) Examples are given of interfacing microcontrollers with common devices like LEDs, LCDs, stepper motors, and 7-segment displays through programming and circuit diagrams.
This document summarizes a study that models the diffusion of satellite-supported telematics systems in European passenger cars using the Bass and Gompertz diffusion models. Data on sales from 5 European car makers are used. Both models are estimated for three groups of car makers that show different diffusion behavior: pioneers, a follower, and laggards. The Bass model estimates show overshooting behavior for pioneers, while the Gompertz model does not. Both models estimate that pioneers will reach half market penetration by 2020, followers by 2025-2028, and laggards by 2052-2061. The Bass model generally estimates faster diffusion.
This document discusses using JSON in Oracle Database 18c/19c. It begins by introducing the presenter and their background. It then covers storing and querying JSON in the database using various SQL and PL/SQL features like JSON_QUERY, JSON_OBJECT, and JSON_TABLE. The document discusses how different SQL data types are converted to JSON. It shows examples of converting rows to CSV, XML, and JSON formats for data exchange. In summary, the document provides an overview of Oracle Database's support for JSON focusing on using it properly for data exchange and storage.
This document discusses using JSON in Oracle Database 18c/19c. It begins with an introduction of the author and their experience. It then covers how JSON is used everywhere in databases and applications now. The main topic is using JSON as intended - as a data interchange format rather than for storage. It highlights the advantages of JSON over CSV and XML for data exchange. It also demonstrates how to transform between SQL and JSON data types and discusses some of the challenges around data formats and encoding.
More Related Content
Similar to Make your data dance: PIVOT and GROUP BY in Oracle SQL (20)
The document discusses using Oracle SQL functions like UNPIVOT, PIVOT, and GROUP BY extensions to transform and summarize data in different ways. It provides examples of using these functions to transpose columns and rows, calculate subtotals and grand totals, and generate spreadsheet-like reports from sales data with multiple grouping levels. The techniques shown allow flexible reshaping and analysis of transactional data in the database.
Make your data dance: PIVOT, UNPIVOT & GROUP BY extensionsstewashton
Ìý
This document discusses using UNPIVOT, PIVOT and GROUP BY extensions in SQL to transform and summarize data in various ways. Specifically, it shows how to:
1) Use UNPIVOT to transform columns into rows and PIVOT to transform rows into columns.
2) Use GROUP BY with ROLLUP, CUBE and GROUPING SETS functions to generate subtotals and totals across multiple levels or dimensions of data.
3) Apply techniques like UNPIVOT, PIVOT and GROUP BY to real-world sales data to transform it into a spreadsheet-like format with rows, columns, subtotals and totals.
This document discusses movement types in Materials Management and Inventory Management (MM-IM) in SAP. It defines movement types, explains how they influence goods movements and valuations, and describes the key tables that define movement types. Movement types determine stock and value updates, screen layouts, and more. The document provides examples of common movement type configurations.
The document discusses using geodata and geospatial functions in MySQL. It provides examples of finding zip codes and points of interest within a given distance, converting a table to use spatial data types and indexes for more efficient geospatial queries, and examples querying weather data from a Mesonet database using spatial functions.
Photon Technical Deep Dive: How to Think VectorizedDatabricks
Ìý
Photon is a new vectorized execution engine powering Databricks written from scratch in C++. In this deep dive, I will introduce you to the basic building blocks of a vectorized engine by walking you through the evaluation of an example query with code snippets. You will learn about expression evaluation, compute kernels, runtime adaptivity, filter evaluation, and vectorized operations against hash tables.
An unconventional approach for ETL of historized dataAndrej Pashchenko
Ìý
Maintaining a data historization is a very common but time consuming task in a data warehouse environment. The common techniques used involve outer joins and some kind of change detection. This change detection must be done with respect of Null-values and is possibly the most trickiest part. But, on the other hand, SQL offers standard functionality with exactly desired behaviour: Group By or Partitioning with analytic functions. Can it be used for this task?
Oren nakdimon oh really... i didn't know it is supported in standard editionOren Nakdimon
Ìý
As Oracle Standard Edition developers, we tend to complain about the features that we don't have. But at the same time, there are good and useful features that are supported in Standard Edition but many developers either don't know about them or assume they are supported only in Enterprise Edition.
This session focuses on such features - introducing them, exploring them, showing when they are useful and how to use them.
4,encoder & decoder MUX and DEMUX EEng - Copy.pdfDamotTesfaye
Ìý
This document discusses various combinational logic functions including decoding, encoding, multiplexing, and decoding. It provides details on decoder and encoder circuits. Decoders accept a binary input and activate only one output corresponding to that input. Encoders have multiple inputs but activate only one at a time, producing a binary output code. Examples of 3-line to 8-line decoders and 8-line to 3-line encoders are shown with their truth tables.
This document provides information on 8088 microprocessor instruction set. It discusses:
1) The basic components of a program including instructions and machine code.
2) Examples of instruction formats and operations for data transfer, arithmetic, logical, and shift instructions.
3) Details on multiplication and division instructions including examples of multiplying and dividing operations.
4) Key benefits of assembly language such as taking up less memory and executing faster than high-level languages.
The document provides an overview of microprocessors and microcontrollers. It discusses the history and evolution of microprocessors from early minicomputers in the 1960s to modern microcontrollers that integrate CPU, memory and I/O onto a single chip. The document outlines common microprocessor components like registers, ALU, buses and control units. It also covers digital logic gates, memory systems, number representations and instruction sets.
Latin America Tour 2019 - 10 great sql featuresConnor McDonald
Ìý
By expanding our knowledge of SQL facilities, we can let all the boring work be handled via SQL rather than a lot of middle-tier code, and we can get performance benefits as an added bonus. Here are some SQL techniques to solve problems that would otherwise require a lot of complex coding, freeing up your time to focus on the delivery of great applications.
An assembler is a program that translates assembly language code into machine language code that can be executed by a computer. It performs this translation in two passes. In the first pass, it identifies symbols and calculates addresses. It builds symbol tables to track symbol values and locations. In the second pass, it uses the symbol tables to replace symbols with values or addresses and generate the machine code instructions and data. The summarizes the key components and processes of an assembler, including its data structures like symbol tables, and how it performs tasks like symbol resolution and machine code generation through its two passes.
This document discusses improvements to histograms in MariaDB. It provides background on how query optimizers use histograms to estimate condition selectivity. It describes the basic equi-width and improved equi-height histograms. It outlines how MariaDB 10.8 introduces a new JSON-based histogram type that stores exact bucket endpoints to improve accuracy, especially for popular values. The new type fixes issues the previous approaches had with inaccurate selectivity estimates for certain conditions. Overall, the document presents histograms as an important tool for query optimization and how MariaDB is enhancing their implementation.
The document discusses new features in PostgreSQL that help with creating simple sharding schemes. It describes how PostgreSQL implements partitioning via table inheritance and declarative partitioning. It also discusses how the foreign data wrapper (FDW) feature can be used with partitioning to implement sharding across multiple servers. The document notes improvements in PostgreSQL version 11 for declarative partitioning and optimization features for the postgres_fdw extension such as predicate pushdown and join pushdown to remote servers.
The document discusses various topics related to microcontroller simulation and interfacing including:
1) A simulation represents a simpler model of a real situation that can be manipulated to determine experimental results. The document discusses using a simulator to show the effects of a given situation.
2) It describes Top View Simulator which simulates the popular 8-bit MCS 51 microcontroller family and includes windows to view the program, registers, memory, and other components.
3) Examples are given of interfacing microcontrollers with common devices like LEDs, LCDs, stepper motors, and 7-segment displays through programming and circuit diagrams.
This document summarizes a study that models the diffusion of satellite-supported telematics systems in European passenger cars using the Bass and Gompertz diffusion models. Data on sales from 5 European car makers are used. Both models are estimated for three groups of car makers that show different diffusion behavior: pioneers, a follower, and laggards. The Bass model estimates show overshooting behavior for pioneers, while the Gompertz model does not. Both models estimate that pioneers will reach half market penetration by 2020, followers by 2025-2028, and laggards by 2052-2061. The Bass model generally estimates faster diffusion.
This document discusses using JSON in Oracle Database 18c/19c. It begins by introducing the presenter and their background. It then covers storing and querying JSON in the database using various SQL and PL/SQL features like JSON_QUERY, JSON_OBJECT, and JSON_TABLE. The document discusses how different SQL data types are converted to JSON. It shows examples of converting rows to CSV, XML, and JSON formats for data exchange. In summary, the document provides an overview of Oracle Database's support for JSON focusing on using it properly for data exchange and storage.
This document discusses using JSON in Oracle Database 18c/19c. It begins with an introduction of the author and their experience. It then covers how JSON is used everywhere in databases and applications now. The main topic is using JSON as intended - as a data interchange format rather than for storage. It highlights the advantages of JSON over CSV and XML for data exchange. It also demonstrates how to transform between SQL and JSON data types and discusses some of the challenges around data formats and encoding.
This document discusses using JSON in Oracle Database 18c/19c. It begins by introducing the author and their background. It then covers how JSON is used in various Oracle technologies like APEX, ORDS, SQL Developer, and more. The document explains why JSON is well-suited for data exchange compared to CSV and XML. It provides examples of transforming data between SQL and JSON formats using functions like JSON_OBJECT and JSON_QUERY. Finally, it discusses considerations for storing JSON in database tables and converting between SQL and JSON data types.
The document discusses date and time data types in Oracle 12c, including date ranges and temporal validity. It provides examples of:
1) Creating a table with a period for presence to define valid time ranges for employee records. This creates hidden start and end date columns.
2) Querying the table as of a specific time to filter for valid records.
3) Using versions to query past states of records within a specified time range.
4) Techniques that can be used with Oracle to implement temporal constraints, indexing, and queries to handle date range overlaps, gaps, and intersections.
This document summarizes Stew Ashton's presentation on using Oracle Database 12c's MATCH_RECOGNIZE clause to solve various "row pattern matching" problems in a more efficient way than pre-12c solutions. The document provides examples of using MATCH_RECOGNIZE for problems involving identifying consecutive values, grouping data into fixed bins, and distributing values evenly across bins. It shows that MATCH_RECOGNIZE offers performance improvements and simpler solutions compared to earlier approaches using window functions, self-joins and the MODEL clause.
A Relative Information Gain-based Query Performance Prediction Framework with...suchanadatta3
Ìý
To improve the QPP estimate for neural models, we propose to use additional information from a set of queries that express a similar information need to the current one (these queries are called variants). The key idea of our proposed method, named Weighted Relative Information Gain (WRIG), is to estimate the performance of these variants, and then to improve the QPP estimate of the original query based on the relative differences with the variants. The hypothesis is that if a query’s estimate is significantly higher than the average QPP score of its variants, then the original query itself is assumed (with a higher confidence) to be one for which a retrieval model works well.
Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures Data Science Lectures
Design Data Model Objects for Analytics, Activation, and AIaaronmwinters
Ìý
Explore using industry-specific data standards to design data model objects in Data Cloud that can consolidate fragmented and multi-format data sources into a single view of the customer.
Design of the data model objects is a critical first step in setting up Data Cloud and will impact aspects of the implementation, including the data harmonization and mappings, as well as downstream automations and AI processing. This session will provide concrete examples of data standards in the education space and how to design a Data Cloud data model that will hold up over the long-term as new source systems and activation targets are added to the landscape. This will help architects and business analysts accelerate adoption of Data Cloud.
CH. 4.pptxt and I will be there in aboutmiesoabdela57
Ìý
The reason why I am not words that start with a good things to do anything else 😉😉 and I will be there in about you that go against the future is only today and tomorrow is unborn child hood I have a few that you told to us the reason why I was children and I will be
The Role of Christopher Campos Orlando in Sustainability Analyticschristophercamposus1
Ìý
Christopher Campos Orlando specializes in leveraging data to promote sustainability and environmental responsibility. With expertise in carbon footprint analysis, regulatory compliance, and green business strategies, he helps organizations integrate sustainability into their operations. His data-driven approach ensures companies meet ESG standards while achieving long-term sustainability goals.
Boosting MySQL with Vector Search Scale22X 2025.pdfAlkin Tezuysal
Ìý
As the demand for vector databases and Generative AI continues to rise, integrating vector storage and search capabilities into traditional databases has become increasingly important. This session introduces the *MyVector Plugin*, a project that brings native vector storage and similarity search to MySQL. Unlike PostgreSQL, which offers interfaces for adding new data types and index methods, MySQL lacks such extensibility. However, by utilizing MySQL's server component plugin and UDF, the *MyVector Plugin* successfully adds a fully functional vector search feature within the existing MySQL + InnoDB infrastructure, eliminating the need for a separate vector database. The session explains the technical aspects of integrating vector support into MySQL, the challenges posed by its architecture, and real-world use cases that showcase the advantages of combining vector search with MySQL's robust features. Attendees will leave with practical insights on how to add vector search capabilities to their MySQL
Deep-QPP: A Pairwise Interaction-based Deep Learning Model for Supervised Que...suchanadatta3
Ìý
Motivated by the recent success of end-to-end deep neural models
for ranking tasks, we present here a supervised end-to-end neural
approach for query performance prediction (QPP). In contrast to
unsupervised approaches that rely on various statistics of document
score distributions, our approach is entirely data-driven. Further,
in contrast to weakly supervised approaches, our method also does
not rely on the outputs from different QPP estimators. In particular, our model leverages information from the semantic interactions between the terms of a query and those in the top-documents retrieved with it. The architecture of the model comprises multiple layers of 2D convolution filters followed by a feed-forward layer of parameters. Experiments on standard test collections demonstrate
that our proposed supervised approach outperforms other state-of-the-art supervised and unsupervised approaches.
AI + Disability. Coded Futures: Better opportunities or biased outcomes?Christine Hemphill
Ìý
A summary report into attitudes to and implications of AI as it relates to disability. Will AI enabled solutions create greater opportunities or amplify biases in society and datasets? Informed by primary mixed methods research conducted in the UK and globally by Open Inclusion on behalf of the Institute of People Centred AI, Uni of Surrey and Royal Holloway University. Initially presented at Google London in Jan 2025.
Make your data dance: PIVOT and GROUP BY in Oracle SQL
1. Make your Data Dance
UNPIVOT, PIVOT and GROUP BY Extensions
Stew Ashton
DOAG 2018
Can you read the following line? If not, please move closer.
It's much better when you can read the code ;)
2. Who am I?
• 1981-2015:
– software engineer / application architect
– From Mainframe to client / server to Web
• 2005-present: focus on Oracle DB development
– Contribute to asktom & ODC/OTN SQL forum
– Presented at OOW, UKOUG tech, DOAG, ilOUG
– Advocate of data-centric application architecture
2
3. Data with Subtotals?
REGION COUNTRY CUSTOMER SALE_DATE QTY AMOUNT
Europe DE Raina Silverberg 2017-01-23 2 110
Europe DE Raina Silverberg 2017-02-19 3 220
3
Europe DE Gloria Saintclair 2017-01-27 2 140
Europe DE Gloria Saintclair 2017-02-26 3 280
Europe DE Gloria Saintclair (Subtotal) 5 420
Europe DE Gloria Saintclair 2017-01-27 2 140
Europe DE Gloria Saintclair 2017-02-26 3 280
Europe DE Raina Silverberg (Subtotal) 5 330
4. Totals and Grand Total?
REGION COUNTRY CUSTOMER SALE_DATE QTY AMOUNT
Europe DE Raina Silverberg 2017-01-23 2 110
Europe DE Raina Silverberg 2017-02-19 3 220
4
Europe DE Gloria Saintclair 2017-01-27 2 140
Europe DE Gloria Saintclair 2017-02-26 3 280
Europe DE Raina Silverberg (Subtotal) 5 330
Europe DE Gloria Saintclair (Subtotal) 5 420
Europe DE (Subtotal) 10 750
Europe (Total) 10 750
(Grand total) 10 750
5. Columns to Rows?
5
A C
1A
2A
1
2
3A3
4
5
6
1B
2B
3B
7
8
9
1C
2C
3C
B
1B
2B
3B
1C
2C
3C
6. Rows to Columns?
6
A C
1A
2A
1
2
3A3
4
5
6
4A
5A
6A
7
8
9
7A
8A
9A
B
4A
5A
6A
7A
8A
9A
9. How?
• UNPIVOT
• PIVOT
• GROUP BY extensions
– ROLLUP
– CUBE
– GROUPING SETS
• GROUPING_ID() and GROUPING() functions
9
10. UNPIVOT: columns to rows
10
KEY COL VAL
1 A 1A
KEY A B
1 1A 1B
1 B 1B
1
with data(KEY,A,B) as (
select 1, '1A', '1B' from dual
)
select * from data
unpivot(VAL for COL in(A,B));
KEY A B
1 1A 1B1
Metadata becomes data
11. KEYKEY 'A' 'B'
1A 1B1
PIVOT: rows to columns
11
KEY COL VAL
1 A 1A
1 B 1B
select * from data
unpivot(VAL for COL in(A,B));
pivot(max(VAL) for COL in('A','B')); Data becomes metadata
1 C 1C
Only previously known COLs are output
12. KEY A_N A_V
1 VA1
Multiple Columns
12
select * from data
unpivot((N, V) for COL in ((A_N, A_V) as 'A',(B_N, B_V) as 'B'))
pivot( max(n) as N, max(v) as V for COL in ('A' as A,'B' as B));
B_N B_V
2 VB
KEY COL N
A 11
V
VA
B 21 VB
1
Round trip!
14. Matrix: rotate!
14
A C
1A
2A
1
2
3A3
B
1B
2B
3B
1C
2C
3C
with normalized as (
select k r, c, v from t
unpivot(v for c in(
a as 1,b as 2,c as 3
))
)
, rotated as (
select 4-c r, r c, v
from normalized
)
select * from rotated
pivot(max(v) for c in(1,2,3))
order by r;
Normalize to cells (row, column, value),
rotate, display
16. 16
OBJ_ID F T
C 1(null)
C 25
C 34
O 13
O 24
O 5(null)
select * from range_input
17. 17
select * from range_input
unpivot (
F for FT in (F as 1, T as -1)
)
OBJ_ID FT F
C 11
C 12
C -15
C 13
C -14
O 11
O -13
O 12
O -14
O 15
OBJ_ID F T
C 1(null)
C 25
C 34
O 13
O 24
O 5(null)
18. 18
select * from range_input
unpivot include nulls (
F for FT in (F as 1, T as -1)
)
OBJ_ID FT F
C 11
C -1(null)
C 12
C -15
C 13
C -14
O 11
O -13
O 12
O -14
O 15
O -1(null)
OBJ_ID FT F
C 11
C 12
C -15
C 13
C -14
O 11
O -13
O 12
O -14
O 15
19. 19
with unpivoted as (
select * from range_input
unpivot include nulls (
F for FT in (F as 1, T as -1)
)
)
select OBJ_ID, F, T, NUM_R
from (
select a.*,
lead(F) over(partition by obj_id order by F) T,
sum(FT) over(partition by obj_id order by F) NUM_R
from unpivoted a
) where F < nvl(T,F+1) and NUM_R > 0
order by obj_id, T;
OBJ_ID F T NUM_R
C 1 2 1
C 2 3 2
C 3 4 3
C 4 5 2
C 5 (null) 1
O 1 2 1
O 2 3 2
O 3 4 1
O 5 (null) 1
20. Grouping Sales History
• 2 years
• 2 regions
• 2 countries per region
• 2 customers per country
• 2 sales per year per customer
20
25. 25
select region, country,
sum(amount) amount
from sales_yr
group by grouping sets (
(),
region,
(region, country)
);
REGION COUNTRY AMOUNT
Europe DE 1500
Europe FR 7140
Europe 8640
Americas CA 8820
Americas US 900
Americas 9720
18360
What if I want 3 levels?
26. 26
select region, country,
sum(amount) amount
from sales_yr
group by rollup(region, country);
REGION COUNTRY AMOUNT
Europe DE 1500
Europe FR 7140
Europe 8640
Americas CA 8820
Americas US 900
Americas 9720
18360
27. 27
select region, country,
sum(amount) amount
from sales_yr
group by cube(region, country);
REGION COUNTRY AMOUNT
18360
CA 8820
DE 1500
FR 7140
US 900
Europe 8640
Europe DE 1500
Europe FR 7140
Americas 9720
Americas CA 8820
Americas US 900
What if I have NULL data?
28. 28
select
region, country,
sum(amount) amount
from sales_yr
group by cube(region, country);
REGION COUNTRY AMOUNT
18360
CA 8820
DE 1500
FR 7140
US 900
Europe 8640
Europe DE 1500
Europe FR 7140
Americas 9720
Americas CA 8820
Americas US 900
29. 29
select
grouping(country) g_c,
region, country,
sum(amount) amount
from sales_yr
group by cube(region, country);
G_C REGION COUNTRY AMOUNT
1 18360
0 CA 8820
0 DE 1500
0 FR 7140
0 US 900
1 Europe 8640
0 Europe DE 1500
0 Europe FR 7140
1 Americas 9720
0 Americas CA 8820
0 Americas US 900
30. 30
select
grouping(region)*2 g_r,
grouping(country) g_c,
region, country,
sum(amount) amount
from sales_yr
group by cube(region, country);
G_R G_C REGION COUNTRY AMOUNT
2 1 18360
2 0 CA 8820
2 0 DE 1500
2 0 FR 7140
2 0 US 900
0 1 Europe 8640
0 0 Europe DE 1500
0 0 Europe FR 7140
0 1 Americas 9720
0 0 Americas CA 8820
0 0 Americas US 900
31. 31
select
grouping_id(region, country) g_rc,
grouping(region)*2 g_r,
grouping(country) g_c,
region, country,
sum(amount) amount
from sales_yr
group by cube(region, country);
G_RC G_R G_C REGION COUNTRY AMOUNT
3 2 1 18360
2 2 0 CA 8820
2 2 0 DE 1500
2 2 0 FR 7140
2 2 0 US 900
1 0 1 Europe 8640
0 0 0 Europe DE 1500
0 0 0 Europe FR 7140
1 0 1 Americas 9720
0 0 0 Americas CA 8820
0 0 0 Americas US 900
32. select deptno,
job, sum(sal) sal
from emp
group by cube(deptno, job);
32
select
deptno,
job, sum(sal) sal
from emp
group by cube(deptno, job);
DEPTNO JOB SAL
29025
CLERK 4150
ANALYST 6000
MANAGER 8275
SALESMAN 5600
PRESIDENT 5000
10 8750
10 CLERK 1300
10 MANAGER 2450
10PRESIDENT 5000
20 10875
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
30 9400
30 CLERK 950
30 MANAGER 2850
30SALESMAN 5600
JOB 10 20 30 TOTAL
ANALYST 6000 6000
CLERK 1300 1900 950 4150
MANAGER 2450 2975 2850 8275
PRESIDENT 5000 5000
SALESMAN 5600 5600
(Total) 8750 10875 9400 29025
SpreadSheet-like Data from EMP
33. 33
select case grouping(deptno)
when 1 then -1 else deptno
end deptno,
job, sum(sal) sal
from emp
group by cube(deptno, job);
DEPTNO JOB SAL
-1 29025
-1 CLERK 4150
-1 ANALYST 6000
-1 MANAGER 8275
-1SALESMAN 5600
-1PRESIDENT 5000
10 8750
10 CLERK 1300
10 MANAGER 2450
10PRESIDENT 5000
20 10875
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 2975
30 9400
30 CLERK 950
30 MANAGER 2850
30SALESMAN 5600
35. 35
select case gr_job
when 1 then '(Total)' else job
end job,
"10", "20", "30", "(Total)"
from (
select case grouping(deptno)
when 1 then -1 else deptno
end deptno,
job, sum(sal) sal,
grouping(job) gr_job
from emp
group by cube(deptno, job)
)
pivot(max(sal) for deptno in (
10, 20, 30, -1 as TOTAL
))
order by gr_job, job;
JOB 10 20 30 TOTAL
ANALYST 6000 6000
CLERK 1300 1900 950 4150
MANAGER 2450 2975 2850 8275
PRESIDENT 5000 5000
SALESMAN 5600 5600
(Total) 8750 10875 9400 29025
36. SpreadSheet-like Data from SALES
• 4 vertical levels
– Grand total, region, country, customer
• 2 horizontal levels
– Year, total all years
• 2 sums
– QTY, AMOUNT
36
37. 37
select * from (
select
decode(grouping_id(REGION,COUNTRY,CUSTOMER),7,'Total',REGION) REGION,
decode(grouping_id(REGION,COUNTRY,CUSTOMER),3,'Total',COUNTRY) COUNTRY,
decode(grouping_id(REGION,COUNTRY,CUSTOMER),1,'Total',CUSTOMER) CUSTOMER,
decode(grouping(YR),1,'Total',YR) YR,
sum(QTY) QTY,
sum(AMOUNT) AMOUNT
from sales_yr
group by grouping sets(
rollup(REGION,COUNTRY,CUSTOMER,YR),
rollup(YR,REGION,COUNTRY)
)
)
pivot(max(qty) as q,max(amount) as a for (yr) in (
('2016') as "2016",
('2017') as "2017",
('Total') as "Total"
))
order by nullif(region,'Total'),nullif(country,'Total'),nullif(customer,'Total')
38. 38
REGION COUNTRY CUSTOMER 2016_Q 2016_A 2017_Q 2017_A Total_Q Total_A
Americas CA Alyce Gray 13 1530 13 1530 26 3060
Americas CA Salena Grace 21 2880 21 2880 42 5760
Americas CA (Total) 34 4410 34 4410 68 8820
Americas US Linette Ingram 3 210 3 210 6 420
Americas US Vida Puleo 3 240 3 240 6 480
Americas US (Total) 6 450 6 450 12 900
Americas (Total) 40 4860 40 4860 80 9720
Europe DE Gloria Saintclair 5 420 5 420 10 840
Europe DE Raina Silverberg 5 330 5 330 10 660
Europe DE (Total) 10 750 10 750 20 1500
Europe FR Koren Macdonald 17 2100 17 2100 34 4200
Europe FR Madelaine Gottlieb 11 1470 11 1470 22 2940
Europe FR (Total) 28 3570 28 3570 56 7140
Europe (Total) 38 4320 38 4320 76 8640
(Total) 78 9180 78 9180 156 18360
39. Make your Data Dance
UNPIVOT, PIVOT and GROUP BY Extensions
Stew Ashton
DOAG 2018
Blog:
https://stewashton.wordpress.com/2018/05/31/pivot-function-with-totals/
Twitter: @stewashton