0
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_nbr, region_cd,进行分组plant_cd

请建议

4

2 回答 2

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
于 2013-05-13T08:17:51.397 回答
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

不做你想做的事?

于 2013-05-13T08:15:24.843 回答