按查询分组的Oracle SQL条件聚集函数

时间:2019-01-07 06:49:56

标签: sql oracle conditional aggregate

我正在尝试编写一个查询,该查询将根据驱动聚合的用户提供的表聚合表中的数据。当我只使用sum语句时,它可以正常工作,但是当我将sum放在case语句中以允许用户指定sum,count,mean等时,我会按错误分组。

我替换了:

 sum(column) 

具有:

 CASE b.calculationtype
      WHEN 'SUM'                  THEN SUM(column)
      WHEN 'MEAN'                 THEN AVG(column)
      WHEN 'COUNT'                THEN COUNT(column)
      WHEN 'VARIANCE'             THEN VARIANCE(column)
      WHEN 'STANDARD DEVIATION'   THEN STDDEV(column)
 END

在按功能评估分组时,Oracle是否超越了case语句?还是我很不幸尝试根据表b中的值更改实际的聚合功能?

我总是可以蛮力地将它强行使用,并将calculationtype逻辑移到实际查询之外,但这似乎有些痛苦,因为我将有5个相同的查询,并根据计算类型调用不同的聚合函数字段。

   select b.REPORT,
          case b.AGG_VARIABLE_A_FLAG
           when 'N' then null
           when 'Y' then a.AGG_VARIABLE_A
          end,
          case b.AGG_VARIABLE_B_FLAG
           when 'N' then null
           when 'Y' then a.AGG_VARIABLE_B
          end,

          --<<< problem starts >>>

          case b.CALCULATIONTYPE
           when 'SUM' then sum(a.column1)  when 'MEAN' then avg(a.column1)  when 'COUNT' then count(a.column1)  when 'VARIANCE' then variance(a.column1)  when 'STANDARD DEVIATION' then stddev(a.column1)
          end,
          case b.CALCULATIONTYPE
           when 'SUM' then sum(a.column2)  when 'MEAN' then avg(a.column2)  when 'COUNT' then count(a.column2)  when 'VARIANCE' then variance(a.column2)  when 'STANDARD DEVIATION' then stddev(a.column2)
          end

          --<<< problem ends >>

     from DATA_TABLE a
             cross join CONTROL_TABLE b
    where a.ID = bind_variable_id
          and a.SOURCEARRAY = b.SOURCEARRAY
          and b.CALCULATIONTYPE <> 'INTERNAL'
    group by b.REPORT,
          case b.AGG_VARIABLE_A_FLAG
           when 'N' then null
           when 'Y' then a.AGG_VARIABLE_A
          end,
          case b.AGG_VARIABLE_B_FLAG
           when 'N' then null
           when 'Y' then a.AGG_VARIABLE_B
          end

1 个答案:

答案 0 :(得分:0)

r3子句中添加<select v-model="newPsu" class="form-control-psu" multiple="true" name="psuDropdown" id="psuDropdown" size="10" style="max-height: 100px"> <option v-if="level>= 1" v-for="option in provinsiOptions" v-bind:value="option.text"> {{ option.text }} </option> <option v-if="level>= 2" v-for="option in kabupatenOptions" v-bind:value="option.text"> {{ option.text }} </option> </select>