如何在没有聚合的情况下进行枢转,这可能吗?

时间:2016-01-12 04:09:56

标签: sql pivot

我想按以下顺序重新排列包含行和列的表。这可能吗?如何?

enter image description here

这是我期望的结果。我怎么能这样做。

enter image description here

2 个答案:

答案 0 :(得分:3)

如果每行有多个值,那么您期望没有聚合函数?

如果每行都有一个值,那么无论使用sum,min,max还是avg聚合函数都是如此。
像这样(在oracle中):

select row_id, 'C1', 'C2', 'C3'
  from (select 1 table_id, 'C1' Column_id, 1 Row_id, 20000 "value"          from dual        union all
    select 1 table_id, 'C2' Column_id, 1 Row_id, 30000 "value"          from dual        union all
    select 1 table_id, 'C3' Column_id, 1 Row_id, 25000 "value"          from dual        union all
    select 1 table_id, 'C1' Column_id, 2 Row_id, 80200 "value"          from dual        union all
    select 1 table_id, 'C2' Column_id, 2 Row_id, 50000 "value"          from dual        union all
    select 1 table_id, 'C3' Column_id, 2 Row_id, 95000 "value" from dual) 
pivot(max("value") for column_id in('C1', 'C2','C3'))

答案 1 :(得分:1)

Select Row_ID,
     Min(Case DBColumnName When 'c1' Then Value End) c1,
     Min(Case DBColumnName When 'c2' Then Value End) c2,
     Min(Case DBColumnName When 'c3' Then Value End) c3
   From table
   Group By Row_ID

编辑:我没有编辑和编写这篇文章。没有运行SQL。我希望,你明白了。