查询以使每一行成为单行

时间:2018-07-02 09:54:42

标签: sql oracle

问题:我必须使用SQL语句将每一行都列为一列。

源表

Colour_id   Colour_desc Value1_min  Value1_max  Value2_min  Value2_max
1           Green          0          0.499        0        0.299
2           Amber          0.5        0.8         0.03      0.05

我希望输出如下所示:

Value1_min_Green    Value1_max_Green    Value2_min_Green    Value2_max_Green    Value1_min_Amber    Value1_max_Green    Value2_min_Amber    Value2_max_Green
0                     0.499               0                 0.299          0.5                       0.8                0.03                           0.05

是否有办法获得高于结果的结果?

2 个答案:

答案 0 :(得分:1)

在1行输出中合并2行值(source是表名):

select green.Value1_min as Value1_min_Green green.Value1_max as Value1_max_Green,
       green.Value2_min as Value2_min_Green green.Value2_max as Value2_max_Green,
       amber.Value1_min as Value1_min_Amber amber.Value1_max as Value1_max_Amber,
       amber.Value2_min as Value2_min_Amber amber.Value2_max as Value2_max_Amber
  from source as green, source as amber
 where amber.Colour_desc = 'Amber'
   and green.Colour_desc = 'Green'

答案 1 :(得分:1)

您可以使用条件聚合:

<svg xmlns="http://www.w3.org/2000/svg" width="43.4" height="43.4">
       <circle class="grade1" cx="21.7" cy="21.7" r="19.2"></circle>
    </svg>

添加其他颜色非常容易-并不会增加性能开销,因为查询已经在进行聚合。