SUM字段不求和

时间:2012-09-27 10:23:34

标签: sql database tsql plsql

与我的上一个问题类似。继承我的代码:

  SELECT TR.ITEMNO,
      SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE 0 END ) AS SUM_IN,
      SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'MH' THEN 1 ELSE 0 END ) AS SUM_OUT
  FROM IQMS.TRANSLOG TR
  WHERE TR.ITEMNO = '200672'
  GROUP BY TR.ITEMNO
  HAVING SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE -1 END ) < 0
  ORDER BY TR.ITEMNO

代码运行正常,但它没有为SUM_OUT总结任何内容。我知道有一个事实,那里有数字要总结。有什么明显的东西让我失踪吗?提前致谢!! :)

1 个答案:

答案 0 :(得分:3)

...试

SUM (TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'PO' THEN 1 ELSE 0 END ) 
OVER(PARTITION BY TR.ITEMNO)  AS SUM_IN,

SUM ( TR.TRANS_QUAN * CASE TR.TRANS_TYPE WHEN 'MH' THEN 1 ELSE 0 END )
OVER(PARTITION BY TR.ITEMNO)  AS SUM_OUT,