sql聚合函数的使用 [英] use of sql aggregate functions

查看:90
本文介绍了sql聚合函数的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


sql的聚合函数SUM()是否可用于具有相同值的记录..
例如:
有一张桌子有
table1

o_id数量
a196 192
a196 496

Hi,
can sql''s aggregate function SUM() be used with records of same value..?
for ex:
there is a table which has
table1

o_id Qty
a196 192
a196 496

can i use sum() function (select o_id,sum(qty) from table1 )in such scenario..?

推荐答案


检查此
Hi ,
Check this
select o_id,sum(qty) from table1 
GROUP BY o_id


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli




是的,您可以使用,但是您应该为聚合函数列以外的内容放置一个group by子句.

例如
Hi,

Yes you can use, but you supposed to put an group by clause for other than Aggregate function columns.

e.g.
Select o_id,SUM(qty) From table1
GROUP BY o_id



这将起作用,并且为您提供数量的总和,并且输出仅等于1行.

问候,
V.J.



this will work and gives you sum of qty and output is equal to 1 row only.

Regards,
V.J.


在组中很简单,只需提供要分组的列名

使用此查询时
it is simple in group by give the column name on which you want to grouping

when you use this query
select o_id,sum(qty) from table1


它将返回错误
现在您可以像这样编写此查询:-


it will return error
now you can write this query like that:-

select o_id,sum(qty) from table1 group by o_id



那么它不会返回错误,并且得出的总和结果是:-192或496是否正确.

如果您想要192和496的总和,则可以将其写为:-



then it will not return error and give result of sum is:- either 192 or 496 is it correct.

if you want sum of 192 and 496 then you can write it as :-

select sum(qty) from table1 


那么它将返回688.


then it will return 688.


这篇关于sql聚合函数的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆