了解Oracle中的SUM

时间:2015-10-26 17:47:37

标签: sql oracle

所以,我认为直到今天我对sum函数的作用有了不错的理解......我有一个问题:

select me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST

 from ARTICLE me
inner join PURCHASING pi
on pi.doc_no = me.doc_no
inner join ARTICLE_VALU mv
on me.ARTICLE_NO = mv.ARTICLE_NO
where me.flag = 'Y'
and mv.prce > 0
and me.ARTICLE_NO in ('TYH0P',
                     'H5TK11',
                     'P4156321',
                     'G54621',
                     '54689P-01',
                     '52P100-02',
                     '6JGHYK',
                     'GGDSJH-09',
                     'JHDKF-04',
                     'GH51JKF')
group by me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST

返回以下内容:

enter image description here

那么,我真正想要的是数量的总和,所以我已将查询更新为以下内容:

select me.ARTICLE_NO, 
   me.DUE_DATE, 
   sum(me.QTY)
   from ARTICLE me
   inner join PURCHASING pi
   on pi.doc_no = me.doc_no
 inner join ARTICLE_VALU mv
   on me.ARTICLE_NO = mv.ARTICLE_NO
   where me.flag = 'Y'
   and mv.prce > 0
    and me.ARTICLE_NO in ('TYH0P',
                     'H5TK11',
                     'P4156321',
                     'G54621',
                     '54689P-01',
                     '52P100-02',
                     '6JGHYK',
                     'GGDSJH-09',
                     'JHDKF-04',
                     'GH51JKF')
group by me.ARTICLE_NO, me.DUE_DATE, me.QTY

我预计结果会变成:

new results

相反,我得到了这个:

enter image description here

我只是不知道为什么它没有给每一个ARTICLE NO一个总和值,也没有给出正确的总和。根据我的理解,总和受到where子句的约束,所以我在添加sum函数之前看到的数据集,我预计sum只会对该数据集中的那些值起作用而不会拾取任何其他数据。希望这些屏幕截图有意义。我不得不手动更改所有数据和表名等,以保持信息的私密性。

1 个答案:

答案 0 :(得分:3)

me.QTY

中删除GROUP BY
select 
   me.ARTICLE_NO, 
   me.DUE_DATE, 
   sum(me.QTY) AS Qty_Sum
from ARTICLE me
join PURCHASING pi
  on pi.doc_no = me.doc_no
join ARTICLE_VALU mv
  on me.ARTICLE_NO = mv.ARTICLE_NO
where me.flag = 'Y'
  and mv.prce > 0
  and me.ARTICLE_NO in ('TYH0P',
                        'H5TK11',
                        'P4156321',
                        'G54621',
                        '54689P-01',
                        '52P100-02',
                        '6JGHYK',
                        'GGDSJH-09',
                        'JHDKF-04',
                        'GH51JKF')
group by me.ARTICLE_NO,
         me.DUE_DATE;

修改

您可以使用CTE:

WITH cte AS
(
  select me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST
  from ARTICLE me
  inner join PURCHASING pi
    on pi.doc_no = me.doc_no
  inner join ARTICLE_VALU mv
    on me.ARTICLE_NO = mv.ARTICLE_NO
  where me.flag = 'Y'
    and mv.prce > 0
    and me.ARTICLE_NO in ('TYH0P',
                     'H5TK11',
                     'P4156321',
                     'G54621',
                     '54689P-01',
                     '52P100-02',
                     '6JGHYK',
                     'GGDSJH-09',
                     'JHDKF-04',
                     'GH51JKF')
 group by me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST
)
SELECT 
   ARTICLE_NO, 
   DUE_DATE, 
   sum(QTY) AS Qty_Sum
FROM cte
GROUP BY
   ARTICLE_NO,
   DUE_DATE;

避免混淆的一些解释:

当你写完:

select me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST
...
group by me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST

你基本上和以下一样:

select DISTINCT me.ARTICLE_NO, me.DUE_DATE, me.QTY, mv.COST

现在,您希望对此DISTINCT子查询进行操作以获得新结果。所以你需要用CTE / Subquery包装它来获得你提出的结果。您尝试将SUM添加到初始查询和更改分组的操作将针对不同的数据进行操作。