需要解决分组

时间:2013-05-13 08:11:32

标签: sql oracle plsql oracle11g plsqldeveloper

SELECT WK_NBR,region_cd,plant_cd, POS_GAP_1DAY 
  FROM(SELECT wk_nbr,region_cd,plant_cd, 
       round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END     ),2) AS POS_GAP_1DAY
       FROM
         table a
       group by wk_nbr,region_cd,plant_cd)

在上面的查询中我遇到的问题是POS_GAP_1DAY应该只汇总列中的正数,但上面的查询将无法给出正确的答案,因为我选中了SUM(gap_1day)>0

问题是我只需要wk_nbrregion_cdplant_cd进行分组。

请建议

2 个答案:

答案 0 :(得分:3)

尝试更改

round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END     ),2) AS POS_GAP_1DAY

ROUND(SUM(CASE WHEN SIGN(gap_1day) > 0 THEN gap_1day ELSE 0 END), 2) AS POS_GAP_1DAY

答案 1 :(得分:2)

当场完成,所以我不知道它是否有效,但是......

 SELECT wk_nbr,region_cd,plant_cd, 
        round(SUM(CASE WHEN GAP_1DAY>0 then GAP_1DAY else 0 end),2) AS POS_GAP_1DAY
   FROM
     table a
   group by wk_nbr,region_cd,plant_cd

不能做你想做的事吗?