Sum(column_name) 和 sum(column_name) over (partition by id) 有什么区别
What is the difference between Sum(column_name) and sum(column_name) over (partition by id)
有 table 个包含两列的 bpm_no 和 total_amount。我试图总结每个 bpm_no 的 total_amount,其中 bpm_no 可以重复,并且 total_amount 列对于不同的行可能不同。我正在使用两种方法,一种如下所示:
select
a.bpm_no,
sum(a.total_amount)
from table a
group by a.bpm_no, a.total_amount
还有另一种方法,我使用如下方法进行分区:
select
a.bpm_no,
sum(a.total_amount) over (partition by a.bpm_no)
from table a
group by a.bpm_no,a.total_amount
但是,我得到了不同的结果,使用第一种方法,它丢弃了一些值。使用第二种方法时,它会按预期显示每条记录。请解释哪一个是正确的,这两种方法有什么区别。
试试这个:
SELECT
a.bpm_no,
SUM(a.total_amount)
FROM
table a
GROUP BY
a.bpm_no
由于您没有使用 group by
,它会将所有值相加
****演示****
使用带有 GROUP BY 的第一个查询应该可以满足您的要求
select a.bpm_no,
sum(a.total_amount)
from table a
GROUP BY a.bpm_no;
检查 this SQLFiddle 以供参考
在第一个查询中,您应该按如下方式编辑 Group By 子句。
select
a.bpm_no,
sum(a.total_amount)
from table a
group by a.bpm_no
第二种情况结果如下。
select
a.bpm_no,
sum(a.total_amount) over (partition by a.bpm_no)
from table a
group by a.bpm_no
在All Query Column Use With Aggregate Not Use In Group By Clause.
有 table 个包含两列的 bpm_no 和 total_amount。我试图总结每个 bpm_no 的 total_amount,其中 bpm_no 可以重复,并且 total_amount 列对于不同的行可能不同。我正在使用两种方法,一种如下所示:
select
a.bpm_no,
sum(a.total_amount)
from table a
group by a.bpm_no, a.total_amount
还有另一种方法,我使用如下方法进行分区:
select
a.bpm_no,
sum(a.total_amount) over (partition by a.bpm_no)
from table a
group by a.bpm_no,a.total_amount
但是,我得到了不同的结果,使用第一种方法,它丢弃了一些值。使用第二种方法时,它会按预期显示每条记录。请解释哪一个是正确的,这两种方法有什么区别。
试试这个:
SELECT
a.bpm_no,
SUM(a.total_amount)
FROM
table a
GROUP BY
a.bpm_no
由于您没有使用 group by
,它会将所有值相加
****演示****
使用带有 GROUP BY 的第一个查询应该可以满足您的要求
select a.bpm_no,
sum(a.total_amount)
from table a
GROUP BY a.bpm_no;
检查 this SQLFiddle 以供参考
在第一个查询中,您应该按如下方式编辑 Group By 子句。
select
a.bpm_no,
sum(a.total_amount)
from table a
group by a.bpm_no
第二种情况结果如下。
select
a.bpm_no,
sum(a.total_amount) over (partition by a.bpm_no)
from table a
group by a.bpm_no
在All Query Column Use With Aggregate Not Use In Group By Clause.