遇到一个难题,须要将单列数据根据分列显示,须要用到Pivot语法。html

参照现有的query,写了一段相似如下script片断的sql语句,问题搞定。经验总结:能用SQL搞定的问题,就不要人工去处理(效率低、易出错)。sql

OCD

AS

(

SELECT *

FROM (

SELECT DISTINCT

ra.HDP_ID,

'OCD' || CAST(ROW_NUMBER() OVER(PARTITION BY ra.HDP_ID

ORDER BY ra.HRA_ID desc) AS VARCHAR(10)) Seq,

p.PN_ID AS TNID,

p.PERSON_FULL_NAME AS OCD

FROM HDDO.HRA_V ra,

HDDO.PERSON_V p

WHERE p.HP_ID = ra.HP_ID

AND ra.CONTACT_ROLE = 'OCD'

)

PIVOT (

MAX(TNID) as TNID,

MAX(OCD)

FOR Seq in('OCD1' AS OCD1, 'OCD2' AS OCD2, 'OCD3' AS OCD3,'OCD4' AS OCD4,'OCD5' AS OCD5)

)

),

-----------------------------------------------------------------------------------------------------------------------------------

oracle

Pivot

Present information in a spreadsheet-type crosstab report from any relational table using simple SQL.app

Pivot

As you know, relational tables are, well, tabular—that is, they are presented in a column-value pair. Consider the case of a table named CUSTOMERS.ui

SQL> desc customers

Name Null? Type

----------------------------------------- -------- ---------------------------

CUST_ID NUMBER(10)

CUST_NAME VARCHAR2(20)

STATE_CODE VARCHAR2(2)

TIMES_PURCHASED NUMBER(3)

When this table is selected: this

select cust_id, state_code, times_purchased

from customers

order by cust_id;

The output is: spa

CUST_ID STATE_CODE TIMES_PURCHASED

------- ---------- ---------------

1 CT 1

2 NY 10

3 NJ 2

4 NY 4

...

and so on ...

Note how the data is represented as rows of values: For each customer, the record shows the customer's home state and how many times the customer purchased something from the store. As the customer purchases more items from the store, the column times_purchased is updated.

rest

Now consider a case where you want to have a report of the purchase frequency each state - that is, how many customers bought something only once, twice, thrice and so on, from each state. In regular SQL, you can issue the following statement:

select state_code, times_purchased, count(1) cnt

from customers

group by state_code, times_purchased;

Here is the output:

ST TIMES_PURCHASED CNT

-- --------------- ----------

CT 0 90

CT 1 165

CT 2 179

CT 3 173

CT 4 173

CT 5 152

...

and so on ...

This is the information you want but it's a little hard to read. A better way to represent the same data may be through the use of crosstab reports, in which you can organized the data vertically and states horizontally, just like a spreadsheet:

Times_purchased

CT NY NJ ...

and so on ...

1 0 1 0 ...

2 23 119 37 ...

3 17 45 1 ...

...

and so on ...

Prior to Oracle Database 11g, you would do that via some sort of a decode function for each value and write each distinct value as a separate column. The technique is quite nonintuitive however.

Fortunately, you now have a great new feature called PIVOT for presenting any query in the crosstab format using a new operator, appropriately named pivot. Here is how you write the query:

select * from (

select times_purchased, state_code

from customers t

)

pivot

(

count(state_code)

for state_code in ('NY','CT','NJ','FL','MO')

)

order by times_purchased

/

Here is the output:

. TIMES_PURCHASED 'NY' 'CT' 'NJ' 'FL' 'MO'

--------------- ---------- ---------- ---------- ---------- ----------

0 16601 90 0 0 0

1 33048 165 0 0 0

2 33151 179 0 0 0

3 32978 173 0 0 0

4 33109 173 0 1 0

... and so on ...

This shows the power of the pivot operator. The state_codes are presented on the header row, instead of a column. Pictorially, here is the how the traditional tabular format looks:

Figure 1 Traditional tabular representation

In a crosstab report, you want to transpose the Times Purchased column to the header row as shown in Figure 2. The column becomes the row, as if the column were rotated 90 degrees anti-clockwise to become the header row. This figurative rotation needs to have a pivot point and in this case the pivot point happens to be the count(state_code) expression.

Figure 2 Pivoted representation

This expression needs to be in the syntax of the query:

...

pivot

(

count(state_code)

for state_code in ('NY','CT','NJ','FL','MO')

)

...

The second line, "for state_code ...," limits the query to only those values. This line is necessary, so unfortunately you have to know the possible values beforehand. This restriction is relaxed in the XML format of the query, described later in this article.

Note the header rows in the output:

. TIMES_PURCHASED 'NY' 'CT' 'NJ' 'FL' 'MO'

--------------- ---------- ---------- ---------- ---------- ----------

The column headers are the data from the table itself: the state codes. The abbreviations may be self explanatory but suppose you want to display the state names instead of abbreviations, ("Connecticut" instead of "CT")? In that case you have to make a little adjustment in the query, in the FOR clause as shown below:

select * from (

select times_purchased as "Puchase Frequency", state_code

from customers t

)

pivot

(

count(state_code)

for state_code in ('NY' as "New York",'CT' "Connecticut",'NJ' "New Jersey",'FL' "Florida",'MO' as "Missouri")

)

order by 1

/

Puchase Frequency New York Connecticut New Jersey Florida Missouri

----------------- ---------- ----------- ---------- ---------- ----------

0 16601 90 0 0 0

1 33048 165 0 0 0

2 33151 179 0 0 0

3 32978 173 0 0 0

4 33109 173 0 1 0

...

and so on ...

The FOR clause can have aliases for the values there, which will become the column headers.



code函数oracle列子,Oracle Pivot函数语法详解及应用实例相关推荐

  1. Oracle 分析函数(10G)语法详解

    Oracle 分析函数(10G)    好东西收藏.   一.Oracle分析函数简介 1.分析函数,最早是从ORACLE8.1.6开始出现的,它的设计目的是为了解决诸如"累计计算" ...

  2. oracle树结构查询----connect by语法详解

    connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 connect by 条件2 where 条件3; 例: ...

  3. oracle中datepart函数,Asp DatePart 函数的语法详解(用于计算日期并返回指定的时间间隔)...

    Asp DatePart 函数的语法详解(用于计算日期并返回指定的时间间隔) 更新时间:2012年07月31日 21:32:58   作者: ASP(VBScript) 参考手册中,已经对 DateP ...

  4. Oracle创建表语句(Create table)语法详解及示例

    Oracle创建表语句(Create table)语法详解及示例   创建表(Create table)语法详解 1. ORACLE常用的字段类型ORACLE常用的字段类型有 VARCHAR2 (si ...

  5. SQL全方位攻略:5. SQL “方言”大比拼:Oracle、MySQL、PostgreSQL限制检索行数语法详解(分页查询)

    系列文章目录 SQL全方位攻略:1.数据库介绍 SQL全方位攻略:2.SQL介绍 SQL全方位攻略:3.SQL标准 SQL全方位攻略:4. 标准SQL和SQL"方言" 文章目录 系 ...

  6. 创建emp表 oracle,Oracle中创建和管理表详解

    Oracle中创建和管理表详解 更新时间:2013年08月01日 15:44:16   作者: 以下是对Oracle中的创建和管理表进行了详细的分析介绍,需要的朋友可以过来参考下 SQL> /* ...

  7. Oracle中游标Cursor基本用法详解

    这篇文章主要介绍了Oracle中游标Cursor基本用法详解,还是比较全面的,具有一定参考价值,需要的朋友可以了解下. 查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT ...

  8. oracle中minus作的应用,Oracle minus用法详解及应用实例

    Oracle minus用法详解及应用实例 Oracle minus用法 "minus"直接翻译为中文是"减"的意思,在Oracle中也是用来做减法操作的,只不 ...

  9. oracle spool 分隔符_sqlplus--spool命令参数详解

    sqlplus--SPOOL参数详解 Spool是Oracle快速导出数据的工具,是sqlplus的指令,不是sql语法里的东西 一.Spool常用的设置 set arraysize 5000;  / ...

最新文章

  1. JavaScript去除字符串首尾空格
  2. 发布国内首个无服务器容器服务,运维效率从未如此高效
  3. 硬盘基本知识(磁头、磁道、扇区、柱面) 转
  4. C4C Adapt menu debugging
  5. LeetCode-210 Course Schedule II
  6. 基于Vue.js 2.x系列 + Element UI + RBAC/AUTH权限 的响应式后台管理系统
  7. cad2012打开后闪退_2012cad闪退怎么解决win10_cad2012闪退win10系统如何修复
  8. 51 Best DevOps Tools for #DevOps Engineers
  9. unity 删除父物体下所有子物体
  10. 数字金额大写转换Java工具类
  11. 21秋期末考试工程项目管理10324k2 (2)
  12. 微信小程序,不可不知的一二三四
  13. JVM学习之---了解JIT
  14. 罗裳轻解,看C++怎样拥java入怀……
  15. 恩智浦并购飞思卡尔后的十大优势
  16. 黑群晖 升级 失联 拯救方案(不丢数据)
  17. 火山引擎成为全球边缘计算大会深圳站合作伙伴
  18. ASP.NET 用 FlexPaper 在页面上显示 PDF 文件
  19. WifiDisplay开启流程
  20. dma接收双缓存 stm32_STM32和WM8960 I2S 利用DMA双缓冲音频播放和录音(二)

热门文章

  1. 本地html如何导出pdf,html表格以pdf格式导出到本地
  2. Win10彻底关闭恢复功能、省流量终极设置
  3. 澎思科技获IDG资本数千万元Pre-A轮融资,推出AI安防全场景软硬件解决方案
  4. Socket开发框架之消息的回调处理
  5. 大型网站技术学习-3. 容器Docker与kubernetes
  6. 10-20C#基础---一维、二维数组冒泡排序
  7. js判断客户浏览器类型,版本
  8. ASA 5505 配置
  9. web服务器 linux+apache+tomcat+mysql+jsp+php 整合安装
  10. 微软-IT-解决方案-统一沟通-发布会