简体   繁体   English

Oracle 10g和11g r2中的数据透视

[英]Pivot in Oracle 10g and 11g r2

I have a table that I need to pivot. 我有一张需要旋转的桌子。 I am using oracle 11g R2 on my system and the server machine has oracle 10g running on it. 我在系统上使用oracle 11g R2,并且服务器计算机上正在运行oracle 10g。

My table contains data: Origin Table: ppl 我的表包含数据:原始表:ppl

ename | edate
emp1 | 01-10-17
emp1 | 01-10-17
emp1 | 01-10-17
emp1 | 02-10-17
emp2 | 01-10-17
emp3 | 01-10-17
emp3 | 01-10-17
emp3 | 02-10-17
emp3 | 02-10-17

Normally in pivot, output should be: output table 1: 通常在数据透视图中,输出应为:输出表1:

ename | 01-10-17 | 02-10-17
emp1  |      3   |      1
emp2  |      1   |      0
emp3  |      2   |      2

But here there is a condition that for each day count has to be >1. 但是这里有一个条件,每天的计数必须大于1。 So desired output will be: output table 2: 因此,期望的输出将是:输出表2:

ename | 01-10-17 | 02-10-17
emp3  |      2   |      2

I used pivot in 11g to get required output and the output is partially correct. 我在11g中使用了数据透视表以获取所需的输出,并且输出部分正确。

select * from
(
  SELECT  ename, edate
  FROM ppl
)
PIVOT
(
  COUNT(edate)
  FOR edate in('01-oct-2017', '02-oct-2017')
)
order by ename;

The above query returns only the pivot in output table 1. My question 1 is where can I put filter in pivot query? 上面的查询仅在输出表1中返回枢轴。 我的问题1是我可以在哪里将过滤器放入枢轴查询中?

On trying to get the same output in 10g, I found out that pivot command is not supported there. 尝试在10g中获得相同的输出时,我发现那里不支持ivot命令。 So here is my query for 10g: 所以这是我对10g的查询:

select ename,
  count(case when edate ='01-10-2017' then ename end) "01-OCT-2017",
  count(case when edate ='02-10-2017' then ename end) "02-OCT-2017"
from ppl
group by ename
having count(case when edate ='01-10-2017' then ename end) >1
and count(case when edate ='02-10-2017' then ename end) >1
order by ename;

This gives the exact output 2 table as desired. 这将根据需要提供确切的输出2表。 My question 2 is that the dates can range to 30 days even. 我的问题2是,日期范围甚至可以是30天。 As I can understand, for that case I need to write "count(case when edate ='01-10-2017' then ename end) "01-OCT-2017"" and "count(case when edate ='01-10-2017' then ename end) >1" 30 times. 据我了解,在这种情况下,我需要编写“ count(edate = '01 -10-2017'时的名称,然后ename结尾)“ 01-OCT-2017”“和” count(edate = '01 -10时的情况) -2017',然后将名称结尾)> 1“ 30次。 Will not such a big query will hamper the time of output? 这样大的查询会不会影响输出时间? Or is there any feasible way to do this better? 还是有任何可行的方法可以更好地做到这一点?

Thank you for help..!! 谢谢你的帮助..!!

Q1. Q1。 The where clause may be included prior to the pivot: 在该数据透视表之前可以包含where子句:

select * from
(
  SELECT  ename, edate
  FROM ppl

  WHERE .......
)
PIVOT
(
  COUNT(edate)
  FOR edate in('01-oct-2017', '02-oct-2017')
)
order by ename;

Q2. Q2。 Yes you can have many case expressions, it will not be a big performance hit. 是的,您可以有很多case表达式,这不会对性能造成很大的影响。

Or is there any feasible way to do this better? 还是有任何可行的方法可以更好地做到这一点? You could implement it through "dynamic SQL" instead, but the query structure will remain the same. 您可以改为通过“动态SQL”实现它,但是查询结构将保持不变。 Dynamic sql just builds the query SQL for you. 动态sql只是为您构建查询SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM