Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Oracle pivot运算符_Sql_Oracle - Fatal编程技术网

Sql Oracle pivot运算符

Sql Oracle pivot运算符,sql,oracle,Sql,Oracle,我是oracle pivot的新手。这可能吗 我有两列Type和Value type value --------------- a a1 b b1 c c1 etc 我能在一行中得到这样的东西吗 a b c a1 b1 c1 在尝试这样的查询时,我得到了这样的输出 select A,B from tbl pivot (max(value) for type in ('a' as A,'b' as B)) --

我是oracle pivot的新手。这可能吗

我有两列
Type
Value

type     value
---------------
a        a1
b        b1
c        c1
etc
我能在一行中得到这样的东西吗

a   b    c 
a1  b1   c1
在尝试这样的查询时,我得到了这样的输出

  select A,B from tbl
  pivot (max(value) for type in ('a' as A,'b' as B))

  ------------------------------------
    A    B
   null  b1
   a1    null
谢谢诸如此类的东西:

SELECT a, b, c
  FROM tbl
 PIVOT
 (
   MAX(Value) FOR Type IN ('a' as a, 
                           'b' as b, 
                           'c' as c)
 )
有关更多详细信息,请参阅文档。

类似以下内容:

SELECT a, b, c
  FROM tbl
 PIVOT
 (
   MAX(Value) FOR Type IN ('a' as a, 
                           'b' as b, 
                           'c' as c)
 )

有关更多详细信息,您可以参考文档。

您之所以获得这样的输出,仅仅是因为您针对一个表(您的
tbl
表)发出
select
语句,该表可能包含一列(例如主键列)它唯一地标识一行,
pivot
运算符考虑该列的值。下面是一个简单的例子:

/*assume it's your table tbl */
with tbl(unique_col, col1, col2) as(
  select 1, 'a',  'a1' from dual union all
  select 2, 'b',  'b1' from dual union all
  select 3, 'c',  'c1' from dual
)
对此类表的查询将提供您在问题中提供的输出(不需要的输出):

select A,B 
  from tbl
pivot(
  max(col2) for col1 in ('a' as A,'b' as B)
)
结果:

A    B
--   --
a1   null   
null b1
A  B
-- --
a1 b1 
为了生成所需的输出,您需要排除行中具有唯一值的列:

select A
     , B 
  from (select col1 
             , col2  /*selecting only those columns we are interested in*/
           from tbl ) 
  pivot(
    max(col2) for col1 in ('a' as A,'b' as B)
  )
结果:

A    B
--   --
a1   null   
null b1
A  B
-- --
a1 b1 

您得到这样的输出仅仅是因为您对一个表(您的
tbl
表)发出
select
语句,该表可能包含一个列(例如主键列),该列唯一地标识一行,并且
pivot
操作符考虑该列的值。下面是一个简单的例子:

/*assume it's your table tbl */
with tbl(unique_col, col1, col2) as(
  select 1, 'a',  'a1' from dual union all
  select 2, 'b',  'b1' from dual union all
  select 3, 'c',  'c1' from dual
)
对此类表的查询将提供您在问题中提供的输出(不需要的输出):

select A,B 
  from tbl
pivot(
  max(col2) for col1 in ('a' as A,'b' as B)
)
结果:

A    B
--   --
a1   null   
null b1
A  B
-- --
a1 b1 
为了生成所需的输出,您需要排除行中具有唯一值的列:

select A
     , B 
  from (select col1 
             , col2  /*selecting only those columns we are interested in*/
           from tbl ) 
  pivot(
    max(col2) for col1 in ('a' as A,'b' as B)
  )
结果:

A    B
--   --
a1   null   
null b1
A  B
-- --
a1 b1 

类型
中的值是否为已知集?是的,假设它们只是
类型
中的a、b、cAre值?是的,假设它们只是a、b、ci。我不确定我是否理解语法。。。这就是我所拥有的:从tbl pivot中选择'a'、'b'、'c'(输入的最大值('a'、'b'、'c'))我不确定我是否理解语法。。。这就是我所拥有的:从tbl pivot中选择'a'、'b'、'c'(输入'a'、'b'、'c')的最大值)是的,这就像一个符咒……感谢您的详细解释,让我理解其中的差异!!!!!是的,这很有魅力…谢谢你的详细解释,让我明白其中的区别!!!!!